Skip to content

Commit

Permalink
issue 1 and issue 2
Browse files Browse the repository at this point in the history
  • Loading branch information
RuofengX committed Oct 18, 2024
1 parent 389e7b3 commit b87561f
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/grammers-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion lib/grammers-tl-gen/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ fn write_enum<W: Write>(
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!(
Expand Down
2 changes: 2 additions & 0 deletions lib/grammers-tl-gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -36,6 +37,7 @@ impl Default for Config {
impl_debug: true,
impl_from_type: true,
impl_from_enum: true,
impl_serde: false,
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion lib/grammers-tl-gen/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ fn write_struct<W: Write>(
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!(
Expand Down
5 changes: 3 additions & 2 deletions lib/grammers-tl-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
8 changes: 8 additions & 0 deletions lib/grammers-tl-types/DEPS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions lib/grammers-tl-types/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
5 changes: 5 additions & 0 deletions lib/grammers-tl-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(pub Vec<T>);

/// 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<u8>);

Expand Down

0 comments on commit b87561f

Please sign in to comment.