use std::{borrow::Cow, fmt};
pub(crate) struct SortCriteria<'c>(pub(crate) &'c [SortCriterion<'c>]);
impl<'c> fmt::Display for SortCriteria<'c> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.0.is_empty() {
write!(f, "")
} else {
let criteria: Vec<String> = self.0.iter().map(|c| c.to_string()).collect();
write!(f, "({})", criteria.join(" "))
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Copy)]
pub enum SortCriterion<'c> {
Arrival,
Cc,
Date,
From,
Reverse(&'c SortCriterion<'c>),
Size,
Subject,
To,
}
impl<'c> fmt::Display for SortCriterion<'c> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use SortCriterion::*;
match self {
Arrival => write!(f, "ARRIVAL"),
Cc => write!(f, "CC"),
Date => write!(f, "DATE"),
From => write!(f, "FROM"),
Reverse(c) => write!(f, "REVERSE {}", c),
Size => write!(f, "SIZE"),
Subject => write!(f, "SUBJECT"),
To => write!(f, "TO"),
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum SortCharset<'c> {
Utf8,
UsAscii,
Custom(Cow<'c, str>),
}
impl<'c> fmt::Display for SortCharset<'c> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use SortCharset::*;
match self {
Utf8 => write!(f, "UTF-8"),
UsAscii => write!(f, "US-ASCII"),
Custom(c) => write!(f, "{}", c),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_criterion_to_string() {
use SortCriterion::*;
assert_eq!("ARRIVAL", Arrival.to_string());
assert_eq!("CC", Cc.to_string());
assert_eq!("DATE", Date.to_string());
assert_eq!("FROM", From.to_string());
assert_eq!("SIZE", Size.to_string());
assert_eq!("SUBJECT", Subject.to_string());
assert_eq!("TO", To.to_string());
assert_eq!("REVERSE TO", Reverse(&To).to_string());
assert_eq!("REVERSE REVERSE TO", Reverse(&Reverse(&To)).to_string());
}
#[test]
fn test_criteria_to_string() {
use SortCriterion::*;
assert_eq!("", SortCriteria(&[]).to_string());
assert_eq!("(ARRIVAL)", SortCriteria(&[Arrival]).to_string());
assert_eq!(
"(ARRIVAL REVERSE FROM)",
SortCriteria(&[Arrival, Reverse(&From)]).to_string()
);
assert_eq!(
"(ARRIVAL REVERSE REVERSE REVERSE FROM)",
SortCriteria(&[Arrival, Reverse(&Reverse(&Reverse(&From)))]).to_string()
);
}
#[test]
fn test_charset_to_string() {
use SortCharset::*;
assert_eq!("UTF-8", Utf8.to_string());
assert_eq!("US-ASCII", UsAscii.to_string());
assert_eq!("CHARSET", Custom("CHARSET".into()).to_string());
}
}