Skip to content

Commit

Permalink
refactor: Derive Default for enums
Browse files Browse the repository at this point in the history
As of Rust 1.62, enums can use `#[derive(Default)]`.

See <rust-lang/rust#94457>.
  • Loading branch information
waywardmonkeys committed May 6, 2024
1 parent 41d8b0a commit f774596
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 28 deletions.
9 changes: 2 additions & 7 deletions fluent-bundle/examples/custom_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 4 additions & 14 deletions fluent-bundle/src/types/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
9 changes: 2 additions & 7 deletions fluent-bundle/tests/custom_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f774596

Please sign in to comment.