Better virtual file system, keyboard through MMIO&VirtIO
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
use core::ops::Deref;
|
||||
#[cfg(feature = "alloc")]
|
||||
use core::{borrow::Borrow, ops::Deref};
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
use alloc::borrow::ToOwned;
|
||||
#[cfg(feature = "alloc")]
|
||||
use alloc::string::String;
|
||||
|
||||
#[repr(transparent)]
|
||||
#[derive(Debug)]
|
||||
pub struct Path {
|
||||
inner: str,
|
||||
}
|
||||
@@ -19,6 +23,13 @@ impl AsRef<Path> for str {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
impl AsRef<Path> for String {
|
||||
fn as_ref(&self) -> &Path {
|
||||
self.as_str().as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<Path> for Path {
|
||||
fn as_ref(&self) -> &Path {
|
||||
self
|
||||
@@ -32,9 +43,17 @@ impl AsRef<str> for Path {
|
||||
}
|
||||
|
||||
impl Path {
|
||||
pub fn new(path: &str) -> &Self {
|
||||
path.as_ref()
|
||||
}
|
||||
|
||||
pub fn split_path(&self) -> (&str, Option<&Path>) {
|
||||
if let Some((start, end)) = self.inner.split_once("/") {
|
||||
(start, Some(end.into()))
|
||||
if end.is_empty() {
|
||||
(start, None)
|
||||
} else {
|
||||
(start, Some(end.into()))
|
||||
}
|
||||
} else {
|
||||
(&self.inner, None)
|
||||
}
|
||||
@@ -48,9 +67,46 @@ impl Path {
|
||||
pub fn is_relative(&self) -> bool {
|
||||
!self.is_absolute()
|
||||
}
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.inner.is_empty()
|
||||
}
|
||||
pub fn is_root(&self) -> bool {
|
||||
self.as_str() == "/"
|
||||
}
|
||||
|
||||
pub fn starts_with<P: AsRef<Self>>(&self, other: P) -> bool {
|
||||
let this = self.split_path();
|
||||
let other = other.as_ref().split_path();
|
||||
if this.0 == other.0 {
|
||||
if let Some(this) = this.1
|
||||
&& let Some(other) = other.1
|
||||
{
|
||||
this.starts_with(other)
|
||||
} else {
|
||||
other.1.is_none()
|
||||
}
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
pub fn without<P: AsRef<Self>>(&self, other: P) -> &Path {
|
||||
let this = self.split_path();
|
||||
let other = other.as_ref().split_path();
|
||||
if this.0 == other.0 {
|
||||
match (this.1, other.1) {
|
||||
(None, None) => Path::new(""),
|
||||
(None, Some(_)) => todo!(),
|
||||
(Some(p), None) => p,
|
||||
(Some(this), Some(other)) => this.without(other),
|
||||
}
|
||||
} else {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
#[derive(Debug, Hash, PartialEq, Eq)]
|
||||
pub struct PathBuf {
|
||||
inner: String,
|
||||
}
|
||||
@@ -63,3 +119,26 @@ impl Deref for PathBuf {
|
||||
self.inner.as_str().into()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
impl AsRef<Path> for PathBuf {
|
||||
fn as_ref(&self) -> &Path {
|
||||
self.inner.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
impl Borrow<str> for PathBuf {
|
||||
fn borrow(&self) -> &str {
|
||||
&self.inner
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
impl From<&str> for PathBuf {
|
||||
fn from(val: &str) -> Self {
|
||||
PathBuf {
|
||||
inner: val.to_owned(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user