#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Presentation {
pub title: Option<String>,
pub description: Option<String>,
pub icon: Option<Icon>,
pub tint: Option<Tint>,
}
impl Presentation {
pub fn title(mut self, title: impl Into<String>) -> Self {
self.title = Some(title.into());
self
}
pub fn title_opt(mut self, title: Option<impl Into<String>>) -> Self {
self.title = title.map(Into::into);
self
}
pub fn description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
pub fn description_opt(mut self, description: Option<impl Into<String>>) -> Self {
self.description = description.map(Into::into);
self
}
pub fn icon(mut self, icon: impl Into<Option<Icon>>) -> Self {
self.icon = icon.into();
self
}
pub fn tint(mut self, tint: impl Into<Option<Tint>>) -> Self {
self.tint = tint.into();
self
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Icon {
Link,
Enum,
Toggle,
Lock,
Globe,
Clock,
Tag,
Text,
Other(String),
}
impl Icon {
pub fn from_name(name: &str) -> Icon {
match name {
"link" => Icon::Link,
"enum" => Icon::Enum,
"toggle" => Icon::Toggle,
"lock" => Icon::Lock,
"globe" => Icon::Globe,
"clock" => Icon::Clock,
"tag" => Icon::Tag,
"text" => Icon::Text,
other => Icon::Other(other.to_owned()),
}
}
pub fn name(&self) -> &str {
match self {
Icon::Link => "link",
Icon::Enum => "enum",
Icon::Toggle => "toggle",
Icon::Lock => "lock",
Icon::Globe => "globe",
Icon::Clock => "clock",
Icon::Tag => "tag",
Icon::Text => "text",
Icon::Other(name) => name,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Tint {
Accent,
Neutral,
Positive,
Warning,
Danger,
}
impl Tint {
pub const ALL: &'static [Tint] = &[
Tint::Accent,
Tint::Neutral,
Tint::Positive,
Tint::Warning,
Tint::Danger,
];
pub fn from_name(name: &str) -> Option<Tint> {
Tint::ALL.iter().copied().find(|tint| tint.name() == name)
}
pub fn name(self) -> &'static str {
match self {
Tint::Accent => "accent",
Tint::Neutral => "neutral",
Tint::Positive => "positive",
Tint::Warning => "warning",
Tint::Danger => "danger",
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_tint_and_icon_round_trips_through_its_name() {
for tint in Tint::ALL {
assert_eq!(Tint::from_name(tint.name()), Some(*tint));
}
assert_eq!(Tint::from_name("Positive"), None);
for icon in [
Icon::Link,
Icon::Enum,
Icon::Toggle,
Icon::Lock,
Icon::Globe,
Icon::Clock,
Icon::Tag,
Icon::Text,
Icon::Other("sparkles".into()),
] {
assert_eq!(Icon::from_name(icon.name()), icon);
}
}
}