termitype 0.0.11

Terminal-based typing test inspired by a certain typing test you might know.
Documentation
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use crate::{
    common::filesystem::{config_dir, create_file},
    constants::STATE_FILE,
    error::{AppError, AppResult},
};
use std::{
    collections::HashMap,
    fs::{self, File},
    io::{self, BufRead, BufWriter, Write},
    path::PathBuf,
};

// TODO: Evaluate when done with the settings to ensure we set a sane default here
const DEFAULT_CAPACITY: usize = 10;

#[derive(Debug, Default)]
pub struct Persistence {
    path: PathBuf,
    values: HashMap<String, String>,
    dirty: bool,
}

impl Clone for Persistence {
    fn clone(&self) -> Self {
        Self {
            path: self.path.clone(),
            values: self.values.clone(),
            dirty: self.dirty,
        }
    }
}

impl Persistence {
    /// Creates a new persistence instance for storing key-value data.
    ///
    /// This initializes the persistence layer, creating the config directory if needed,
    /// and loads existing state from disk if available.
    ///
    /// # Returns
    /// Returns an `AppResult<Self>` containing the new Persistence instance.
    ///
    /// # Errors
    /// Returns an error if the config directory cannot be created or accessed.
    pub fn new() -> AppResult<Self> {
        let config_dir = config_dir()?;
        let path = config_dir.join(STATE_FILE);

        if !config_dir.exists() {
            fs::create_dir_all(config_dir)?;
        }

        let mut persistence = Self {
            path: path.clone(),
            values: HashMap::with_capacity(DEFAULT_CAPACITY),
            dirty: false,
        };

        if path.exists() && persistence.load().is_err() {
            eprintln!("Failed to load state file");
        }

        Ok(persistence)
    }

    /// Gets a value from the persistent state.
    ///
    /// # Arguments
    /// * `key` - The key to look up
    ///
    /// # Returns
    /// Returns `Some(&str)` if the key exists, `None` otherwise.
    pub fn get(&self, key: &str) -> Option<&str> {
        self.values.get(key).map(String::as_str)
    }

    /// Gets a value from the persistent state, or returns the default if not found.
    ///
    /// # Arguments
    /// * `key` - The key to look up
    /// * `default` - The default value to return if key is not found
    ///
    /// # Returns
    /// Returns the value if found, otherwise the default.
    pub fn get_or<'a>(&'a self, key: &str, default: &'a str) -> &'a str {
        self.get(key).unwrap_or(default)
    }

    /// Deletes a key from the persistent state.
    ///
    /// Marks the state as dirty if the key existed. Call `flush()` to save to disk.
    ///
    /// # Arguments
    /// * `key` - The key to delete
    ///
    /// # Returns
    /// Returns `true` if the key existed and was removed, `false` otherwise.
    pub fn delete(&mut self, key: &str) -> bool {
        if self.values.remove(key).is_some() {
            self.dirty = true;
            true
        } else {
            false
        }
    }

    /// Returns true if there are unsaved changes.
    pub fn is_dirty(&self) -> bool {
        self.dirty
    }

    /// Returns the number of stored key-value pairs.
    pub fn len(&self) -> usize {
        self.values.len()
    }

    /// Returns true if no key-value pairs are stored.
    pub fn is_empty(&self) -> bool {
        self.values.is_empty()
    }

    /// Clears all stored key-value pairs.
    ///
    /// Marks the state as dirty. Call `flush()` to save to disk.
    pub fn clear(&mut self) {
        if !self.values.is_empty() {
            self.values.clear();
            self.dirty = true;
        }
    }

    /// Sets a value in the persistent state.
    ///
    /// Marks the state as dirty. Call `flush()` to save to disk.
    ///
    /// # Arguments
    /// * `key` - The key to set
    /// * `value` - The value to store
    ///
    /// # Returns
    /// Returns `Ok(())` on success.
    pub fn set(&mut self, key: &str, value: &str) -> AppResult<()> {
        if self.values.get(key).map(|v| v.as_str()) != Some(value) {
            self.values.insert(key.to_string(), value.to_string());
            self.dirty = true;
        }
        Ok(())
    }

    /// Loads state from disk.
    ///
    /// Parses the state file in key=value format, ignoring empty lines and comments.
    ///
    /// # Returns
    /// Returns `Ok(())` on success.
    ///
    /// # Errors
    /// Returns an error if the file cannot be read or contains invalid format.
    fn load(&mut self) -> AppResult<()> {
        let file = File::open(&self.path)?;
        let reader = io::BufReader::new(file);
        let mut values = HashMap::with_capacity(DEFAULT_CAPACITY);

        for (idx, line) in reader.lines().enumerate() {
            let line = line?;
            let line = line.trim();

            // skip empty lines and comments
            if line.is_empty() || line.starts_with('#') {
                continue;
            }

            if let Some((k, v)) = line.split_once('=') {
                let key = k.trim();
                let value = v.trim();
                values.insert(key.to_string(), value.to_string());
            } else {
                let err = format!("Invalid format at line {}: {line}", idx + 1);
                eprintln!("{err}");
                return Err(AppError::InvalidConfigData(err));
            }
        }
        self.values = values;
        self.dirty = false;

        Ok(())
    }

    /// Saves current state to disk.
    ///
    /// Uses atomic writes by saving to a temporary file first, then renaming.
    ///
    /// # Returns
    /// Returns `Ok(())` on success.
    ///
    /// # Errors
    /// Returns an error if writing or renaming fails.
    fn save(&mut self) -> AppResult<()> {
        let temp_path = self.path.with_extension("tmp");
        let file = create_file(&temp_path)?;
        let mut writer = BufWriter::new(file);

        for (k, v) in &self.values {
            writeln!(writer, "{k} = {v}")?;
        }

        writer.flush()?;

        fs::rename(temp_path, &self.path)?;
        self.dirty = false;

        Ok(())
    }

    /// Forces an immediate save to disk if there are unsaved changes.
    ///
    /// # Returns
    /// Returns `Ok(())` on success.
    ///
    /// # Errors
    /// Returns an error if saving fails.
    pub fn flush(&mut self) -> AppResult<()> {
        if self.dirty {
            self.save()?;
        }
        Ok(())
    }
}

