1 // Copyright (c) The camino Contributors
2 // SPDX-License-Identifier: MIT OR Apache-2.0
4 //! Serde implementations for `Utf8Path`.
6 //! The Serde implementations for `Utf8PathBuf` are derived, but `Utf8Path` is an unsized type which
7 //! the derive impls can't handle. Implement these by hand.
10 use serde
::{de, Deserialize, Deserializer, Serialize, Serializer}
;
13 struct Utf8PathVisitor
;
15 impl<'a
> de
::Visitor
<'a
> for Utf8PathVisitor
{
16 type Value
= &'a Utf8Path
;
18 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
19 formatter
.write_str("a borrowed path")
22 fn visit_borrowed_str
<E
>(self, v
: &'a
str) -> Result
<Self::Value
, E
>
29 fn visit_borrowed_bytes
<E
>(self, v
: &'a
[u8]) -> Result
<Self::Value
, E
>
33 std
::str::from_utf8(v
)
35 .map_err(|_
| de
::Error
::invalid_value(de
::Unexpected
::Bytes(v
), &self))
39 impl<'de
: 'a
, 'a
> Deserialize
<'de
> for &'a Utf8Path
{
40 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
44 deserializer
.deserialize_str(Utf8PathVisitor
)
48 impl Serialize
for Utf8Path
{
49 fn serialize
<S
>(&self, serializer
: S
) -> Result
<S
::Ok
, S
::Error
>
53 self.as_str().serialize(serializer
)