os_path.rs
raw
//! Serde adapter serializing [`PathBuf`] fields via [`OsString`].
//!
//! Serde's own `Path` implementation rejects paths that are not valid
//! UTF-8, but file names on real filesystems are arbitrary bytes and a
//! backup tool must preserve them exactly. `OsString`'s serde encoding
//! handles the platform's raw representation, so path fields annotated
//! with `#[serde(with = "os_path")]` round-trip losslessly.
use std::{
ffi::OsString,
path::{Path, PathBuf},
};
use serde::{Deserialize as _, Serialize as _};
pub fn serialize<S>(path: &Path, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
path.as_os_str().serialize(serializer)
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<PathBuf, D::Error>
where
D: serde::Deserializer<'de>,
{
OsString::deserialize(deserializer).map(PathBuf::from)
}