This repository has been archived on 2025-01-30. You can view files and clone it, but cannot push or open issues or pull requests.
site_icons/src/icon.rs

60 lines
1.2 KiB
Rust
Raw Normal View History

2021-01-29 12:23:15 +00:00
use super::IconInfo;
use serde::Serialize;
use std::{
cmp::Ordering,
fmt::{self, Display},
2021-02-02 17:30:35 +00:00
str::FromStr,
2021-01-29 12:23:15 +00:00
};
use url::Url;
2021-02-02 17:37:12 +00:00
#[derive(Debug, Clone, PartialOrd, PartialEq, Ord, Eq)]
2021-01-29 12:23:15 +00:00
pub enum IconKind {
SiteLogo,
SiteFavicon,
AppIcon,
}
impl Display for IconKind {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
f.write_str(match self {
IconKind::SiteLogo => "site_logo",
IconKind::AppIcon => "app_icon",
IconKind::SiteFavicon => "site_favicon",
})
}
}
2021-02-02 17:30:35 +00:00
impl FromStr for IconKind {
type Err = String;
fn from_str(kind: &str) -> Result<Self, Self::Err> {
match kind {
"site_logo" => Ok(IconKind::SiteLogo),
"app_icon" => Ok(IconKind::AppIcon),
"site_favicon" => Ok(IconKind::SiteFavicon),
_ => Err("unknown icon kind!".into()),
}
}
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
2021-01-29 12:23:15 +00:00
pub struct Icon {
pub url: Url,
#[serde(with = "serde_with::rust::display_fromstr")]
pub kind: IconKind,
#[serde(flatten)]
pub info: IconInfo,
}
impl Ord for Icon {
fn cmp(&self, other: &Self) -> Ordering {
self.info.cmp(&other.info)
}
}
impl PartialOrd for Icon {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}