timewall 2.1.0

All-in-one tool for Apple dynamic HEIF wallpapers on GNU/Linux
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use std::{
    collections::HashSet,
    env, fs,
    path::{Path, PathBuf},
};

use anyhow::{Context, Result};
use directories::ProjectDirs;
use log::debug;
use serde::{de::DeserializeOwned, Serialize};

use crate::constants::{APP_NAME, APP_QUALIFIER};

/// Abstraction over a cache directory. Manges multiple cache subdirectories accessed by
/// a string key.
#[derive(Debug)]
pub struct Cache {
    base_dir: PathBuf,
    pub entries: HashSet<String>,
}

impl Cache {
    /// Find user's cache directory and and create or load a cache of given name in it.
    ///
    /// E.g. `Cache::find("wallpapers")` will create 'wallpapers' directory in timewall
    /// directory in user's main cache directory.
    pub fn find(name: &str) -> Self {
        Self::in_dir(get_cache_dir().join(name))
    }

    /// Load cache from a given directory. Create it if it doesn't exist.
    fn in_dir<P: AsRef<Path>>(path: P) -> Self {
        let path = path.as_ref();

        if !path.exists() {
            fs::create_dir_all(path).expect("couldn't create cache directory");
        }
        let entry_dirs = path
            .read_dir()
            .unwrap()
            .flatten()
            .filter(|e| e.file_type().unwrap().is_dir())
            .flat_map(|e| e.file_name().into_string())
            .collect();

        Self {
            base_dir: path.to_path_buf(),
            entries: entry_dirs,
        }
    }

    /// Get path to the dir for a given key. Create the dir if it doesn't exist.
    pub fn entry(&mut self, key: &String) -> PathBuf {
        if self.entries.contains(key) {
            self.get_entry(key)
        } else {
            self.add_entry(key)
        }
    }

    /// Remove the cache dir for a given key.
    pub fn remove_entry(&mut self, key: &str) {
        let entry_path = self.get_entry(key);
        if entry_path.is_dir() {
            fs::remove_dir_all(entry_path).expect("couldn't remove cache entry directory");
        }
        self.entries.remove(key);
    }

    /// Create cache dir for a given key. Panics if the dir already exists.
    fn add_entry(&mut self, key: &str) -> PathBuf {
        let entry_path = self.base_dir.join(key);
        fs::create_dir(&entry_path).expect("couldn't create cache entry directory");
        self.entries.insert(key.to_owned());
        entry_path
    }

    /// Construct path to cache dir for a given key. Does not check whether the dir exists or not!
    fn get_entry(&self, key: &str) -> PathBuf {
        self.base_dir.join(key)
    }
}

/// Abstraction over a symlink to the last used wallpaper.
pub struct LastWallpaper {
    link_path: PathBuf,
}

impl LastWallpaper {
    /// Find user's cache directory and load instance from there.
    pub fn find() -> Self {
        Self::load(get_cache_dir().join("last_wall"))
    }

    /// Load instance from given link path.
    fn load<P: AsRef<Path>>(link_path: P) -> Self {
        let link_path = link_path.as_ref();
        let parent_dir = link_path.parent().unwrap();

        if !parent_dir.exists() {
            fs::create_dir_all(parent_dir).expect("couldn't create cache directory");
        }

        Self {
            link_path: link_path.to_path_buf(),
        }
    }

    /// Save path to the last wallpaper.
    /// This may silently fail. We don't care because it's not a critical functionality.
    pub fn save<P: AsRef<Path>>(&self, path: P) {
        if fs::read_link(&self.link_path).is_ok() {
            fs::remove_file(&self.link_path).ok();
        }
        std::os::unix::fs::symlink(path.as_ref().canonicalize().unwrap(), &self.link_path).ok();
    }

    /// Get path to the last used wallpaper, if it exists.
    pub fn get(&self) -> Option<PathBuf> {
        if self.link_path.exists() {
            Some(fs::read_link(&self.link_path).unwrap())
        } else {
            None
        }
    }

    /// Remove the link to the last used wallpaper.
    pub fn clear(&self) {
        if fs::read_link(&self.link_path).is_ok() {
            fs::remove_file(&self.link_path).ok();
        }
    }
}

fn get_cache_dir() -> PathBuf {
    if let Result::Ok(path_str) = env::var("TIMEWALL_CACHE_DIR") {
        PathBuf::from(path_str)
    } else {
        match ProjectDirs::from(APP_QUALIFIER, "", APP_NAME) {
            Some(app_dirs) => app_dirs.cache_dir().to_path_buf(),
            None => panic!("couldn't determine user's home directory"),
        }
    }
}

/// Return value of a cached call.
#[derive(PartialEq, Eq, Debug)]
pub enum CachedCallRetval<T>
where
    T: Serialize + DeserializeOwned,
{
    /// The call was successful and the returned value is fresh.
    Fresh(T),
    /// The call failed and cached value is returned.
    Cached(T),
}

/// Abstraction over a cached call to a function.
pub struct CachedCall {
    cache_file_path: PathBuf,
}

impl CachedCall {
    pub fn find(name: &str) -> Self {
        Self::load(get_cache_dir().join(format!("{name}.json")))
    }

    fn load<P: AsRef<Path>>(path: P) -> Self {
        let path = path.as_ref();
        let parent_dir = path.parent().unwrap();

        if !parent_dir.exists() {
            fs::create_dir_all(parent_dir).expect("couldn't create cache directory");
        }

        Self {
            cache_file_path: path.to_path_buf(),
        }
    }

    /// Call the given function and cache the result in a file.
    ///
    /// If the call fails, return the cached result if it exists.
    pub fn call_with_fallback<F, R>(&self, func: F) -> Result<CachedCallRetval<R>>
    where
        F: FnOnce() -> Result<R>,
        R: Serialize + DeserializeOwned,
    {
        let call_result = func();

        match call_result {
            Ok(retval) => {
                let retval_serialized =
                    serde_json::to_string(&retval).context("failed to serialize value")?;
                fs::write(&self.cache_file_path, retval_serialized)
                    .context("failed to write to cache file")?;

                Ok(CachedCallRetval::Fresh(retval))
            }
            Err(e) if self.cache_file_path.exists() => {
                debug!("CachedCall function error: {e}, falling back to cache");

                let cached_retval_serialized = fs::read_to_string(&self.cache_file_path)
                    .context("failed to read the cached value file")?;
                let cached_retval = serde_json::from_str(&cached_retval_serialized)
                    .context("failed to deserialize cached value")?;

                Ok(CachedCallRetval::Cached(cached_retval))
            }
            Err(e) => Err(e).context("failed to call the function and no cache found"),
        }
    }
}

#[cfg(test)]
mod tests {
    use anyhow::anyhow;
    use anyhow::Ok;
    use assert_fs::prelude::*;
    use assert_fs::TempDir;
    use predicates::prelude::*;
    use rstest::*;
    use serde::Deserialize;

    use super::*;

    #[fixture]
    fn tmp_dir() -> TempDir {
        assert_fs::TempDir::new().unwrap()
    }

    #[rstest]
    fn test_cache_in_dir_not_exists(tmp_dir: TempDir) {
        let expected_dir = tmp_dir.child("random_dir");

        Cache::in_dir(&expected_dir);

        expected_dir.assert(predicate::path::is_dir());
    }

    #[rstest]
    fn test_cache_in_dir_exists(tmp_dir: TempDir) {
        let expected_entries = HashSet::from([String::from("first"), String::from("other")]);
        for entry in &expected_entries {
            tmp_dir.child(entry).create_dir_all().unwrap();
        }

        let cache = Cache::in_dir(&tmp_dir);

        assert_eq!(cache.entries, expected_entries);
    }

    #[rstest]
    fn test_cache_entry_not_exists(tmp_dir: TempDir) {
        let entry_name = String::from("random-entry");
        let expected_dir = tmp_dir.child(&entry_name);

        let mut cache = Cache::in_dir(&tmp_dir);

        assert_eq!(cache.entry(&entry_name), expected_dir.path());
        expected_dir.assert(predicate::path::is_dir());
    }

    #[rstest]
    fn test_cache_entry_exists(tmp_dir: TempDir) {
        let entry_name = String::from("some_entry");
        let expected_dir = tmp_dir.child(&entry_name);
        expected_dir.create_dir_all().unwrap();

        let mut cache = Cache::in_dir(&tmp_dir);

        assert_eq!(cache.entry(&entry_name), expected_dir.path());
        expected_dir.assert(predicate::path::is_dir());

        cache.remove_entry(&entry_name);
        expected_dir.assert(predicate::path::missing());
    }

    #[rstest]
    fn test_cache_entry_remove_exists(tmp_dir: TempDir) {
        let entry_name = String::from("some_entry");
        let expected_dir = tmp_dir.child(&entry_name);
        expected_dir.create_dir_all().unwrap();

        let mut cache = Cache::in_dir(&tmp_dir);

        expected_dir.assert(predicate::path::is_dir());
        cache.remove_entry(&entry_name);
        expected_dir.assert(predicate::path::missing());
    }

