~lthms/typed-urls

1f203c30307c611503efa32cc92f1bf5ebf8a0eb — Thomas Letan 2 months ago 88801d6
Derive Serialize implementation
2 files changed, 22 insertions(+), 1 deletions(-)

M Cargo.toml
M typed-urls-derive/src/lib.rs
M Cargo.toml => Cargo.toml +2 -1
@@ 9,5 9,6 @@ edition = "2018"
license = "AGPL-3.0-or-later"

[dependencies]
serde_json = "*"
serde = "1.0"
serde_json = "1.0"
typed-urls-derive = { path = "typed-urls-derive/" }
\ No newline at end of file

M typed-urls-derive/src/lib.rs => typed-urls-derive/src/lib.rs +20 -0
@@ 130,6 130,24 @@ fn derive_enum_display_impl(
    }
}

fn derive_enum_serialize_impl(
    name : &Ident,
) -> Quote {
    quote! {
        impl serde::Serialize for #name {
            fn serialize<S>(
                &self,
                serializer : S,
            ) -> Result<S::Ok, S::Error>
            where
                S : serde::Serializer,
            {
                serializer.serialize_str(&self.to_string())
            }
        }
    }
}

fn ident_to_litstr(
    prefix : &str,
    id : &Ident,


@@ 183,6 201,7 @@ fn derive_typed_url_boilerplate(ast : &DeriveInput) -> Quote {
    let route_display = derive_route_display_impl(enum_ast, &fmts, &fields);
    let routes = derive_routes_array(enum_ast);

    let enum_serialize_impl = derive_enum_serialize_impl(enum_name);
    let enum_typed_url_impl =
        derive_enum_typed_url_impl(enum_name, enum_ast, &typed_field, &fields);



@@ 194,6 213,7 @@ fn derive_typed_url_boilerplate(ast : &DeriveInput) -> Quote {
        #route_display
        #routes

        #enum_serialize_impl
        #enum_display
        #enum_typed_url_impl
    }