1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use super::IconInfo;
use serde::Serialize;
use std::{
  cmp::Ordering,
  fmt::{self, Display},
  str::FromStr,
};
use url::Url;

#[derive(Debug, Clone, PartialOrd, PartialEq, Ord, Eq)]
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",
    })
  }
}

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)]
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))
  }
}