1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// License: see LICENSE file at root directory of `master` branch

//! # Root

pub mod en;

/// # Plural
///
/// This trait is implemented for all integers and floats along with [`Lang`][::Lang].
///
/// [::Lang]: enum.Lang.html
pub trait Plural<N> {

    /// # Formats self based on input value `n`
    fn fmt(&self, n: N) -> &str;

}

/// # Languages
///
/// Variants are named after ISO 639-1 codes: <https://en.wikipedia.org/wiki/ISO_639-1>
///
/// ## Examples
///
/// ```
/// use plurals::{Lang, Plural};
///
/// const FOOT: Lang = Lang::En { singular: "foot", plural: "feet" };
///
/// assert_eq!(FOOT.fmt(0), "feet");
/// assert_eq!(FOOT.fmt(1), "foot");
/// assert_eq!(FOOT.fmt(2), "feet");
/// assert_eq!(FOOT.fmt(4), "feet");
/// ```
#[derive(Debug, Eq, PartialEq, Hash)]
pub enum Lang<'a> {

    /// # English
    En {

        /// # Singular form
        singular: &'a str,

        /// # Plural form
        plural: &'a str,

    },

}

macro_rules! impl_plural_for_languages_with_unsigned_integers {
    ($($ty: ty, $test_fn_name: tt,)+) => {
        $(
            #[test]
            fn $test_fn_name() {
                assert_eq!(<$ty>::min_value(), 0);
            }

            impl<'a> Plural<$ty> for Lang<'a> {

                fn fmt(&self, n: $ty) -> &str {
                    match self {
                        Lang::En { singular, plural } => match n {
                            1 => singular,
                            _ => plural,
                        },
                    }
                }

            }
        )+
    }
}

impl_plural_for_languages_with_unsigned_integers!(
    u8, test_impl_plural_for_languages_with_u8,
    u16, test_impl_plural_for_languages_with_u16,
    u32, test_impl_plural_for_languages_with_u32,
    u64, test_impl_plural_for_languages_with_u64,
    u128, test_impl_plural_for_languages_with_u128,
    usize, test_impl_plural_for_languages_with_usize,
);

macro_rules! impl_plural_for_languages_with_signed_integers {
    ($($ty: ty, $test_fn_name: tt,)+) => {
        $(
            #[test]
            fn $test_fn_name() {
                assert!(<$ty>::min_value() < 0);
            }

            impl<'a> Plural<$ty> for Lang<'a> {

                fn fmt(&self, n: $ty) -> &str {
                    match self {
                        Lang::En { singular, plural } => match n {
                            -1 | 1 => singular,
                            _ => plural,
                        },
                    }
                }

            }
        )+
    }
}

impl_plural_for_languages_with_signed_integers!(
    i8, test_impl_plural_for_languages_with_i8,
    i16, test_impl_plural_for_languages_with_i16,
    i32, test_impl_plural_for_languages_with_i32,
    i64, test_impl_plural_for_languages_with_i64,
    i128, test_impl_plural_for_languages_with_i128,
    isize, test_impl_plural_for_languages_with_isize,
);

macro_rules! impl_plural_for_languages_with_floats {
    ($($ty: ty, $test_fn_name: tt,)+) => {
        $(
            #[test]
            fn $test_fn_name() {
                assert!(<$ty>::from(1.0).is_sign_positive());
            }

            impl<'a> Plural<$ty> for Lang<'a> {

                fn fmt(&self, _: $ty) -> &str {
                    match self {
                        Lang::En { singular: _, plural } => plural,
                    }
                }

            }
        )+
    }
}

impl_plural_for_languages_with_floats!(
    f32, test_impl_plural_for_languages_with_f32,
    f64, test_impl_plural_for_languages_with_f64,
);