    #[rstest]
    fn test_cache_entry_remove_not_exists(tmp_dir: TempDir) {
        let entry_name = String::from("some_entry");
        let expected_dir = tmp_dir.child(&entry_name);

        let mut cache = Cache::in_dir(&tmp_dir);

        expected_dir.assert(predicate::path::missing());
        cache.remove_entry(&entry_name);
        expected_dir.assert(predicate::path::missing());
    }

    #[rstest]
    #[should_panic(expected = "couldn't create cache entry directory")]
    fn test_cache_entry_file_conflict(tmp_dir: TempDir) {
        let entry_name = String::from("some_entry");
        tmp_dir.child(&entry_name).touch().unwrap();

        Cache::in_dir(&tmp_dir).entry(&entry_name);
    }

    #[rstest]
    fn test_last_wallpaper_load_not_exists(tmp_dir: TempDir) {
        let fake_cache_dir = tmp_dir.child("cache_dir");
        let link_path = fake_cache_dir.child("test_link");

        LastWallpaper::load(&link_path);

        fake_cache_dir.assert(predicate::path::exists());
    }

    #[rstest]
    fn test_last_wallpaper_save_get(tmp_dir: TempDir) {
        let target_path_1 = tmp_dir.child("target.heic");
        let target_path_2 = tmp_dir.child("other_target.heic");
        let link_path = tmp_dir.child("test_link");
        target_path_1.touch().unwrap();
        target_path_2.touch().unwrap();

        let last_wall = LastWallpaper::load(&link_path);
        link_path.assert(predicate::path::missing());
        assert_eq!(last_wall.get(), None);

        last_wall.save(&target_path_1);
        assert_eq!(last_wall.get(), Some(target_path_1.to_path_buf()));

        fs::remove_file(target_path_1).unwrap();
        last_wall.save(&target_path_2);
        assert_eq!(last_wall.get(), Some(target_path_2.to_path_buf()));
    }

    #[rstest]
    fn test_last_wallpaper_save_clear(tmp_dir: TempDir) {
        let target_path = tmp_dir.child("target.heic");
        let link_path = tmp_dir.child("test_link");
        target_path.touch().unwrap();

        let last_wall = LastWallpaper::load(&link_path);

        last_wall.save(&target_path);
        link_path.assert(predicate::path::exists());

        last_wall.clear();
        link_path.assert(predicate::path::missing());
    }

    #[derive(Deserialize, Serialize, PartialEq, Eq, Debug, Clone, Copy)]
    pub struct TestStruct {
        pub a: i32,
        pub b: i32,
    }

    const TEST_STRUCT_1: TestStruct = TestStruct { a: 1, b: 2 };
    const TEST_STRUCT_2: TestStruct = TestStruct { a: 3, b: 4 };

    fn test_fun_err() -> Result<TestStruct> {
        Err(anyhow!("error"))
    }

    #[rstest]
    fn test_cached_call_ok(tmp_dir: TempDir) -> Result<()> {
        let cache_file = tmp_dir.child("cached_call_test");
        let cached_call = CachedCall::load(&cache_file);

        let result = cached_call.call_with_fallback(|| Ok(TEST_STRUCT_1))?;
        assert_eq!(result, CachedCallRetval::Fresh(TEST_STRUCT_1));

        let result = cached_call.call_with_fallback(|| Ok(TEST_STRUCT_2))?;
        assert_eq!(result, CachedCallRetval::Fresh(TEST_STRUCT_2));

        Ok(())
    }

    #[rstest]
    fn test_cached_call_err(tmp_dir: TempDir) {
        let cache_file = tmp_dir.child("cached_call_test");
        let cached_call = CachedCall::load(&cache_file);

        let result = cached_call.call_with_fallback(test_fun_err);
        assert!(result.is_err());
    }

    #[rstest]
    fn test_cached_call_fallback(tmp_dir: TempDir) -> Result<()> {
        let cache_file = tmp_dir.child("cached_call_test");
        let cached_call = CachedCall::load(&cache_file);

        let result = cached_call.call_with_fallback(|| Ok(TEST_STRUCT_1))?;
        assert_eq!(result, CachedCallRetval::Fresh(TEST_STRUCT_1));

        let result = cached_call.call_with_fallback(test_fun_err)?;
        assert_eq!(result, CachedCallRetval::Cached(TEST_STRUCT_1));

        Ok(())
    }
}