From 704e4ae4e75ee69ec0b71da64324d0d2e54613c7 Mon Sep 17 00:00:00 2001 From: Bruce Mitchener Date: Mon, 6 May 2024 14:40:09 +0700 Subject: [PATCH] refactor: Derive `Default` for enums (#356) --- fluent-bundle/examples/custom_type.rs | 9 ++------- fluent-bundle/src/types/number.rs | 18 ++++-------------- fluent-bundle/tests/custom_types.rs | 9 ++------- 3 files changed, 8 insertions(+), 28 deletions(-) diff --git a/fluent-bundle/examples/custom_type.rs b/fluent-bundle/examples/custom_type.rs index f3e3c3dc..a6093732 100644 --- a/fluent-bundle/examples/custom_type.rs +++ b/fluent-bundle/examples/custom_type.rs @@ -21,21 +21,16 @@ use fluent_bundle::{FluentArgs, FluentBundle, FluentResource, FluentValue}; // - timeStyle // // with an enum of allowed values. -#[derive(Debug, PartialEq, Eq, Clone, Hash)] +#[derive(Debug, Default, PartialEq, Eq, Clone, Hash)] enum DateTimeStyleValue { Full, Long, Medium, Short, + #[default] None, } -impl std::default::Default for DateTimeStyleValue { - fn default() -> Self { - Self::None - } -} - // This is just a helper to make it easier to convert // a value provided to FluentArgs into an option value. impl<'l> From<&FluentValue<'l>> for DateTimeStyleValue { diff --git a/fluent-bundle/src/types/number.rs b/fluent-bundle/src/types/number.rs index a81d15e3..b9699279 100644 --- a/fluent-bundle/src/types/number.rs +++ b/fluent-bundle/src/types/number.rs @@ -8,19 +8,14 @@ use intl_pluralrules::operands::PluralOperands; use crate::args::FluentArgs; use crate::types::FluentValue; -#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] +#[derive(Debug, Copy, Clone, Default, Hash, PartialEq, Eq)] pub enum FluentNumberStyle { + #[default] Decimal, Currency, Percent, } -impl std::default::Default for FluentNumberStyle { - fn default() -> Self { - Self::Decimal - } -} - impl From<&str> for FluentNumberStyle { fn from(input: &str) -> Self { match input { @@ -32,19 +27,14 @@ impl From<&str> for FluentNumberStyle { } } -#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] +#[derive(Debug, Copy, Clone, Default, Hash, PartialEq, Eq)] pub enum FluentNumberCurrencyDisplayStyle { + #[default] Symbol, Code, Name, } -impl std::default::Default for FluentNumberCurrencyDisplayStyle { - fn default() -> Self { - Self::Symbol - } -} - impl From<&str> for FluentNumberCurrencyDisplayStyle { fn from(input: &str) -> Self { match input { diff --git a/fluent-bundle/tests/custom_types.rs b/fluent-bundle/tests/custom_types.rs index 8c6f9169..082f864a 100644 --- a/fluent-bundle/tests/custom_types.rs +++ b/fluent-bundle/tests/custom_types.rs @@ -47,21 +47,16 @@ fn fluent_custom_type() { #[test] fn fluent_date_time_builtin() { - #[derive(Debug, PartialEq, Clone)] + #[derive(Debug, Default, PartialEq, Clone)] enum DateTimeStyleValue { Full, Long, Medium, Short, + #[default] None, } - impl std::default::Default for DateTimeStyleValue { - fn default() -> Self { - Self::None - } - } - impl<'l> From<&FluentValue<'l>> for DateTimeStyleValue { fn from(input: &FluentValue) -> Self { if let FluentValue::String(s) = input {