Search code examples
rustencodingutf-8serde

Encoding PathBuf containing path with invalid utf-8 characters using serde in Rust


I have a struct with pathbuf as one of its field, struct A { pub first: bool, pub path: PathBuf, } Then path can have invalid utf-8 characters as linux allow to create files and folder with invalid utf-8 characters. I am using rmp_serde::to_vec_named() to get vec from the struct object. But, In case of path with invalid utf-8 characters, it is crashing with Error : SerdeEncodeMspack(Syntax("path contains invalid UTF-8 characters")).

Is there any way to encode a struct with invalid utf-8 charcters without skipping it? Thanks in advance


Solution

  • Could you use a custom de/serialize function to convert it to/from an OsString when serializing?

    Something like this should work.

    use serde::{Deserialize, Serialize};
    use std::path::PathBuf;
    use std::ffi::OsString;
    
    #[derive(Serialize, Deserialize)]
    struct Demo {
        #[serde(with = "path_handling")]
        path: PathBuf,
    }
    
    mod path_handling {
        use super::*;
        use serde::de::Deserializer;
        use serde::ser::Serializer;
        pub fn serialize<S>(p: &PathBuf, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            p.as_os_str().serialize(serializer)
        }
        pub fn deserialize<'de, D>(deserializer: D) -> Result<PathBuf, D::Error>
        where
            D: Deserializer<'de>,
        {
            Ok(OsString::deserialize(deserializer)?.into())
        }
    }