//! 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(path: &Path, serializer: S) -> Result where S: serde::Serializer, { path.as_os_str().serialize(serializer) } pub fn deserialize<'de, D>(deserializer: D) -> Result where D: serde::Deserializer<'de>, { OsString::deserialize(deserializer).map(PathBuf::from) }