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
// License: see LICENSE file at root directory of `master` branch

//! # Either singular or plural

use crate::Plural;

/// # Either singular or plural
///
/// ## Examples
///
/// ```
/// use plurals::{Either, Plural};
///
/// const FOOT: Either = Either::new("foot", "feet");
///
/// assert_eq!(FOOT.fmt(0), "feet");
/// assert_eq!(FOOT.fmt(1), "foot");
/// assert_eq!(FOOT.fmt(2), "feet");
/// assert_eq!(FOOT.fmt(3), "feet");
/// ```
#[derive(Debug, Eq, PartialEq, Hash)]
pub struct Either<'a> {
    singular: &'a str,
    plural: &'a str,
}

impl<'a> Either<'a> {

    /// # Makes new instance
    pub const fn new(singular: &'a str, plural: &'a str) -> Self {
        Self {
            singular,
            plural,
        }
    }

    /// # Singular
    pub fn singular(&self) -> &str {
        self.singular
    }

    /// # Plural
    pub fn plural(&self) -> &str {
        self.plural
    }

}

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

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

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

            }
        )+
    }
}

impl_plural_for_unsigned_integers!(
    u8, test_impl_plural_for_u8,
    u16, test_impl_plural_for_u16,
    u32, test_impl_plural_for_u32,
    u64, test_impl_plural_for_u64,
    u128, test_impl_plural_for_u128,
    usize, test_impl_plural_for_usize,
);

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

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

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

            }
        )+
    }
}

impl_plural_for_signed_integers!(
    i8, test_impl_plural_for_i8,
    i16, test_impl_plural_for_i16,
    i32, test_impl_plural_for_i32,
    i64, test_impl_plural_for_i64,
    i128, test_impl_plural_for_i128,
    isize, test_impl_plural_for_isize,
);

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

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

                fn fmt(&self, _: $ty) -> &str {
                    self.plural
                }

            }
        )+
    }
}

impl_plural_for_floats!(
    f32, test_impl_plural_for_f32,
    f64, test_impl_plural_for_f64,
);