use std::fmt;
use std::str::FromStr;
use crate::Error;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Count {
Num(i64),
Word(String),
}
impl fmt::Display for Count {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Count::Num(n) => write!(f, "{n}"),
Count::Word(w) => f.write_str(w),
}
}
}
macro_rules! count_from_int {
($($t:ty),*) => {
$(impl From<$t> for Count {
fn from(n: $t) -> Self {
match i64::try_from(n) {
Ok(n) => Count::Num(n),
Err(_) => Count::Word(n.to_string()),
}
}
})*
};
}
count_from_int!(i8, i16, i32, i64, isize, u8, u16, u32, u64, usize);
impl From<&str> for Count {
fn from(s: &str) -> Self {
Count::Word(s.to_string())
}
}
impl From<String> for Count {
fn from(s: String) -> Self {
Count::Word(s)
}
}
impl From<&Count> for Count {
fn from(c: &Count) -> Self {
c.clone()
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Gender {
#[default]
Neuter,
Feminine,
Masculine,
GenderNeutral,
FeminineOrMasculine,
MasculineOrFeminine,
}
impl Gender {
pub const ALL: [Gender; 6] = [
Gender::Neuter,
Gender::Feminine,
Gender::Masculine,
Gender::GenderNeutral,
Gender::FeminineOrMasculine,
Gender::MasculineOrFeminine,
];
pub fn as_str(self) -> &'static str {
match self {
Gender::Neuter => "neuter",
Gender::Feminine => "feminine",
Gender::Masculine => "masculine",
Gender::GenderNeutral => "gender-neutral",
Gender::FeminineOrMasculine => "feminine or masculine",
Gender::MasculineOrFeminine => "masculine or feminine",
}
}
}
impl fmt::Display for Gender {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for Gender {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Gender::ALL
.into_iter()
.find(|g| g.as_str() == s)
.ok_or_else(|| Error::BadGender(s.to_string()))
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Classical {
pub all: bool,
pub zero: bool,
pub herd: bool,
pub names: bool,
pub persons: bool,
pub ancient: bool,
}
impl Classical {
pub const fn all() -> Self {
Classical {
all: true,
zero: true,
herd: true,
names: true,
persons: true,
ancient: true,
}
}
pub const fn none() -> Self {
Classical {
all: false,
zero: false,
herd: false,
names: false,
persons: false,
ancient: false,
}
}
}
impl Default for Classical {
fn default() -> Self {
Classical {
names: true,
..Classical::none()
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Comparison {
Eq,
PluralSingular,
SingularPlural,
PluralPlural,
}
impl Comparison {
pub fn as_str(self) -> &'static str {
match self {
Comparison::Eq => "eq",
Comparison::PluralSingular => "p:s",
Comparison::SingularPlural => "s:p",
Comparison::PluralPlural => "p:p",
}
}
}
impl fmt::Display for Comparison {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct NumberToWordsOptions {
pub group: u8,
pub comma: String,
pub andword: String,
pub zero: String,
pub one: String,
pub decimal: Option<String>,
pub threshold: Option<i64>,
}
impl Default for NumberToWordsOptions {
fn default() -> Self {
NumberToWordsOptions {
group: 0,
comma: ",".to_string(),
andword: "and".to_string(),
zero: "zero".to_string(),
one: "one".to_string(),
decimal: Some("point".to_string()),
threshold: None,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct JoinOptions {
pub sep: Option<String>,
pub sep_spaced: bool,
pub final_sep: Option<String>,
pub conj: String,
pub conj_spaced: bool,
}
impl Default for JoinOptions {
fn default() -> Self {
JoinOptions {
sep: None,
sep_spaced: true,
final_sep: None,
conj: "and".to_string(),
conj_spaced: true,
}
}
}