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

refactor(model, util, validate)!: Convert CommandOptionChoice to a struct with value variants #2081

Merged

Conversation

suneettipirneni
Copy link
Member

@suneettipirneni suneettipirneni commented Jan 20, 2023

This PR refactors CommandOptionChoice to be a struct rather than an enum. The main reasoning behind this has to do with the fact that all choice variants have fields in common (name and name_localizations). Accessing these fields currently requires the user to exhaustively match an enum variant. With these changes, the shared fields can be accessed directly and instead variance is introduced by using an enum type on the value field.

@github-actions github-actions bot added c-model Affects the model crate t-feature Addition of a new feature labels Jan 20, 2023
Copy link
Member

@zeylahellyer zeylahellyer left a comment

Choose a reason for hiding this comment

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

Could be nice to have this for name_localizations as well?

@suneettipirneni
Copy link
Member Author

I was thinking about my implementation and think it can be improved. What are your thoughts on a breaking change to make CommandOptionChoice a struct and have it's value be an enum. ie

struct CommandOptionChoice {
	pub name: String,
	pub name_localizations: Option<HashMap<String, String>>,
	pub value: CommandOptionChoiceValue,
}

enum CommandOptionChoiceValue {
	String(String),
	Integer(i64),
	Number(f64),
}

This way if/when new choice variants are added (or possibly new fields) we don't need to update name() and name_localization() getters.

@vilgotf
Copy link
Member

vilgotf commented Jan 24, 2023

I think that's an even better change!

@github-actions github-actions bot added c-all Affects all crates or the project as a whole c-book Affects the book c-cache Affects the cache crate c-gateway Affects the gateway crate c-gateway-queue Affects the gateway queue crate c-http Affects the http crate c-http-ratelimiting Affects the http ratelimiting crate c-lavalink Affects the lavalink crate c-mention Affects the mention crate c-standby Affects the standby crate c-util Affects the util crate c-validate Affects the validate crate labels Jan 24, 2023
@suneettipirneni suneettipirneni changed the title feat(model): Add name() getter to CommandOptionChoice feat(model)!: Convert CommandOptionChoice to a struct with value variants Jan 24, 2023
@suneettipirneni suneettipirneni changed the base branch from main to next January 24, 2023 19:16
@github-actions github-actions bot added m-breaking change Breaks the public API. and removed c-book Affects the book c-lavalink Affects the lavalink crate c-mention Affects the mention crate c-standby Affects the standby crate c-validate Affects the validate crate c-gateway-queue Affects the gateway queue crate c-http Affects the http crate c-gateway Affects the gateway crate c-http-ratelimiting Affects the http ratelimiting crate labels Jan 24, 2023
@github-actions github-actions bot removed the c-cache Affects the cache crate label Jan 24, 2023
@suneettipirneni suneettipirneni changed the title feat(model)!: Convert CommandOptionChoice to a struct with value variants refactor(model)!: Convert CommandOptionChoice to a struct with value variants Jan 24, 2023
@github-actions github-actions bot added the t-refactor Refactors APIs or code. label Jan 24, 2023
Copy link
Member

@vilgotf vilgotf left a comment

Choose a reason for hiding this comment

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

A very nice improvement!
One thing I noticed in the builder API is that we silently discard localizations if the choice is not found. It might be better to panic to alert users of their buggy code. Something like:

    /// ...
    /// # Panics
    ///
    /// Panics if the choice is not set.
    /// ...
    #[track_caller]
    pub fn choice_localizations<K: Into<String>, V: Into<String>>(
        mut self,
        choice_name: &str,
        name_localizations: impl IntoIterator<Item = (K, V)>,
    ) -> Self {
        let choice = self
            .0
            .choices
            .as_mut()
            .expect("choices are set")
            .iter_mut()
            .find(
                |choice| matches!(choice, CommandOptionChoice{ name, value: CommandOptionChoiceValue::Integer(_), ..}  if name == choice_name),
            ).expect("choice is set");
        // ...

twilight-model/src/application/command/option.rs Outdated Show resolved Hide resolved
twilight-model/src/application/command/option.rs Outdated Show resolved Hide resolved
twilight-util/src/builder/command.rs Outdated Show resolved Hide resolved
twilight-util/src/builder/command.rs Outdated Show resolved Hide resolved
@github-actions github-actions bot added the c-validate Affects the validate crate label Jan 28, 2023
@suneettipirneni suneettipirneni changed the title refactor(model)!: Convert CommandOptionChoice to a struct with value variants refactor(model, util, validate)!: Convert CommandOptionChoice to a struct with value variants Jan 28, 2023
@suneettipirneni suneettipirneni requested review from vilgotf and zeylahellyer and removed request for vilgotf and zeylahellyer January 28, 2023 20:22
Copy link
Member

@vilgotf vilgotf left a comment

Choose a reason for hiding this comment

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

LGTM, I pushed some small changes that I did not felt warranted a second review round

@zeylahellyer zeylahellyer self-assigned this Feb 4, 2023
@zeylahellyer zeylahellyer merged commit 9a683a7 into twilight-rs:next Feb 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c-all Affects all crates or the project as a whole c-model Affects the model crate c-util Affects the util crate c-validate Affects the validate crate m-breaking change Breaks the public API. t-feature Addition of a new feature t-refactor Refactors APIs or code.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants