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

Use enums instead of string constants #52

Merged
merged 2 commits into from
Jun 24, 2024
Merged

Use enums instead of string constants #52

merged 2 commits into from
Jun 24, 2024

Conversation

robin-nitrokey
Copy link
Member

Previously, we used String<_> to represent string constants in some responses. This has multiple drawbacks:

  • It is error-prone because the value is not validated.
  • Construction is fallible because of the fixed length of the string.
  • The length needs to be bumped if longer values are added.

This patch introduces enums to replace these constants. As cbor_smol serializes enums using the variant index instead of the string, we need to manually implement the string conversion.

Previously, we used String<_> to represent string constants in some
responses.  This has multiple drawbacks:
- It is error-prone because the value is not validated.
- Construction is fallible because of the fixed length of the string.
- The length needs to be bumped if longer values are added.

This patch introduces enums to replace these constants.  As cbor_smol
serializes enums using the variant index instead of the string, we need
to manually implement the string conversion.
Copy link
Contributor

@sosthene-nitrokey sosthene-nitrokey left a comment

Choose a reason for hiding this comment

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

I didn't know about #[serde(into = "…")]

I would have used a newtype that is just a wrapper around a &'static str, with some associated constants, but enums are better given into.

These tests makes sure that the serialization of the new enums works
correctly, and also help to detect accidental changes to the field order
used by serde_indexed in the response struct.
@robin-nitrokey robin-nitrokey merged commit 3a6cc6c into main Jun 24, 2024
8 checks passed
@robin-nitrokey robin-nitrokey deleted the enums branch June 24, 2024 11:38
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