1
use chrono::{DateTime, Utc};
2
use std::{
3
    ffi::OsStr,
4
    fs::DirEntry,
5
    path::{Path, PathBuf},
6
};
7

            
8
pub(crate) struct IndexPath(PathBuf);
9

            
10
impl IndexPath {
11
    pub(crate) fn to_path_buf(&self) -> PathBuf {
12
        self.0.clone()
13
    }
14
98
    fn entries(&self) -> std::io::Result<impl Iterator<Item = DirEntry>> {
15
98
        std::fs::read_dir(self.0.parent().unwrap()).map(|iter| {
16
470
            iter.filter_map(|e| e.ok()).filter(|e| {
17
470
                if e.file_name() == INDEX_NAME {
18
98
                    return false;
19
372
                }
20
372
                // skip entries that are not files
21
372
                e.file_type().map(|ft| ft.is_file()).unwrap_or_default()
22
470
            })
23
98
        })
24
98
    }
25
88
    pub(crate) fn file_paths(&self) -> std::io::Result<impl Iterator<Item = PathBuf>> {
26
352
        self.entries().map(|f| f.map(|e| e.path()))
27
88
    }
28
10
    pub(crate) fn files(&self) -> std::io::Result<impl Iterator<Item = String>> {
29
10
        self.entries()
30
20
            .map(|f| f.filter_map(|path| path.file_name().to_str().map(|s| s.to_string())))
31
10
    }
32
46
    pub(crate) fn dir_name(&self) -> &OsStr {
33
46
        self.0.parent().unwrap().file_name().unwrap()
34
46
    }
35
44
    pub(crate) fn date(&self) -> DateTime<Utc> {
36
44
        DateTime::parse_from_rfc3339(self.dir_name().to_str().unwrap())
37
44
            .unwrap()
38
44
            .into()
39
44
    }
40
}
41

            
42
30
#[derive(Debug, thiserror::Error)]
43
#[error("{0}")]
44
pub(crate) struct InvalidIndexPath(String, PathBuf);
45

            
46
const INDEX_NAME: &str = "index.ttl";
47

            
48
impl TryFrom<PathBuf> for IndexPath {
49
    type Error = InvalidIndexPath;
50

            
51
152
    fn try_from(dir: PathBuf) -> Result<Self, Self::Error> {
52
152
        let file_name = if let Some(file_name) = dir.file_name() {
53
142
            file_name
54
        } else {
55
10
            return Err(InvalidIndexPath(
56
10
                format!("{} does not contain a file name.", dir.display()),
57
10
                dir,
58
10
            ));
59
        };
60
142
        let file_name = if let Some(file_name) = file_name.to_str() {
61
132
            file_name
62
        } else {
63
10
            return Err(InvalidIndexPath(
64
10
                format!(
65
10
                    "The name {} is not valid unicode",
66
10
                    file_name.to_string_lossy()
67
10
                ),
68
10
                dir,
69
10
            ));
70
        };
71
132
        if DateTime::parse_from_rfc3339(file_name).is_err() {
72
11
            return Err(InvalidIndexPath(
73
11
                format!("{} is not a valid rfc339 date", file_name),
74
11
                dir,
75
11
            ));
76
121
        }
77
121
        Ok(Self(dir.join(INDEX_NAME)))
78
152
    }
79
}
80
impl TryFrom<&Path> for IndexPath {
81
    type Error = InvalidIndexPath;
82
61
    fn try_from(value: &Path) -> Result<Self, Self::Error> {
83
61
        IndexPath::try_from(value.to_path_buf())
84
61
    }
85
}
86

            
87
impl AsRef<Path> for IndexPath {
88
33
    fn as_ref(&self) -> &Path {
89
33
        &self.0
90
33
    }
91
}