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
#![deny(clippy::pedantic)]
// #![warn(missing_docs)]

use set_error::ChangeError;

use std::{
    path::Path,
    rc::Rc,
    time::{Duration, SystemTime},
};

pub struct FileListBuilder {
    files: Vec<WatchedFile>,
    interval: Duration,
    max_retries: Option<u32>,
}

pub struct WatchedFile {
    path: String,
    time: SystemTime,
    functions_on_run: Vec<Rc<Fn(String) -> WatchingFuncResult>>,
}

pub enum WatchingFuncResult {
    Success,
    Retry,
}

impl FileListBuilder {
    fn new() -> Self {
        Self {
            files: Vec::new(),
            interval: Duration::from_millis(1000),
            max_retries: None,
        }
    }
    fn launch(self) -> () {}
    fn add_file(mut self, file: WatchedFile) -> Self {
        self.files.push(file);
        self
    }
    fn with_interval(mut self, inter: Duration) -> Self {
        self.interval = inter;
        self
    }
    fn with_max_retries(mut self, re: u32) -> Self {
        self.max_retries = Some(re);
        self
    }
}

impl WatchedFile {
    fn new(path: String) -> Result<Self, String> {
        Ok(Self {
            path: path.clone(),
            time: Path::new(&path)
                .metadata()
                .set_error(&format!("failed to open file {} metadata", path))?
                .modified()
                .set_error(&format!("failed to find files date modified {}", path))?,
            functions_on_run: Vec::new(),
        })
    }
    fn add_func<F: 'static + Fn(String) -> WatchingFuncResult>(mut self, func: F) -> Self {
        self.functions_on_run.push(Rc::new(func));
        self
    }
}