pub fn reset_persistence() -> anyhow::Result<()> {
    let mut persistence = Persistence::new()?;
    persistence.clear();
    persistence.flush()?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::sync::Mutex;
    use tempfile::TempDir;

    // NOTE: this is to prevent concurrent ENV variables access causing tests to sometimes fail due to race conditions
    static ENV_MUTEX: Mutex<()> = Mutex::new(());

    struct EnvGuard {
        original_home: Option<String>,
        original_appdata: Option<String>,
        original_xdg: Option<String>,
    }

    impl Drop for EnvGuard {
        fn drop(&mut self) {
            // NOTE: Rust 2024 edition marks as unsafe `env::set_var()`, we only use this on tests so it's fine
            // https://github.com/rust-lang/rust/pull/124636
            unsafe {
                if let Some(ref h) = self.original_home {
                    std::env::set_var("HOME", h);
                }
                if let Some(ref a) = self.original_appdata {
                    std::env::set_var("APPDATA", a);
                }
                if let Some(ref x) = self.original_xdg {
                    std::env::set_var("XDG_CONFIG_HOME", x);
                }
            }
        }
    }

    fn init() -> (Persistence, TempDir, EnvGuard) {
        let _guard = ENV_MUTEX.lock().unwrap();

        let original_home = std::env::var("HOME").ok();
        let original_appdata = std::env::var("APPDATA").ok();
        let original_xdg = std::env::var("XDG_CONFIG_HOME").ok();

        let temp_dir = TempDir::new().unwrap();
        let temp_path = temp_dir.path().to_path_buf();

        let config_dir = if cfg!(target_os = "macos") {
            temp_path.join("Library/Application Support/termitype")
        } else if cfg!(target_os = "windows") {
            temp_path.join("AppData/Roaming/termitype")
        } else {
            temp_path.join(".config/termitype")
        };
        fs::create_dir_all(&config_dir).unwrap();

        let path = config_dir.join(".tmp.state");

        let persistence = Persistence {
            path,
            values: HashMap::with_capacity(DEFAULT_CAPACITY),
            dirty: false,
        };

        (
            persistence,
            temp_dir,
            EnvGuard {
                original_home,
                original_appdata,
                original_xdg,
            },
        )
    }

    #[test]
    fn test_config_dir_creation() {
        #[allow(unused_variables)]
        let (ps, tmp, guard) = init();
        assert!(ps.path.parent().unwrap().exists());
        assert!(ps.path.parent().unwrap().is_dir());
    }

    #[test]
    fn test_set_and_get() {
        #[allow(unused_variables)]
        let (mut ps, tmp, guard) = init();
        let config_json = r#"{"mode":"Words","language":"en","numbers":true}"#;
        ps.set("config", config_json).unwrap();
        ps.flush().unwrap();
        assert_eq!(ps.get("config"), Some(config_json))
    }

    #[test]
    fn test_set_and_get_json() {
        #[allow(unused_variables)]
        let (mut ps, tmp, guard) = init();
        let json_value = r#"{"mode":"Time","language":"en","numbers":false}"#;
        ps.set("config", json_value).unwrap();
        ps.flush().unwrap();
        assert_eq!(ps.get("config"), Some(json_value))
    }

    #[test]
    fn test_empty_lines() {
        #[allow(unused_variables)]
        let (mut ps, tmp, guard) = init();
        let config_json = r#"{"mode":"Time","language":"en"}"#;
        fs::write(&ps.path, format!("\n\n\nconfig = {}\n", config_json)).unwrap();

        ps.load().unwrap();
        assert_eq!(ps.values.len(), 1);
        assert_eq!(ps.get("config"), Some(config_json));
    }

    #[test]
    fn test_invalid_lines() {
        #[allow(unused_variables)]
        let (mut ps, tmp, guard) = init();
        let config_json = r#"{"mode":"Words","language":"en"}"#;
        fs::write(
            &ps.path,
            format!("\ninvalid line - random\nconfig = {}\n", config_json),
        )
        .unwrap();

        assert!(ps.load().is_err())
    }

    #[test]
    fn test_comment_lines() {
        #[allow(unused_variables)]
        let (mut ps, tmp, guard) = init();
        let config_json = r#"{"mode":"Time","language":"en"}"#;
        fs::write(
            &ps.path,
            format!("config = {}\n#other = ignored", config_json),
        )
        .unwrap();

        ps.load().unwrap();
        assert_eq!(ps.values.len(), 1);
        assert_eq!(ps.get("config"), Some(config_json));
    }

    #[test]
    fn test_save_and_load() {
        #[allow(unused_variables)]
        let (mut ps1, tmp, guard) = init();
        let config_json = r#"{"mode":"Time","language":"en","numbers":false,"symbols":true}"#;
        ps1.set("config", config_json).unwrap();
        ps1.set("other_key", "other_value").unwrap();
        ps1.flush().unwrap();

        let mut ps2 = Persistence {
            path: ps1.path.clone(),
            values: HashMap::with_capacity(DEFAULT_CAPACITY),
            dirty: false,
        };

        ps2.load().unwrap();

        assert_eq!(ps2.get("config"), Some(config_json));
        assert_eq!(ps2.get("other_key"), Some("other_value"));
    }

    #[test]
    fn test_get_or() {
        #[allow(unused_variables)]
        let (mut ps, tmp, guard) = init();
        ps.set("existing", "value").unwrap();
        assert_eq!(ps.get_or("existing", "default"), "value");
        assert_eq!(ps.get_or("nonexistent", "default"), "default");
    }

    #[test]
    fn test_delete() {
        #[allow(unused_variables)]
        let (mut ps, tmp, guard) = init();
        ps.set("key1", "value1").unwrap();
        ps.set("key2", "value2").unwrap();
        ps.flush().unwrap();

        assert!(ps.delete("key1"));
        assert!(ps.is_dirty());
        assert_eq!(ps.get("key1"), None);
        assert_eq!(ps.get("key2"), Some("value2"));

        assert!(!ps.delete("nonexistent"));
        assert_eq!(ps.len(), 1);
    }

    #[test]
    fn test_is_dirty_and_len() {
        #[allow(unused_variables)]
        let (mut ps, tmp, guard) = init();
        assert!(!ps.is_dirty());
        assert_eq!(ps.len(), 0);
        assert!(ps.is_empty());

        ps.set("key", "value").unwrap();
        assert!(ps.is_dirty());
        assert_eq!(ps.len(), 1);
        assert!(!ps.is_empty());

        ps.flush().unwrap();
        assert!(!ps.is_dirty());
    }

    #[test]
    fn test_flush_when_not_dirty() {
        #[allow(unused_variables)]
        let (mut ps, tmp, guard) = init();
        // should to nothing
        ps.flush().unwrap();
        assert!(!ps.is_dirty());
    }

    #[test]
    fn test_clear() {
        #[allow(unused_variables)]
        let (mut ps, tmp, guard) = init();
        ps.set("key1", "value1").unwrap();
        ps.set("key2", "value2").unwrap();
        assert_eq!(ps.len(), 2);

        ps.clear();
        assert!(ps.is_dirty());
        assert_eq!(ps.len(), 0);
        assert!(ps.is_empty());
        assert_eq!(ps.get("key1"), None);
    }
}