From b87561ffeee2c4e1d65417066d896afd965c8317 Mon Sep 17 00:00:00 2001 From: ruofeng Date: Fri, 18 Oct 2024 15:37:24 +0800 Subject: [PATCH] issue 1 and issue 2 --- lib/grammers-client/Cargo.toml | 1 + lib/grammers-tl-gen/src/enums.rs | 4 +++- lib/grammers-tl-gen/src/lib.rs | 2 ++ lib/grammers-tl-gen/src/structs.rs | 4 +++- lib/grammers-tl-types/Cargo.toml | 5 +++-- lib/grammers-tl-types/DEPS.md | 8 ++++++++ lib/grammers-tl-types/build.rs | 1 + lib/grammers-tl-types/src/lib.rs | 5 +++++ 8 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/grammers-client/Cargo.toml b/lib/grammers-client/Cargo.toml index 78f9b758..65833728 100644 --- a/lib/grammers-client/Cargo.toml +++ b/lib/grammers-client/Cargo.toml @@ -18,6 +18,7 @@ markdown = ["pulldown-cmark"] html = ["html5ever"] proxy = ["grammers-mtsender/proxy"] parse_invite_link = ["url"] +serde = ["grammers-tl-types/impl-serde"] [dependencies] chrono = "0.4.38" diff --git a/lib/grammers-tl-gen/src/enums.rs b/lib/grammers-tl-gen/src/enums.rs index 400bfdc0..c2ab1772 100644 --- a/lib/grammers-tl-gen/src/enums.rs +++ b/lib/grammers-tl-gen/src/enums.rs @@ -37,7 +37,9 @@ fn write_enum( writeln!(file, "{indent}#[derive(Debug)]")?; } - writeln!(file, "{indent}#[derive(serde_derive::Serialize, serde_derive::Deserialize)]")?; + if config.impl_serde{ + writeln!(file, "{indent}#[derive(serde_derive::Serialize, serde_derive::Deserialize)]")?; + } writeln!(file, "{indent}#[derive(Clone, PartialEq)]")?; writeln!( diff --git a/lib/grammers-tl-gen/src/lib.rs b/lib/grammers-tl-gen/src/lib.rs index 5cac0797..c2f1380c 100644 --- a/lib/grammers-tl-gen/src/lib.rs +++ b/lib/grammers-tl-gen/src/lib.rs @@ -26,6 +26,7 @@ pub struct Config { pub impl_debug: bool, pub impl_from_type: bool, pub impl_from_enum: bool, + pub impl_serde: bool, } impl Default for Config { @@ -36,6 +37,7 @@ impl Default for Config { impl_debug: true, impl_from_type: true, impl_from_enum: true, + impl_serde: false, } } } diff --git a/lib/grammers-tl-gen/src/structs.rs b/lib/grammers-tl-gen/src/structs.rs index 374573ea..61b69c59 100644 --- a/lib/grammers-tl-gen/src/structs.rs +++ b/lib/grammers-tl-gen/src/structs.rs @@ -63,7 +63,9 @@ fn write_struct( writeln!(file, "{indent}#[derive(Debug)]")?; } - writeln!(file, "{indent}#[derive(serde_derive::Serialize, serde_derive::Deserialize)]")?; + if config.impl_serde{ + writeln!(file, "{indent}#[derive(serde_derive::Serialize, serde_derive::Deserialize)]")?; + } writeln!(file, "{indent}#[derive(Clone, PartialEq)]")?; write!( diff --git a/lib/grammers-tl-types/Cargo.toml b/lib/grammers-tl-types/Cargo.toml index 0e32f450..d7320cea 100644 --- a/lib/grammers-tl-types/Cargo.toml +++ b/lib/grammers-tl-types/Cargo.toml @@ -32,9 +32,10 @@ deserializable-functions = [] impl-debug = [] impl-from-enum = [] impl-from-type = [] +impl-serde = ["dep:serde", "dep:serde_derive"] tl-api = [] tl-mtproto = [] [dependencies] -serde = "1.0.210" -serde_derive = "1.0.210" +serde = { version = "1.0.210", optional = true } +serde_derive = { version = "1.0.210", optional = true } diff --git a/lib/grammers-tl-types/DEPS.md b/lib/grammers-tl-types/DEPS.md index 27fa3feb..541bef0d 100644 --- a/lib/grammers-tl-types/DEPS.md +++ b/lib/grammers-tl-types/DEPS.md @@ -11,3 +11,11 @@ Used to parse the `.tl` files provided by Telegram's open source projects. ## toml Used to test that this file lists all dependencies from `Cargo.toml`. + +## serde + +Support serde ecosystem. + +## serde_derive + +Macros that auto generate serde code. diff --git a/lib/grammers-tl-types/build.rs b/lib/grammers-tl-types/build.rs index ae1d2d90..0a4d0a2f 100644 --- a/lib/grammers-tl-types/build.rs +++ b/lib/grammers-tl-types/build.rs @@ -76,6 +76,7 @@ fn main() -> std::io::Result<()> { impl_debug: cfg!(feature = "impl-debug"), impl_from_enum: cfg!(feature = "impl-from-enum"), impl_from_type: cfg!(feature = "impl-from-type"), + impl_serde: cfg!(feature = "impl-serde"), }; generate_rust_code(&mut file, &definitions, layer, &config)?; diff --git a/lib/grammers-tl-types/src/lib.rs b/lib/grammers-tl-types/src/lib.rs index c26f5765..b2f91e17 100644 --- a/lib/grammers-tl-types/src/lib.rs +++ b/lib/grammers-tl-types/src/lib.rs @@ -84,17 +84,22 @@ pub use deserialize::{Cursor, Deserializable}; pub use generated::{enums, functions, name_for_id, types, LAYER}; pub use serialize::Serializable; +#[cfg(feature = "impl-serde")] +use serde_derive::{Deserialize, Serialize}; + /// This struct represents the concrete type of a vector, that is, /// `vector` as opposed to the type `Vector`. This bare type is less /// common, so instead of creating a enum for `Vector` wrapping `vector` /// as Rust's `Vec` (as we would do with auto-generated code), /// a new-type for `vector` is used instead. +#[cfg_attr(feature = "impl-serde", derive(Serialize, Deserialize))] #[derive(Clone, Debug, PartialEq)] pub struct RawVec(pub Vec); /// This struct represents an unparsed blob, which should not be deserialized /// as a bytes string. Used by functions returning generic objects which pass /// the underlying result without any modification or interpretation. +#[cfg_attr(feature = "impl-serde", derive(Serialize, Deserialize))] #[derive(Clone, Debug, PartialEq)] pub struct Blob(pub Vec);