harp/
path.rs

1//! Remote paths on HTTP servers
2
3#[cfg(feature = "alloc")]
4use {
5    crate::error::Error,
6    alloc::{borrow::ToOwned, string::String},
7    core::{
8        fmt::{self, Display},
9        str::FromStr,
10    },
11};
12
13/// Paths to HTTP resources (owned buffer)
14// TODO: corresponding borrowed `Path` type
15#[cfg(feature = "alloc")]
16pub struct PathBuf(String);
17
18#[cfg(feature = "alloc")]
19impl FromStr for PathBuf {
20    type Err = Error;
21
22    /// Create a path from the given string
23    fn from_str(path: &str) -> Result<Self, Error> {
24        // TODO: validate path
25        Ok(PathBuf(path.to_owned()))
26    }
27}
28
29#[cfg(feature = "alloc")]
30impl AsRef<str> for PathBuf {
31    fn as_ref(&self) -> &str {
32        self.0.as_ref()
33    }
34}
35
36#[cfg(feature = "alloc")]
37impl Display for PathBuf {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        self.0.fmt(f)
40    }
41}
42
43#[cfg(feature = "alloc")]
44impl<'a> From<&'a str> for PathBuf {
45    fn from(path: &str) -> Self {
46        Self::from_str(path).unwrap()
47    }
48}