icns-rs

Easily create .icns files (Mac Icons) with this Rust library or the included CLI app.
Log | Files | Refs | LICENSE

os_type.rs (2352B)


      1 use std::str::FromStr;
      2 
      3 /// OSType is an enum of various icon types that can exist inside an icns
      4 /// container. This enum only contains the high resolution variants that we care
      5 /// about. Find the full list here: https://en.wikipedia.org/wiki/Apple_Icon_Image_format
      6 #[derive(Clone)]
      7 pub enum OSType {
      8     IC10,
      9     IC14,
     10     IC13,
     11     IC07,
     12     IC12,
     13     IC11,
     14 }
     15 
     16 impl OSType {
     17     /// Returns the largest OSType for the given dimension.
     18     pub fn nearest(d: u32) -> Self {
     19         for variant in OSType::variants() {
     20             if d >= variant.size() {
     21                 return variant;
     22             }
     23         }
     24         OSType::IC11
     25     }
     26     /// Get a list of all OSType variants.
     27     pub fn variants() -> Vec<OSType> {
     28         vec![
     29             OSType::IC10,
     30             OSType::IC14,
     31             OSType::IC13,
     32             OSType::IC07,
     33             OSType::IC12,
     34             OSType::IC11,
     35         ]
     36     }
     37     /// Size in pixels.
     38     pub fn size(&self) -> u32 {
     39         match self {
     40             OSType::IC10 => 1024,
     41             OSType::IC14 => 512,
     42             OSType::IC13 => 256,
     43             OSType::IC07 => 128,
     44             OSType::IC12 => 64,
     45             OSType::IC11 => 32,
     46         }
     47     }
     48     /// 4 byte header corresponding to the OSType.
     49     pub fn header(&self) -> &'static str {
     50         match self {
     51             OSType::IC10 => "ic10",
     52             OSType::IC14 => "ic14",
     53             OSType::IC13 => "ic13",
     54             OSType::IC07 => "ic07",
     55             OSType::IC12 => "ic12",
     56             OSType::IC11 => "ic11",
     57         }
     58     }
     59     /// Get a list of all variants equal to or smaller than the current one.
     60     pub fn smaller_variants(&self) -> Vec<OSType> {
     61         let variants = OSType::variants();
     62         for (ii, v) in variants.iter().enumerate() {
     63             if v.size() <= self.size() {
     64                 return variants[ii..].to_vec();
     65             }
     66         }
     67         variants
     68     }
     69 }
     70 
     71 impl FromStr for OSType {
     72     type Err = String;
     73     fn from_str(s: &str) -> Result<Self, Self::Err> {
     74         match s {
     75             "ic10" => Ok(OSType::IC10),
     76             "ic14" => Ok(OSType::IC14),
     77             "ic13" => Ok(OSType::IC13),
     78             "ic07" => Ok(OSType::IC07),
     79             "ic12" => Ok(OSType::IC12),
     80             "ic11" => Ok(OSType::IC11),
     81             _ => Err(format!("{} is not an icns OSType", s)),
     82         }
     83     }
     84 }