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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
/// Represents possible wallpaper types. #[derive(Debug, PartialEq, Clone, Copy)] pub enum WallpaperType { /// If the wallpaper's name is `tiled.*`, this variant is chosen. /// /// `Tiled` means that the image is placed at (0, 0) of the screen and then /// repeat along both axes until the screen is filled. Tiled, /// If the wallpaper's name is `background.*`, this variant is chosen. /// /// `Background` means that the image is placed at the center of the screen /// and is scaled down or up to the size of the screen so that it fills the /// entire screen. Background, } impl ToString for WallpaperType { /// Returns a string corresponding to `WallpaperType`'s value. /// /// # Possible return values /// /// Enum variant | String value /// ------------ | -------------- /// `Tiled` | `"tiled"` /// `Background` | `"background"` fn to_string(&self) -> String { match self { WallpaperType::Tiled => "tiled", WallpaperType::Background => "background", }.to_string() } } /// Represents possible wallpaper extensions. It is taken from the wallpaper's /// name. /// /// # Notes /// /// This crate does **not** check the real type of the wallpaper, it is simply /// taken from the theme's wallpaper filename. If you want to be sure that the /// wallpaper is either a .jpg or a .png file, do it yourself. #[derive(Debug, PartialEq, Clone, Copy)] pub enum WallpaperExtension { /// If the wallpaper's extension is `.png`, this variant is chosen. Png, /// If the wallpaper's extension is `.jpg`, this variant is chosen. Jpg, } impl ToString for WallpaperExtension { /// Returns a string corresponding to `WallpaperExtension`'s value. /// /// # Possible return values /// /// Enum variant | String value /// ------------ | ------------ /// `Png` | `"png"` /// `Jpg` | `"jpg"` /// /// # Examples /// /// ``` /// use tdesktop_theme::*; /// /// let wallpaper = Wallpaper { /// wallpaper_type: WallpaperType::Tiled, /// extension: WallpaperExtension::Jpg, /// bytes: b"Pretend it's a wallpaper".to_vec(), /// }; /// /// let name = format!( /// "examples/trash/wallpaper.{}", /// wallpaper.extension.to_string(), /// ); /// /// // std::fs::write(name, wallpaper.bytes).unwrap(); /// ``` fn to_string(&self) -> String { match self { WallpaperExtension::Png => "png", WallpaperExtension::Jpg => "jpg", }.to_string() } } /// Represents a theme's wallpaper. #[derive(Debug, PartialEq, Clone)] pub struct Wallpaper { /// Represents the type of the wallpaper, e.g. `background.*` or `tiled.*`. pub wallpaper_type: WallpaperType, /// Represents the extension of the wallpaper, e.g. `*.png` or `*.jpg`. pub extension: WallpaperExtension, /// Holds the wallpaper's original bytes. pub bytes: Vec<u8>, } impl Wallpaper { /// Generates the name of the wallpaper when the theme is zipped. /// /// # Possible return values /// /// `wallpaper.wallpaper_type` | `wallpaper.extension` | Return value /// -------------------------- | --------------------- | ------------------ /// `Tiled` | `Png` | `"tiled.png"` /// `Tiled` | `Jpg` | `"tiled.jpg"` /// `Background` | `Png` | `"background.png"` /// `Background` | `Jpg` | `"background.jpg"` /// /// # Examples /// /// ``` /// use tdesktop_theme::*; /// /// let wallpaper = Wallpaper { /// wallpaper_type: WallpaperType::Background, /// extension: WallpaperExtension::Jpg, /// bytes: Vec::new(), /// }; /// /// assert_eq!(wallpaper.get_filename(), "background.jpg"); /// ``` pub fn get_filename(&self) -> String { format!( "{name}.{extension}", name = self.wallpaper_type.to_string(), extension = self.extension.to_string(), ) } } #[cfg(test)] mod tests { use super::*; #[test] fn converts_type_to_string_correctly() { assert_eq!(WallpaperType::Tiled.to_string(), "tiled"); assert_eq!(WallpaperType::Background.to_string(), "background"); } #[test] fn converts_extension_to_string_correctly() { assert_eq!(WallpaperExtension::Png.to_string(), "png"); assert_eq!(WallpaperExtension::Jpg.to_string(), "jpg"); } #[test] fn generates_correct_filenames() { let mut wallpaper = Wallpaper { wallpaper_type: WallpaperType::Tiled, extension: WallpaperExtension::Png, bytes: Vec::new(), }; assert_eq!(wallpaper.get_filename(), "tiled.png"); wallpaper.wallpaper_type = WallpaperType::Background; assert_eq!(wallpaper.get_filename(), "background.png"); wallpaper.extension = WallpaperExtension::Jpg; assert_eq!(wallpaper.get_filename(), "background.jpg"); wallpaper.wallpaper_type = WallpaperType::Tiled; assert_eq!(wallpaper.get_filename(), "tiled.jpg"); } }