dtolnay / miniserde

Data structure serialization library with several opposite design goals from Serde

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Shall it contain adapters for Display and FromStr?

vi opened this issue · comments

Like these:

macro_rules! miniserialize_for_display {
    ($t:ty) => {
        impl miniserde::Serialize for $t {
            fn begin(&self) -> miniserde::ser::Fragment {
                miniserde::ser::Fragment::Str(std::borrow::Cow::Owned(format!("{}",self)))
            }
        }
    };
}
macro_rules! minideserialize_for_fromstr {
    ($t:ty) => {
        impl miniserde::Deserialize for $t {
            fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
                make_place!(Place);
                impl miniserde::de::Visitor for Place<$t> {
                    fn string(&mut self, s: &str) -> miniserde::Result<()> {
                        match std::str::FromStr::from_str(s) {
                            Ok(x) => {
                                self.out = Some(x);
                                Ok(())
                            },
                            Err(_) => Err(miniserde::Error),
                        }
                    }
                }
                Place::new(out)
            }
        }
    };
}

Combined with strum it should provide easy miniserde for enums.

Thanks for the idea! I would prefer not to include these. Maybe there could be a separate crate that provides such helpers for implementing miniserde::Serialize and miniserde::Deserialize, like what we have for Serde with crates like serde_aux, serde_with, serde_plain. The macros you suggested are a lot like these two:

Shall it be mini-crates for each adapter and helper or one general big "miniserde toolbox" enhancement crate?

Shall some documentation example (and maybe the currently only structs with named fields are supported error message) mention the external crate?

If it were up to me, I would prefer one consolidated toolbox crate. But when adding any helpers that take a noticeable amount of time to compile or that pull in any additional dependencies, I would put those behind an off-by-default Cargo cfg. 😸

I posted a request for implementation in dtolnay/request-for-implementation#11.