mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-12 23:41:03 +00:00
impl Deserialize for &'a Path
This commit is contained in:
@@ -897,6 +897,44 @@ parse_impl!(net::SocketAddrV6);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
struct PathVisitor;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<'a> Visitor<'a> for PathVisitor {
|
||||
type Value = &'a Path;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str("a borrowed path")
|
||||
}
|
||||
|
||||
fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Ok(v.as_ref())
|
||||
}
|
||||
|
||||
fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
str::from_utf8(v)
|
||||
.map(AsRef::as_ref)
|
||||
.map_err(|_| Error::invalid_value(Unexpected::Bytes(v), &self))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<'de: 'a, 'a> Deserialize<'de> for &'a Path {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_str(PathVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
struct PathBufVisitor;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user