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

Add serde support for tl_types. #277

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

RuofengX
Copy link

@RuofengX RuofengX commented Oct 18, 2024

Add two lines for each entrance in generated.rs

  • all enums
  • all structs
    (Did I miss for other things?)

Pass all tests in crate grammers-tl-types

@RuofengX
Copy link
Author

Related issue #276

@RuofengX
Copy link
Author

RuofengX commented Oct 18, 2024

Whoops, generated code seems encount some issue.

@RuofengX RuofengX changed the title Add serde support for tl_types. [WIP]Add serde support for tl_types. Oct 18, 2024
@Lonami
Copy link
Owner

Lonami commented Oct 18, 2024

Can you revert the formatting change to the .toml files out of this PR?

Copy link
Owner

@Lonami Lonami left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand why so many crates need the serde dependency. Shouldn't it only be grammers-tl-types?

Also, please make it optional. I want to continue offering the crates with serde off by default.

@RuofengX
Copy link
Author

RuofengX commented Oct 18, 2024

I'm not sure I understand why so many crates need the serde dependency. Shouldn't it only be grammers-tl-types?

I tried to add serde code only in grammers-tl-types first, all tests pass in tl-types crates.

Then I add my fork of grammers-client in my project, it compile with error saying that the generated generated.rs(that long file) missing dependence serde. I checked it twice, the error is not from grammers-tl-types, it from grammers-seesion. Then I added serde and serde-derive dep in all crates that depend on grammers-tl-types. Thus no error again.

I not formillar with build system, but I am looking into it.


Also, please make it optional. I want to continue offering the crates with serde off by default.

Sure, I will write cfg(feature="serde") in grammers-tl-gen crate, then added a new feature in grammers-client after first issue done.

@RuofengX
Copy link
Author

RuofengX commented Oct 18, 2024

I think it's done. Our work is never over.

Behavior now

User can fetch any raw struct (defined by grammers_tl_types) returned by client function after enable the serde feature tag.

Flaw

Structure in grammers_client like Client, Update still not deserializable, because it contains runtime data. Further work need to be done. I will implement only serialize for those structure soon.

Example

https://github.com/RuofengX/gray-mirror-tg/blob/3a6b343184307b9ccb9c66a74fb64bc8c041577c/src/app/mod.rs#L110

@RuofengX RuofengX changed the title [WIP]Add serde support for tl_types. Add serde support for tl_types. Oct 18, 2024
@RuofengX RuofengX requested a review from Lonami October 18, 2024 07:53
@RuofengX RuofengX marked this pull request as draft October 18, 2024 15:14
@RuofengX
Copy link
Author

We need more test and docs about this new feature.

@RuofengX RuofengX marked this pull request as ready for review October 18, 2024 15:42
#[derive(Clone, Debug, PartialEq)]
pub struct Blob(pub Vec<u8>);
pub struct Blob(#[cfg_attr(feature = "impl-serde", serde(with = "serde_bytes"))] pub Vec<u8>);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Help needed.

I am not formiliar with tl proto. Is there any other structure that store bytes in .tl definition files? All bytes-like properties need marked by serde_bytes for better performence.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, Vec<u8> are also generated for the TL bytes type. If that's the case, we may need to check the field types when generating code.

Might be good to write a test that makes sure this works as expected.

@@ -37,6 +37,10 @@ fn write_enum<W: Write>(
writeln!(file, "{indent}#[derive(Debug)]")?;
}

if config.impl_serde{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting is going to complain here.

@@ -63,6 +63,10 @@ fn write_struct<W: Write>(
writeln!(file, "{indent}#[derive(Debug)]")?;
}

if config.impl_serde{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here.

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

Successfully merging this pull request may close these issues.

2 participants