aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Letan <lthms@soap.coffee>2020-10-22 17:30:19 +0200
committerThomas Letan <lthms@soap.coffee>2020-10-22 17:30:19 +0200
commit1f203c30307c611503efa32cc92f1bf5ebf8a0eb (patch)
tree4450f827a0d3a3011b67ec571c768ba92416ab5c
parentRemove the Routing trait in favor of the more logical TypedUrl (diff)
Derive Serialize implementation
-rw-r--r--Cargo.toml3
-rw-r--r--typed-urls-derive/src/lib.rs20
2 files changed, 22 insertions, 1 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 6de5511..3c0c232 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -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
diff --git a/typed-urls-derive/src/lib.rs b/typed-urls-derive/src/lib.rs
index afee82a..4c727fc 100644
--- a/typed-urls-derive/src/lib.rs
+++ b/typed-urls-derive/src/lib.rs
@@ -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
}