#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum FontDisplay {
#[default]
Auto,
Block,
Swap,
Fallback,
Optional,
}
impl FontDisplay {
#[must_use]
pub const fn keyword(self) -> &'static str {
match self {
Self::Auto => "auto",
Self::Block => "block",
Self::Swap => "swap",
Self::Fallback => "fallback",
Self::Optional => "optional",
}
}
#[must_use]
pub fn from_keyword(keyword: &str) -> Option<Self> {
Some(match keyword {
"auto" => Self::Auto,
"block" => Self::Block,
"swap" => Self::Swap,
"fallback" => Self::Fallback,
"optional" => Self::Optional,
_ => return None,
})
}
pub(crate) const fn hash(
self,
h: topcoat_core::fnv1a::Fnv1a<u64>,
) -> topcoat_core::fnv1a::Fnv1a<u64> {
h.write(self.keyword().as_bytes())
}
}
impl std::fmt::Display for FontDisplay {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.keyword())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_is_auto() {
assert_eq!(FontDisplay::default(), FontDisplay::Auto);
}
#[test]
fn displays_as_its_keyword() {
assert_eq!(FontDisplay::Auto.to_string(), "auto");
assert_eq!(FontDisplay::Block.to_string(), "block");
assert_eq!(FontDisplay::Swap.to_string(), "swap");
assert_eq!(FontDisplay::Fallback.to_string(), "fallback");
assert_eq!(FontDisplay::Optional.to_string(), "optional");
}
#[test]
fn from_keyword_round_trips() {
for strategy in [
FontDisplay::Auto,
FontDisplay::Block,
FontDisplay::Swap,
FontDisplay::Fallback,
FontDisplay::Optional,
] {
assert_eq!(
FontDisplay::from_keyword(strategy.keyword()),
Some(strategy)
);
}
}
#[test]
fn from_keyword_rejects_unknown() {
assert_eq!(FontDisplay::from_keyword("infinite"), None);
}
}