Skip to content

Commit

Permalink
Deserialize PathBuf from bytes
Browse files Browse the repository at this point in the history
&Path already allows this. Also complete the tests for Path/PathBuf.
  • Loading branch information
heftig committed Oct 22, 2019
1 parent cf31418 commit 42990d8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
18 changes: 18 additions & 0 deletions serde/src/de/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1580,6 +1580,24 @@ impl<'de> Visitor<'de> for PathBufVisitor {
{
Ok(From::from(v))
}

fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where
E: Error,
{
str::from_utf8(v)
.map(From::from)
.map_err(|_| Error::invalid_value(Unexpected::Bytes(v), &self))
}

fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
where
E: Error,
{
String::from_utf8(v)
.map(From::from)
.map_err(|e| Error::invalid_value(Unexpected::Bytes(&e.into_bytes()), &self))
}
}

#[cfg(feature = "std")]
Expand Down
12 changes: 12 additions & 0 deletions test_suite/tests/test_de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -889,11 +889,23 @@ declare_tests! {
Path::new("/usr/local/lib") => &[
Token::BorrowedStr("/usr/local/lib"),
],
Path::new("/usr/local/lib") => &[
Token::BorrowedBytes(b"/usr/local/lib"),
],
}
test_path_buf {
PathBuf::from("/usr/local/lib") => &[
Token::Str("/usr/local/lib"),
],
PathBuf::from("/usr/local/lib") => &[
Token::String("/usr/local/lib"),
],
PathBuf::from("/usr/local/lib") => &[
Token::Bytes(b"/usr/local/lib"),
],
PathBuf::from("/usr/local/lib") => &[
Token::ByteBuf(b"/usr/local/lib"),
],
}
test_cstring {
CString::new("abc").unwrap() => &[
Expand Down

0 comments on commit 42990d8

Please sign in to comment.