| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
use std::path::{Path, PathBuf}; |
| 14 |
|
| 15 |
|
| 16 |
#[allow(unused_imports)] |
| 17 |
use crate::{derive_tonal_steps, list_themes_from_dirs, load_theme}; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#[derive(Debug, Default, Clone)] |
| 28 |
pub struct ThemeDirs { |
| 29 |
bundled: Vec<PathBuf>, |
| 30 |
system: Vec<PathBuf>, |
| 31 |
custom: Option<PathBuf>, |
| 32 |
} |
| 33 |
|
| 34 |
impl ThemeDirs { |
| 35 |
#[must_use] |
| 36 |
pub fn new() -> Self { |
| 37 |
Self::default() |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
#[must_use] |
| 46 |
pub fn bundled(mut self, dir: Option<PathBuf>) -> Self { |
| 47 |
self.bundled.extend(dir); |
| 48 |
self |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
#[must_use] |
| 53 |
pub fn system(mut self, dir: Option<PathBuf>) -> Self { |
| 54 |
self.system.extend(dir); |
| 55 |
self |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
#[must_use] |
| 61 |
pub fn custom(mut self, dir: Option<PathBuf>) -> Self { |
| 62 |
self.custom = dir; |
| 63 |
self |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
#[must_use] |
| 68 |
pub fn build(self) -> Vec<(PathBuf, bool)> { |
| 69 |
let mut dirs = Vec::new(); |
| 70 |
for dir in self.bundled.into_iter().chain(self.system) { |
| 71 |
if dir.is_dir() { |
| 72 |
dirs.push((dir, false)); |
| 73 |
} |
| 74 |
} |
| 75 |
if let Some(dir) = self.custom |
| 76 |
&& dir.is_dir() |
| 77 |
{ |
| 78 |
dirs.push((dir, true)); |
| 79 |
} |
| 80 |
dirs |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
pub fn find_theme_path(dirs: &[(PathBuf, bool)], id: &str) -> Option<(PathBuf, bool)> { |
| 89 |
let filename = format!("{id}.toml"); |
| 90 |
|
| 91 |
for (dir, is_custom) in dirs.iter().rev() { |
| 92 |
let path = dir.join(&filename); |
| 93 |
if path.is_file() { |
| 94 |
return Some((path, *is_custom)); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
None |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
static EMBEDDED: include_dir::Dir<'static> = |
| 107 |
include_dir::include_dir!("$CARGO_MANIFEST_DIR/themes"); |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
pub fn embedded_themes() -> impl Iterator<Item = (&'static str, &'static str)> { |
| 121 |
EMBEDDED.files().filter_map(|file| { |
| 122 |
let path = file.path(); |
| 123 |
if path.extension().and_then(|e| e.to_str()) != Some("toml") { |
| 124 |
return None; |
| 125 |
} |
| 126 |
let id = path.file_stem()?.to_str()?; |
| 127 |
Some((id, file.contents_utf8()?)) |
| 128 |
}) |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
pub fn bundled_themes_dir() -> Option<PathBuf> { |
| 144 |
let themes = Path::new(env!("CARGO_MANIFEST_DIR")).join("themes"); |
| 145 |
if themes.is_dir() { Some(themes) } else { None } |
| 146 |
} |
| 147 |
|
| 148 |
#[cfg(test)] |
| 149 |
mod tests { |
| 150 |
use super::*; |
| 151 |
use crate::parse_theme_str; |
| 152 |
use std::fs; |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
#[test] |
| 159 |
fn the_users_own_themes_outrank_everything() { |
| 160 |
let root = tempfile::tempdir().unwrap(); |
| 161 |
let make = |name: &str| { |
| 162 |
let dir = root.path().join(name); |
| 163 |
std::fs::create_dir_all(&dir).unwrap(); |
| 164 |
dir |
| 165 |
}; |
| 166 |
let (bundled, system, custom) = (make("bundled"), make("system"), make("custom")); |
| 167 |
|
| 168 |
let dirs = ThemeDirs::new() |
| 169 |
.custom(Some(custom.clone())) |
| 170 |
.bundled(Some(bundled.clone())) |
| 171 |
.system(Some(system.clone())) |
| 172 |
.build(); |
| 173 |
|
| 174 |
assert_eq!( |
| 175 |
dirs, |
| 176 |
vec![(bundled, false), (system, false), (custom.clone(), true)], |
| 177 |
"lowest precedence first, whatever order the tiers were added in", |
| 178 |
); |
| 179 |
assert!(dirs.last().unwrap().1, "only the user's tier is custom"); |
| 180 |
|
| 181 |
|
| 182 |
for dir in dirs.iter().map(|(dir, _)| dir) { |
| 183 |
std::fs::write(dir.join("shared.toml"), "[meta]\nname = \"x\"\n").unwrap(); |
| 184 |
} |
| 185 |
assert_eq!( |
| 186 |
find_theme_path(&dirs, "shared").unwrap().0, |
| 187 |
custom.join("shared.toml"), |
| 188 |
"the user's copy is the one that loads", |
| 189 |
); |
| 190 |
} |
| 191 |
|
| 192 |
#[test] |
| 193 |
fn a_directory_that_does_not_exist_is_dropped() { |
| 194 |
let root = tempfile::tempdir().unwrap(); |
| 195 |
let real = root.path().join("real"); |
| 196 |
std::fs::create_dir_all(&real).unwrap(); |
| 197 |
|
| 198 |
let dirs = ThemeDirs::new() |
| 199 |
.bundled(Some(root.path().join("nope"))) |
| 200 |
.system(None) |
| 201 |
.custom(Some(real.clone())) |
| 202 |
.build(); |
| 203 |
|
| 204 |
assert_eq!(dirs, vec![(real, true)]); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
#[test] |
| 210 |
fn more_than_one_bundled_tier_is_allowed() { |
| 211 |
let root = tempfile::tempdir().unwrap(); |
| 212 |
let (first, second) = (root.path().join("a"), root.path().join("b")); |
| 213 |
std::fs::create_dir_all(&first).unwrap(); |
| 214 |
std::fs::create_dir_all(&second).unwrap(); |
| 215 |
|
| 216 |
let dirs = ThemeDirs::new() |
| 217 |
.bundled(Some(first.clone())) |
| 218 |
.bundled(Some(second.clone())) |
| 219 |
.build(); |
| 220 |
assert_eq!(dirs, vec![(first, false), (second, false)]); |
| 221 |
} |
| 222 |
|
| 223 |
#[test] |
| 224 |
fn find_theme_path_reverse_priority() { |
| 225 |
let d1 = tempfile::tempdir().unwrap(); |
| 226 |
let d2 = tempfile::tempdir().unwrap(); |
| 227 |
fs::write(d1.path().join("s.toml"), "[meta]\n").unwrap(); |
| 228 |
fs::write(d2.path().join("s.toml"), "[meta]\n").unwrap(); |
| 229 |
let dirs = vec![ |
| 230 |
(d1.path().to_path_buf(), false), |
| 231 |
(d2.path().to_path_buf(), true), |
| 232 |
]; |
| 233 |
let (path, is_custom) = find_theme_path(&dirs, "s").unwrap(); |
| 234 |
assert!(is_custom); |
| 235 |
assert_eq!(path, d2.path().join("s.toml")); |
| 236 |
} |
| 237 |
|
| 238 |
#[test] |
| 239 |
fn bundled_themes_dir_resolves_to_shipped_themes() { |
| 240 |
|
| 241 |
|
| 242 |
let dir = bundled_themes_dir().expect("makeover ships a themes/ directory"); |
| 243 |
assert!(dir.join("akari-dawn.toml").is_file()); |
| 244 |
assert!(dir.join("akari-night.toml").is_file()); |
| 245 |
} |
| 246 |
|
| 247 |
#[test] |
| 248 |
fn every_theme_is_accounted_for_in_third_party_notices() { |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
let notices = std::fs::read_to_string( |
| 253 |
Path::new(env!("CARGO_MANIFEST_DIR")).join("THIRD-PARTY-NOTICES.md"), |
| 254 |
) |
| 255 |
.expect("THIRD-PARTY-NOTICES.md must exist"); |
| 256 |
let missing: Vec<&str> = embedded_themes() |
| 257 |
.map(|(id, _)| id) |
| 258 |
.filter(|id| !notices.contains(*id)) |
| 259 |
.collect(); |
| 260 |
assert!( |
| 261 |
missing.is_empty(), |
| 262 |
"themes missing from THIRD-PARTY-NOTICES.md: {missing:?}" |
| 263 |
); |
| 264 |
} |
| 265 |
|
| 266 |
#[test] |
| 267 |
fn adapted_themes_carry_inline_attribution() { |
| 268 |
|
| 269 |
|
| 270 |
const ORIGINALS: [&str; 5] = [ |
| 271 |
"makenotwork", |
| 272 |
"goingson", |
| 273 |
"audiofiles", |
| 274 |
"high-contrast", |
| 275 |
"neobrute", |
| 276 |
]; |
| 277 |
for (id, source) in embedded_themes() { |
| 278 |
if ORIGINALS.contains(&id) { |
| 279 |
continue; |
| 280 |
} |
| 281 |
assert!( |
| 282 |
source.contains("adapted from"), |
| 283 |
"adapted theme `{id}` is missing its inline attribution header" |
| 284 |
); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
#[test] |
| 289 |
fn embedded_themes_match_the_directory() { |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
let dir = bundled_themes_dir().unwrap(); |
| 295 |
let mut on_disk: Vec<String> = std::fs::read_dir(&dir) |
| 296 |
.unwrap() |
| 297 |
.filter_map(|e| { |
| 298 |
let path = e.ok()?.path(); |
| 299 |
if path.extension()? != "toml" { |
| 300 |
return None; |
| 301 |
} |
| 302 |
Some(path.file_stem()?.to_str()?.to_string()) |
| 303 |
}) |
| 304 |
.collect(); |
| 305 |
let mut embedded: Vec<String> = embedded_themes().map(|(id, _)| id.to_string()).collect(); |
| 306 |
on_disk.sort(); |
| 307 |
embedded.sort(); |
| 308 |
assert_eq!(embedded, on_disk, "embedded theme set drifted from themes/"); |
| 309 |
} |
| 310 |
|
| 311 |
#[test] |
| 312 |
fn every_embedded_theme_parses() { |
| 313 |
|
| 314 |
|
| 315 |
let mut count = 0; |
| 316 |
for (id, source) in embedded_themes() { |
| 317 |
parse_theme_str(id, source, false) |
| 318 |
.unwrap_or_else(|e| panic!("embedded theme `{id}` failed to parse: {e}")); |
| 319 |
count += 1; |
| 320 |
} |
| 321 |
assert!(count >= 30, "expected the full theme set, got {count}"); |
| 322 |
} |
| 323 |
} |
| 324 |
|