icns-rs

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

commit a913cd634c6e21bec1406632f59b13a06a909172
parent 8a2c57c5a260d004d5508baab7a4202890137b99
Author: Jack Mordaunt <jackmordaunt@gmail.com>
Date:   Sat,  7 Mar 2020 16:20:10 +0800

Chore: Use more clear string -> bytes api.

Diffstat:
Msrc/encode.rs | 21++++-----------------
Msrc/os_type.rs | 33++++++++++++++++-----------------
2 files changed, 20 insertions(+), 34 deletions(-)

diff --git a/src/encode.rs b/src/encode.rs @@ -30,7 +30,7 @@ struct IconSet { } /// Magic bytes that denote an icns file. These bytes appear at index 0. -const ICONSET_MAGIC: [char; 4] = ['i', 'c', 'n', 's']; +const ICONSET_MAGIC: &'static str = "icns"; impl IconSet { /// Write the encoded iconset to writer `w`. @@ -41,12 +41,7 @@ impl IconSet { icon.write_to(&mut buffer)?; } // Write the 4-byte magic bytes to identify this as an icns image. - wr.write_all( - &ICONSET_MAGIC - .iter() - .map(|c| *c as u8) - .collect::<Vec<u8>>(), - )?; + wr.write_all(ICONSET_MAGIC.as_bytes())?; // Write the 4-byte container size in bytes. wr.write_u32::<BigEndian>((buffer.len() + 8) as u32)?; // Write the encoded icons. @@ -76,14 +71,7 @@ impl Icon { )?; // Write the 4-byte OSType identifier. // Coerce array of characters into slice of bytes through a vec deref. - wr.write_all( - &self - .kind - .header() - .iter() - .map(|c| *c as u8) - .collect::<Vec<u8>>(), - )?; + wr.write_all(&self.kind.header().as_bytes())?; // Write the 4-byte icon size in bytes (data.len + header.len). wr.write_u32::<BigEndian>((buffer.len() + 8) as u32)?; // Write the image data. @@ -137,4 +125,4 @@ impl From<&RgbaImage> for IconSet { .collect(); IconSet { icons } } -} -\ No newline at end of file +} diff --git a/src/os_type.rs b/src/os_type.rs @@ -1,7 +1,7 @@ /// OSType is an enum of various icon types that can exist inside an icns /// container. This enum only contains the high resolution variants that we care -/// about. Find the full list here: https://en.wikipedia.org/wiki/Apple_Icon_Image_format -#[derive(Clone)] +/// about. Find the full list here: https://en.wikipedia.org/wiki/Apple_Icon_Image_format +#[derive(Clone)] pub enum OSType { IC10, IC14, @@ -12,16 +12,16 @@ pub enum OSType { } impl OSType { - /// Returns the largest OSType for the given dimension. + /// Returns the largest OSType for the given dimension. pub fn nearest(d: u32) -> Self { for variant in OSType::variants() { - if d >= variant.size(){ + if d >= variant.size() { return variant; } } OSType::IC11 } - /// Get a list of all OSType variants. + /// Get a list of all OSType variants. pub fn variants() -> Vec<OSType> { vec![ OSType::IC10, @@ -32,7 +32,7 @@ impl OSType { OSType::IC11, ] } - /// Size in pixels. + /// Size in pixels. pub fn size(&self) -> u32 { match self { OSType::IC10 => 1024, @@ -43,18 +43,18 @@ impl OSType { OSType::IC11 => 32, } } - /// 4 byte header corresponding to the OSType. - pub fn header(&self) -> [char; 4] { + /// 4 byte header corresponding to the OSType. + pub fn header(&self) -> &'static str { match self { - OSType::IC10 => ['i', 'c', '1', '0'], - OSType::IC14 => ['i', 'c', '1', '4'], - OSType::IC13 => ['i', 'c', '1', '3'], - OSType::IC07 => ['i', 'c', '0', '7'], - OSType::IC12 => ['i', 'c', '1', '2'], - OSType::IC11 => ['i', 'c', '1', '1'], + OSType::IC10 => "ic10", + OSType::IC14 => "ic14", + OSType::IC13 => "ic13", + OSType::IC07 => "ic07", + OSType::IC12 => "ic12", + OSType::IC11 => "ic11", } } - /// Get a list of all variants equal to or smaller than the current one. + /// Get a list of all variants equal to or smaller than the current one. pub fn smaller_variants(&self) -> Vec<OSType> { let variants = OSType::variants(); for (ii, v) in variants.iter().enumerate() { @@ -64,4 +64,4 @@ impl OSType { } variants } -} -\ No newline at end of file +}