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

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

            
94
impl AsRef<Path> for IndexPath {
95
    fn as_ref(&self) -> &Path {
96
        &self.0
97
    }
98
}