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

Derive value improvements for enums #540

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

katyo
Copy link

@katyo katyo commented Jan 6, 2024

  • Support enums without explicit values
  • Support string representation for enums

@katyo
Copy link
Author

katyo commented Jan 6, 2024

As a future improvement: what about using external crate for ident case conversion?

Copy link
Contributor

@zeenix zeenix left a comment

Choose a reason for hiding this comment

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

Thanks for this contribution! Some nits to fix. Talking of which, could you kindly add emoji prefixes as per our contribution guidelines?

zvariant_derive/tests/tests.rs Outdated Show resolved Hide resolved
zvariant_derive/tests/tests.rs Outdated Show resolved Hide resolved
@zeenix
Copy link
Contributor

zeenix commented Jan 6, 2024

As a future improvement: what about using external crate for ident case conversion?

I'm not sure what you mean but I'd like to avoid adding a dep unless there is a compelling rationale presented. :)

@zeenix
Copy link
Contributor

zeenix commented Feb 7, 2024

@katyo Hi there. Do you think you'll have time to finish this soon? No hurry, just a polite ping. :)

@katyo
Copy link
Author

katyo commented Feb 19, 2024

@zeenix I'm a little bit busy lately, but I try fix this pr asap.

@katyo katyo force-pushed the derive-value-enum branch 3 times, most recently from f60a387 to 57bb1ce Compare February 25, 2024 09:40
@katyo katyo force-pushed the derive-value-enum branch from 57bb1ce to 69ca851 Compare February 25, 2024 10:05
@katyo katyo requested a review from zeenix February 25, 2024 10:06
Copy link
Contributor

@zeenix zeenix left a comment

Choose a reason for hiding this comment

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

Looks great in general. Thanks for updating. BTW, you forgot to add emojis still. ;)

@@ -1496,8 +1496,12 @@ mod tests {
let decoded: Enum = encoded.deserialize().unwrap().0;
assert_eq!(decoded, Enum::Variant3);

assert_eq!(Value::from(Enum::Variant1), Value::U8(0));
assert_eq!(Enum::try_from(Value::U8(2)), Ok(Enum::Variant3));
assert_eq!(Enum::try_from(Value::U8(4)), Err(Error::IncorrectType));
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! I think you want to also update the docs.

#[derive(Deserialize, Serialize, Type, Debug, PartialEq)]
#[zvariant(signature = "s")]
#[derive(Deserialize, Serialize, Type, Value, OwnedValue, Debug, PartialEq)]
#[zvariant(signature = "s", rename_all = "snake_case")]
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be nice to document these additions in the macro docs.

}
}

#into_value
})
}

fn enum_name_for_variant(
Copy link
Contributor

Choose a reason for hiding this comment

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

SerializeDict and DeserializeDict already support these renaming attributes so I'm sure there is existing code you can use.

Copy link
Author

Choose a reason for hiding this comment

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

Of course, but currently dict_name_for_field expect syn::Field as an argument, not something like Literal or String. I would like to avoid modifying existing code which is not related to this PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

It is related to this PR since there is no good reason to duplicate the code. AFAIK, it's a pretty trivial change so why not just do it? It will need to be renamed and moved to utils mod btw.

Copy link
Author

Choose a reason for hiding this comment

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

Ok

@katyo katyo force-pushed the derive-value-enum branch from 69ca851 to 0f102fc Compare February 25, 2024 14:12
@zeenix
Copy link
Contributor

zeenix commented Jul 11, 2024

@zeenix I'm a little bit busy lately, but I try fix this pr asap.

Just a gentle reminder, in hope that you're not super busy and can finish this PR. 🙏

@zeenix
Copy link
Contributor

zeenix commented Jul 30, 2024

Just a gentle reminder, in hope that you're not super busy and can finish this PR. 🙏

I guess not. 😞

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