Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shall it contain adapters for Display and FromStr? #3

Closed
vi opened this issue Sep 16, 2018 · 4 comments
Closed

Shall it contain adapters for Display and FromStr? #3

vi opened this issue Sep 16, 2018 · 4 comments

Comments

@vi
Copy link

vi commented Sep 16, 2018

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.

@dtolnay
Copy link
Owner

dtolnay commented Sep 16, 2018

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:

@dtolnay dtolnay closed this as completed Sep 16, 2018
@vi
Copy link
Author

vi commented Sep 16, 2018

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?

@dtolnay
Copy link
Owner

dtolnay commented Sep 16, 2018

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. 😸

@dtolnay
Copy link
Owner

dtolnay commented Jan 22, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants