rust-todo-cli 0.3.1

A CLI tool for managing to-do lists
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
//! # storage模块
//!
//! 负责读取和保存数据:
//! - 包括主文件和备份文件
//! - 若主文件损坏会尝试读取备份文件
//! - 通过文件锁防止发生并发的读写冲突
use crate::error::{AppError, io_err};
use crate::task::Task;
use std::fs::File;
use std::io::{ErrorKind, Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};

/// 文件来源枚举,用于标记读取任务列表的来源
/// - Main 主文件
/// - Backup 备份文件
#[derive(Debug, PartialEq)]
enum Origin {
    Main,
    Backup,
}

/// 核心tasks结构体,负责保存文件地址,实现公开的读写方法
///
/// 包括字段:
/// - main 主文件地址
/// - backup 备份文件地址
pub struct TaskStore {
    main: PathBuf,
    backup: PathBuf,
}

impl TaskStore {
    /// 用于初始化创建TaskStore实例
    /// ### Args:
    /// custom: `Option<String>` 用户自定义文件保存地址
    pub fn new(custom: Option<String>) -> Self {
        let main = custom.map(PathBuf::from).unwrap_or_else(Self::default_main);
        let mut backup = main.clone().into_os_string();
        backup.push(".bak");
        Self {
            main,
            backup: PathBuf::from(backup),
        }
    }

    /// 在未传入自定义地址时,利用data_dir生成默认地址,供new使用
    fn default_main() -> PathBuf {
        dirs::data_dir()
            .unwrap_or_else(|| PathBuf::from("."))
            .join("rstodo")
            .join("task.json")
    }

    /// 返回主文件地址
    pub fn main_path(&self) -> &Path {
        &self.main
    }

    /// 返回备份文件地址
    pub fn backup_path(&self) -> &Path {
        &self.backup
    }

    /// Tasks只读方法
    /// 逻辑:
    /// 1.若主文件正常,只读主文件
    /// 2.若主文件不存在,走load_backup_fallback函数尝试读取备份文件
    /// 3.若主文件为空文件,走load_backup_fallback函数尝试读取备份文件
    /// 4.若主文件损坏,走load_backup_fallback函数尝试读取备份文件,若备份文件为空或损坏,报错而不覆盖
    pub fn load(&self) -> Result<Vec<Task>, AppError> {
        let file = match open_read(self.main_path()) {
            Ok(file) => file,
            Err(err) if is_not_found(&err) => {
                return load_backup_fallback(self.backup_path());
            }
            Err(e) => return Err(e),
        };
        lock_share(&file, self.main_path())?;
        match read_task_from(&file, self.main_path()) {
            Ok(Some(tasks)) => Ok(tasks),
            Ok(None) => load_backup_fallback(self.backup_path()),
            Err(AppError::Corrupted { path, source }) => {
                match load_backup_fallback(self.backup_path()) {
                    Ok(tasks) if !tasks.is_empty() => Ok(tasks),
                    _ => Err(AppError::Corrupted { path, source }),
                }
            }
            Err(e) => Err(e),
        }
    }

    /// Tasks更新方法
    /// 逻辑:
    /// 1.使用load_for_update函数读取(来源,任务列表)
    /// 2.若读取的是主文件,将主文件备份到副文件,之后再将操作覆盖到主文件
    pub fn update(
        &self,
        f: impl FnOnce(&mut Vec<Task>) -> Result<(), AppError>,
    ) -> Result<(), AppError> {
        create_dir(self.main_path())?;

        let main = open_read_write(self.main_path())?;
        let backup = open_read_write(self.backup_path())?;

        lock_private(&main, self.main_path())?;
        lock_private(&backup, self.backup_path())?;

        let (origin, mut tasks) =
            load_for_update(&main, self.main_path(), &backup, self.backup_path())?;
        f(&mut tasks)?;
        if origin == Origin::Main
            && let Err(err) =
                copy_main_to_backup(&main, self.main_path(), &backup, self.backup_path())
        {
            eprintln!(">_< Warning: backup failed: {}", err);
        };
        overwrite(
            &main,
            serialize_tasks(&tasks, self.main_path())?.as_bytes(),
            self.main_path(),
        )?;
        Ok(())
    }

    /// 只读备份文件方法
    /// 调用load_backup_strict只读备份文件
    pub fn load_backup(&self) -> Result<Vec<Task>, AppError> {
        load_backup_strict(self.backup_path())
    }

    /// 提取备份文件并覆盖主文件
    /// 将备份文件提取,并通过restore_main_from_backup覆盖到主文件,以实现undo
    pub fn restore_backup(&self) -> Result<(), AppError> {
        create_dir(self.main_path())?;
        let main = open_read_write(self.main_path())?;
        let backup = open_read_write(self.backup_path())?;
        lock_private(&main, self.main_path())?;
        lock_private(&backup, self.backup_path())?;
        restore_main_from_backup(&main, self.main_path(), &backup, self.backup_path())
    }
}

// tier1
/// 将读取的字符串切片使用serde_json序列化为`Vec<Task>`
fn parse_tasks(text: &str, path: &Path) -> Result<Option<Vec<Task>>, AppError> {
    if text.trim().is_empty() {
        return Ok(None);
    }
    let tasks = serde_json::from_str(text).map_err(|err| AppError::Corrupted {
        path: path.to_string_lossy().to_string(),
        source: err,
    })?;
    Ok(Some(tasks))
}

/// 将`&[Task]`的数组使用serde_json格式化为用于保存的`String`
fn serialize_tasks(tasks: &[Task], path: &Path) -> Result<String, AppError> {
    let data =
        serde_json::to_string_pretty(tasks).map_err(|serde_json_err| AppError::Corrupted {
            path: path.to_string_lossy().to_string(),
            source: serde_json_err,
        })?;
    Ok(data)
}

// tier2
/// 捕获NotFound(未找到需要打开的文件)错误,用于后续特殊处理
fn is_not_found(err: &AppError) -> bool {
    matches!(
        err,
        AppError::Io {
            operation: _,
            path: _,
            source
        } if source.kind() == ErrorKind::NotFound
    )
}

/// 从备份中恢复数据的警告信息
fn warn_recovered_from_backup(backup_path: &Path) {
    eprintln!(
        ">_< Warning: main task file is missing or corrupted, loaded data from backup file '{}'. Changes since the last successful save may be lost.",
        backup_path.display()
    )
}

/// 根据地址,创建需要的文件夹
fn create_dir(path: &Path) -> Result<(), AppError> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent).map_err(|err| io_err("create dir", path, err))?;
    };
    Ok(())
}

/// 传入文件`&Path`,打开并返回只读的文件句柄
fn open_read(path: &Path) -> Result<File, AppError> {
    let file = File::options()
        .read(true)
        .write(false)
        .create(false)
        .truncate(false)
        .open(path)
        .map_err(|err| io_err("open read", path, err))?;
    Ok(file)
}

/// 传入文件`&Path`,打开并返回可读写的文件句柄
fn open_read_write(path: &Path) -> Result<File, AppError> {
    let file = File::options()
        .read(true)
        .write(true)
        .create(true)
        .truncate(false)
        .open(path)
        .map_err(|err| io_err("open read write", path, err))?;
    Ok(file)
}

/// 将传入的`&File`句柄使用lock锁定,用于读写修改
fn lock_private(file: &File, path: &Path) -> Result<(), AppError> {
    file.lock()
        .map_err(|err| io_err("lock private", path, err))?;
    Ok(())
}

/// 将传入的`&File`句柄使用lock_shared锁定,用于只读项目
fn lock_share(file: &File, path: &Path) -> Result<(), AppError> {
    file.lock_shared()
        .map_err(|err| io_err("lock shared", path, err))?;
    Ok(())
}

/// 读取传入的`&File`句柄中的文件内容,返回文件全文的`String`字符串,用于序列化读取
fn read_string(file: &File, path: &Path) -> Result<String, AppError> {
    let mut f = file
        .try_clone()
        .map_err(|err| io_err("try clone", path, err))?;
    f.seek(SeekFrom::Start(0))
        .map_err(|err| io_err("seek to start", path, err))?;
    let mut text = String::new();
    f.read_to_string(&mut text)
        .map_err(|err| io_err("read to string", path, err))?;
    Ok(text)
}

/// 读取传入的`&File`句柄中的文件内容,返回文件全文的`Vec<u8>`字符比特,用于覆写其他文件
fn read_bytes(file: &File, path: &Path) -> Result<Vec<u8>, AppError> {
    let mut f = file
        .try_clone()
        .map_err(|err| io_err("try clone", path, err))?;
    f.seek(SeekFrom::Start(0))
        .map_err(|err| io_err("seek to start", path, err))?;
    let mut data: Vec<u8> = Vec::new();
    f.read_to_end(&mut data)
        .map_err(|err| io_err("read to end", path, err))?;
    Ok(data)
}

/// 使用传入的`&[u8]`字符比特,覆写到`&File`传入的文件中
fn overwrite(file: &File, data: &[u8], path: &Path) -> Result<(), AppError> {
    let mut f = file
        .try_clone()
        .map_err(|err| io_err("try clone", path, err))?;
    f.seek(SeekFrom::Start(0))
        .map_err(|err| io_err("seek to start", path, err))?;
    f.set_len(0).map_err(|err| io_err("set len 0", path, err))?;
    f.write_all(data)
        .map_err(|err| io_err("write all", path, err))?;
    f.flush().map_err(|err| io_err("flush", path, err))?;
    Ok(())
}

// tier3
/// 使用read_string读取文件的内容,并通过parse_tasks序列化为`Option<Vec<Task>>`返回
fn read_task_from(file: &File, path: &Path) -> Result<Option<Vec<Task>>, AppError> {
    let text = read_string(file, path)?;
    let tasks = parse_tasks(&text, path)?;
    Ok(tasks)
}

/// 将主文件通过read_bytes读取为字节比特,利用overwrite覆写入备份文件
fn copy_main_to_backup(
    main: &File,
    main_path: &Path,
    backup: &File,
    backup_path: &Path,
) -> Result<(), AppError> {
    let data = read_bytes(main, main_path)?;
    overwrite(backup, &data, backup_path)
}

// tier4
/// 从主文件或备份中读取任务列表,返回文件来源和任务列表
///
/// 逻辑:
/// - 若主文件正常,返回(Main, 主文件任务列表)
/// - 若主文件为空文件或损坏,尝试读取备份文件,若备份文件存在内容,发布警告,恢复数据,返回(Backup, 备份文件任务列表)
/// - 若主文件为空文件,尝试读取备份文件,若备份文件也为空,返回空列表重新开始
/// - 若主文件损坏,尝试读取备份文件,若备份文件为空,直接返回错误
fn load_for_update(
    main: &File,
    main_path: &Path,
    backup: &File,
    backup_path: &Path,
) -> Result<(Origin, Vec<Task>), AppError> {
    match read_task_from(main, main_path) {
        Ok(Some(tasks)) => Ok((Origin::Main, tasks)),
        Ok(None) => match read_task_from(backup, backup_path)? {
            Some(tasks) => {
                warn_recovered_from_backup(backup_path);
                Ok((Origin::Backup, tasks))
            }
            None => Ok((Origin::Main, Vec::new())),
        },
        Err(err) => match read_task_from(backup, backup_path)? {
            Some(tasks) => {
                warn_recovered_from_backup(backup_path);
                Ok((Origin::Backup, tasks))
            }
            None => Err(err),
        },
    }
}

/// 为TaskStore中只读的方法提供主文件为空或损坏后的回落
///
/// 逻辑:
/// - 若备份文件存在内容,抛出警告并返回备份文件中的内容
/// - 若备份文件为空,返回空列表
/// - 若备份文件损坏,返回错误
fn load_backup_fallback(backup_path: &Path) -> Result<Vec<Task>, AppError> {
    let backup_file = match open_read(backup_path) {
        Ok(file) => file,
        Err(err) if is_not_found(&err) => return Ok(Vec::new()),
        Err(err) => return Err(err),
    };
    lock_share(&backup_file, backup_path)?;
    match read_task_from(&backup_file, backup_path) {
        Ok(Some(tasks)) => {
            warn_recovered_from_backup(backup_path);
            Ok(tasks)
        }
        Ok(None) => Ok(Vec::new()),
        Err(err) => Err(err),
    }
}

/// 为TaskStore中只读的读取备份文件提供实现函数
///
/// 逻辑:
/// - 若备份文件不存在,返回`NothingToUndo`错误
/// - 若备份文件存在且不为空,返回备份文件中的任务列表
/// - 其他情况,返回`NothingToUndo`错误
fn load_backup_strict(backup_path: &Path) -> Result<Vec<Task>, AppError> {
    let backup_file = match open_read(backup_path) {
        Ok(file) => file,
        Err(err) if is_not_found(&err) => return Err(AppError::NothingToUndo),
        Err(e) => return Err(e),
    };
    lock_share(&backup_file, backup_path)?;
    match read_task_from(&backup_file, backup_path)? {
        Some(tasks) if !tasks.is_empty() => Ok(tasks),
        _ => Err(AppError::NothingToUndo),
    }
}

/// 为TaskStore中使用备份覆盖主文件的undo操作提供实现函数
///
/// 逻辑:
/// - 读取备份文件中的任务列表,若没有任务,返回`NothingToUndo`错误
/// - 读取备份文件的字符比特,覆写主文件,完成undo
fn restore_main_from_backup(
    main: &File,
    main_path: &Path,
    backup: &File,
    backup_path: &Path,
) -> Result<(), AppError> {
    if read_task_from(backup, backup_path)?.is_none() {
        return Err(AppError::NothingToUndo);
    }
    let data = read_bytes(backup, backup_path)?;
    overwrite(main, &data, main_path)
}

/// 单元测试
#[cfg(test)]
mod tests {
    use chrono::{DateTime, Utc};

    use crate::task::Priority;

    use super::*;
    use std::{fs, os::unix::fs::PermissionsExt, process};

    fn temp_path(name: &str) -> String {
        std::env::temp_dir()
            .join(format!("{}_rstodo_test_{}.json", process::id(), name))
            .to_string_lossy()
            .to_string()
    }

    fn write_file(path: &Path, contents: &str) {
        fs::write(path, contents).unwrap();
    }

    fn set_test_task() -> Task {
        let deadline: DateTime<Utc> = "2000-1-1T12:00:00+00:00".parse().unwrap();
        Task::new(
            1,
            "test_task1".to_string(),
            Some("desc".to_string()),
            Some(deadline),
            Priority::High,
        )
    }

    #[test]
    fn file_path() {
        let store = TaskStore::new(Some("test1.json".to_string()));
        assert_eq!(store.main_path(), Path::new("test1.json"));
        assert_eq!(store.backup_path(), Path::new("test1.json.bak"));

        let default_store = TaskStore::new(None);
        let main = default_store.main_path();
        assert!(main.ends_with(Path::new("rstodo").join("task.json")));
        assert_eq!(
            default_store.backup_path(),
            main.with_file_name("task.json.bak")
        );
    }

    #[test]
    fn test_load_tasks() {
        let file = temp_path("load");
        let path = TaskStore::new(Some(file.clone()));
        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());

        assert_eq!(path.load().unwrap(), Vec::new());

        write_file(path.main_path(), "");
        assert_eq!(path.load().unwrap(), Vec::new());

        write_file(
            path.main_path(),
            &serde_json::to_string_pretty(&vec![set_test_task()]).unwrap(),
        );
        let load = path.load().unwrap();
        assert_eq!(load, vec![set_test_task()]);

