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
112
    fn entries(&self) -> std::io::Result<impl Iterator<Item = DirEntry>> {
15
112
        std::fs::read_dir(self.0.parent().unwrap()).map(|iter| {
16
536
            iter.filter_map(|e| e.ok()).filter(|e| {
17
536
                if e.file_name() == INDEX_NAME {
18
112
                    return false;
19
424
                }
20
424
                // skip entries that are not files
21
424
                e.file_type().map(|ft| ft.is_file()).unwrap_or_default()
22
536
            })
23
112
        })
24
112
    }
25
100
    pub(crate) fn file_paths(&self) -> std::io::Result<impl Iterator<Item = PathBuf>> {
26
400
        self.entries().map(|f| f.map(|e| e.path()))
27
100
    }
28
12
    pub(crate) fn files(&self) -> std::io::Result<impl Iterator<Item = String>> {
29
12
        self.entries()
30
24
            .map(|f| f.filter_map(|path| path.file_name().to_str().map(|s| s.to_string())))
31
12
    }
32
52
    pub(crate) fn dir_name(&self) -> &OsStr {
33
52
        self.0.parent().unwrap().file_name().unwrap()
34
52
    }
35
50
    pub(crate) fn date(&self) -> DateTime<Utc> {
36
50
        DateTime::parse_from_rfc3339(self.dir_name().to_str().unwrap())
37
50
            .unwrap()
38
50
            .into()
39
50
    }
40
}
41

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

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