        write_file(path.main_path(), "{Illegal data");
        match path.load() {
            Err(AppError::Corrupted { path, source: _ }) => {
                assert_eq!(path, file)
            }
            _ => unreachable!(),
        }

        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());
    }

    #[test]
    fn test_update() {
        let file = temp_path("update");
        let path = TaskStore::new(Some(file.clone()));
        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());
        for _ in 0..5 {
            path.update(|t| {
                t.push(set_test_task());
                Ok(())
            })
            .unwrap();
        }
        let load = path.load().unwrap();
        assert_eq!(load.len(), 5);

        for task in load.iter().take(5) {
            assert_eq!(task.id(), 1);
            assert_eq!(task._content(), "test_task1".to_string());
            assert_eq!(task.description(), Some("desc".to_string()));
            assert_eq!(
                task.deadline(),
                Some("2000-1-1T12:00:00+00:00".parse::<DateTime<Utc>>().unwrap())
            );
            assert_eq!(task.priority(), Priority::High);
        }

        path.update(|t| {
            t.clear();
            Ok(())
        })
        .unwrap();
        let load = path.load().unwrap();
        assert_eq!(load, Vec::new());
        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());
    }

    #[test]
    fn test_update_err() {
        let file = temp_path("update_err");
        let path = TaskStore::new(Some(file.clone()));
        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());

        path.update(|t| {
            t.push(set_test_task());
            Ok(())
        })
        .unwrap();

        let before = fs::read_to_string(path.main_path()).unwrap();
        let err = path.update(|_| Err(AppError::NothingToChange)).unwrap_err();
        match err {
            AppError::NothingToChange => {}
            _ => unreachable!(),
        }
        assert_eq!(fs::read_to_string(path.main_path()).unwrap(), before);
        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());
    }

    #[test]
    fn test_load_tasks_err() {
        let file = temp_path("load_err_not_utf8");
        let path = TaskStore::new(Some(file.clone()));
        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());

        fs::write(path.main_path(), [0xFF, 0xFF]).unwrap();
        let err = path.load().unwrap_err();
        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());

        match err {
            AppError::Io {
                operation,
                path: _,
                source,
            } => {
                assert_eq!(operation, "read to string");
                assert!(!source.to_string().is_empty());
            }
            _ => unreachable!(),
        }
    }

    #[cfg(unix)]
    #[test]
    fn test_permission_denied() {
        let file = temp_path("permission_denied");
        let path = TaskStore::new(Some(file.clone()));
        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());

        fs::write(path.main_path(), "test_content").unwrap();
        assert_eq!(
            fs::read_to_string(path.main_path()).unwrap(),
            "test_content".to_string()
        );

        let no_permission = 0o000;
        let std_permission = 0o644;
        fs::set_permissions(path.main_path(), fs::Permissions::from_mode(no_permission)).unwrap();
        let err = path.load().unwrap_err();
        match err {
            AppError::Io {
                operation,
                path: _,
                source,
            } => {
                assert_eq!(operation, "open read");
                assert!(!source.to_string().is_empty());
            }
            _ => unreachable!(),
        }
        fs::set_permissions(path.main_path(), fs::Permissions::from_mode(std_permission)).unwrap();
        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());
    }

    #[test]
    fn test_update_create_dir() {
        let dir = temp_path("create_file");
        let file = format!("{}/sub/subsub/task.json", dir);
        let path = TaskStore::new(Some(file.clone()));
        let _ = fs::remove_dir_all(&dir);

        path.update(|t| {
            t.push(set_test_task());
            Ok(())
        })
        .unwrap();
        let load = path.load().unwrap();
        let _ = fs::remove_dir_all(&dir);
        assert_eq!(load.len(), 1);
        assert_eq!(load[0]._content(), "test_task1".to_string());
    }

    #[test]
    fn test_update_create_dir_err() {
        let path = TaskStore::new(Some("".to_string()));
        let err = path.update(|_| Ok(())).unwrap_err();
        match err {
            AppError::Io {
                operation,
                path: _,
                source,
            } => {
                assert_eq!(operation, "open read write");
                assert!(!source.to_string().is_empty());
            }
            _ => unreachable!(),
        }
    }

    #[test] // 主文件损坏,备份为空
    fn test_backup1_fn_readonly() {
        let file = temp_path("test_backup1");
        let path = TaskStore::new(Some(file.clone()));
        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());

        write_file(path.main_path(), "{Illegal data");
        write_file(path.backup_path(), "");
        match path.load() {
            Err(AppError::Corrupted { path, source }) => {
                assert_eq!(path, file);
                assert!(!source.to_string().is_empty());
            }
            _ => unreachable!(),
        }
        assert_eq!(
            fs::read_to_string(path.main_path()).unwrap(),
            "{Illegal data"
        );

        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());
    }

    #[test] // 主文件损坏,备份完好,从备份恢复
    fn test_backup2_fn_readonly() {
        let file = temp_path("test_backup2");
        let path = TaskStore::new(Some(file.clone()));
        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());

        write_file(path.main_path(), "{Illegal data");
        write_file(
            path.backup_path(),
            &serde_json::to_string_pretty(&vec![set_test_task()]).unwrap(),
        );
        let load = path.load().unwrap();
        assert_eq!(load, vec![set_test_task()]);
        let backup: Vec<Task> =
            serde_json::from_str(&fs::read_to_string(path.backup_path()).unwrap()).unwrap();
        assert_eq!(backup, load);
        assert_eq!(backup, vec![set_test_task()]);

        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());
    }

    #[test] // 主文件损坏,备份为空
    fn test_backup3_fn_readwrite() {
        let file = temp_path("test_backup3");
        let path = TaskStore::new(Some(file.clone()));
        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());

        write_file(path.main_path(), "{Illegal data");
        write_file(path.backup_path(), "");

        match path.update(|t| {
            t.push(set_test_task());
            Ok(())
        }) {
            Err(AppError::Corrupted { path, source }) => {
                assert_eq!(path, file);
                assert!(!source.to_string().is_empty());
            }
            _ => unreachable!(),
        }
        assert_eq!(
            fs::read_to_string(path.main_path()).unwrap(),
            "{Illegal data"
        );

        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());
    }

    #[test] // 主文件损坏,备份完好,从备份恢复
    fn test_backup4_fn_readwrite() {
        let file = temp_path("test_backup4");
        let path = TaskStore::new(Some(file.clone()));
        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());

        write_file(path.main_path(), "{Illegal data");
        write_file(
            path.backup_path(),
            &serde_json::to_string_pretty(&vec![set_test_task()]).unwrap(),
        );

        path.update(|t| {
            t.push(set_test_task());
            Ok(())
        })
        .unwrap();
        let load = path.load().unwrap();
        assert_eq!(load.len(), 2);
        assert_eq!(load[0]._content(), "test_task1".to_string());
        assert_eq!(load[1]._content(), "test_task1".to_string());
        let backup: Vec<Task> =
            serde_json::from_str(&fs::read_to_string(path.backup_path()).unwrap()).unwrap();
        assert_eq!(backup, vec![set_test_task()]);

        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());
    }

    #[test] // 主文件确实,备份完好,从备份恢复
    fn test_backup5_fn_readwrite() {
        let file = temp_path("test_backup5");
        let path = TaskStore::new(Some(file.clone()));
        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());

        write_file(
            path.backup_path(),
            &serde_json::to_string_pretty(&vec![set_test_task()]).unwrap(),
        );

        path.update(|t| {
            t.push(set_test_task());
            Ok(())
        })
        .unwrap();
        let load = path.load().unwrap();
        assert_eq!(load.len(), 2);
        assert_eq!(load[0]._content(), "test_task1".to_string());
        assert_eq!(load[1]._content(), "test_task1".to_string());
        let backup: Vec<Task> =
            serde_json::from_str(&fs::read_to_string(path.backup_path()).unwrap()).unwrap();
        assert_eq!(backup, vec![set_test_task()]);

        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());
    }

    #[test]
    fn test_load_backup() {
        let file = temp_path("test_load_backup");
        let path = TaskStore::new(Some(file.clone()));
        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());

        let err = path.load_backup().unwrap_err();
        assert!(matches!(err, AppError::NothingToUndo));

        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());

        write_file(
            path.backup_path(),
            &serde_json::to_string_pretty(&vec![set_test_task()]).unwrap(),
        );

        let tasks = path.load_backup().unwrap();
        assert_eq!(tasks, vec![set_test_task()]);

        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());

        write_file(path.backup_path(), "");

        let err = path.load_backup().unwrap_err();
        assert!(matches!(err, AppError::NothingToUndo));

        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());

        write_file(path.backup_path(), "{Illegal data");
        let err = path.load_backup().unwrap_err();
        match err {
            AppError::Corrupted {
                path: err_path,
                source,
            } => {
                assert_eq!(err_path, path.backup);
                assert!(!source.to_string().is_empty());
            }
            _ => unreachable!(),
        }
        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());
    }

    #[test]
    fn test_restore_backup() {
        let file = temp_path("test_restore_backup");
        let path = TaskStore::new(Some(file.clone()));
        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());
        write_file(
            path.main_path(),
            &serde_json::to_string_pretty(&vec![set_test_task()]).unwrap(),
        );
        write_file(
            path.backup_path(),
            &serde_json::to_string_pretty(&vec![set_test_task(), set_test_task()]).unwrap(),
        );

        assert_eq!(path.load().unwrap(), vec![set_test_task()]);
        let backup: Vec<Task> =
            serde_json::from_str(&fs::read_to_string(path.backup_path()).unwrap()).unwrap();
        assert_eq!(backup, vec![set_test_task(), set_test_task()]);

        path.restore_backup().unwrap();
        assert_eq!(path.load().unwrap(), vec![set_test_task(), set_test_task()]);
        assert_eq!(
            path.load_backup().unwrap(),
            vec![set_test_task(), set_test_task()]
        );

        path.restore_backup().unwrap();
        assert_eq!(path.load().unwrap(), vec![set_test_task(), set_test_task()]);
        assert_eq!(
            path.load_backup().unwrap(),
            vec![set_test_task(), set_test_task()]
        );

        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());
        write_file(
            path.main_path(),
            &serde_json::to_string_pretty(&vec![set_test_task()]).unwrap(),
        );
        write_file(path.backup_path(), "");

        let err = path.restore_backup().unwrap_err();
        assert!(matches!(err, AppError::NothingToUndo));

        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());

        write_file(path.backup_path(), "{Illegal data");
        let err = path.restore_backup().unwrap_err();
        match err {
            AppError::Corrupted {
                path: err_path,
                source,
            } => {
                assert_eq!(err_path, path.backup);
                assert!(!source.to_string().is_empty());
            }
            _ => unreachable!(),
        }
        let _ = fs::remove_file(path.main_path());
        let _ = fs::remove_file(path.backup_path());
    }
}