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
use crate::rust::Checksum;
use serde::{Deserialize, Serialize, Serializer};
use std::{
    collections::BTreeMap,
    ffi::OsString,
    fmt::{Display, Formatter, Result as FmtResult},
    path::{Component, Path, PathBuf},
};
use thiserror::Error;

// TODO: impl serialize, deserialize, display, from_str, etc
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct EntryName(OsString);

impl EntryName {
    pub fn new<S: Into<OsString>>(input: S) -> Self {
        Self(input.into())
    }
}

impl Display for EntryName {
    fn fmt(&self, fmt: &mut Formatter<'_>) -> FmtResult {
        for chunk in self.0.as_encoded_bytes().utf8_chunks() {
            for character in chunk.valid().chars() {
                #[allow(clippy::match_like_matches_macro)]
                let escape = match character {
                    character if character.is_control() => true,
                    _ => false,
                };
                match escape {
                    true => {
                        let mut buffer = [0; 4];
                        let string = character.encode_utf8(&mut buffer);
                        write!(fmt, "\\u{{")?;
                        for byte in string.bytes() {
                            write!(fmt, "{byte:02x}")?;
                        }
                        write!(fmt, "}}")?;
                    }
                    false => write!(fmt, "{character}")?,
                }
            }
            for byte in chunk.invalid().iter() {
                write!(fmt, "\\x{{{byte:02x}}}")?;
            }
        }
        Ok(())
    }
}

#[test]
fn test_entry_name_display() {
    assert_eq!(&EntryName::new("abc".to_string()).to_string(), "abc");
    assert_eq!(
        &EntryName::new("abc def".to_string()).to_string(),
        "abc def"
    );
    assert_eq!(
        &EntryName::new("file.rs".to_string()).to_string(),
        "file.rs"
    );
}

impl Serialize for EntryName {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        if serializer.is_human_readable() {
            let encoded = self.to_string();
            serializer.serialize_str(&encoded)
        } else {
            serializer.serialize_bytes(self.0.as_encoded_bytes())
        }
    }
}

#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
pub struct Node {
    pub name: OsString,
    // TODO: optional?
    pub mode: u32,
    pub uid: u64,
    pub gid: u64,
    pub mtime: u64,
    pub kind: Kind,
}

#[derive(Error, Debug)]
pub enum MkdirError {
    #[error("non-relative path")]
    NonRelativePath,
    #[error("parent dir")]
    ParentDir,
    #[error("encountered file")]
    EncounteredFile,
}

#[derive(Error, Debug)]
pub enum InsertError {
    #[error("entry exists already")]
    Exists,
    #[error("parent is not a directory")]
    NotDirectory,
}

impl Node {
    pub fn mkdir(&mut self, path: &Path) -> Result<&mut Self, MkdirError> {
        let mut node = self;
        for component in path.components() {
            let next = match component {
                Component::RootDir => return Err(MkdirError::NonRelativePath),
                Component::Prefix(_) => return Err(MkdirError::NonRelativePath),
                // should this be an error?
                Component::CurDir => continue,
                Component::ParentDir => return Err(MkdirError::ParentDir),
                Component::Normal(path) => path,
            };
            node = match &mut node.kind {
                Kind::File(_) => return Err(MkdirError::EncounteredFile),
                Kind::Link(_) => return Err(MkdirError::EncounteredFile),
                Kind::Directory(children) => children
                    .entry(next.into())
                    .or_insert_with(|| Node::dir(next.into())),
            };
        }

        Ok(node)
    }

    pub fn root() -> Self {
        Self::dir("".into())
    }

    pub fn dir(name: OsString) -> Self {
        Node {
            name,
            mode: 0o644,
            uid: 0,
            gid: 0,
            mtime: 0,
            kind: Kind::Directory(BTreeMap::default()),
        }
    }

    pub fn insert(&mut self, node: Node) -> Result<(), InsertError> {
        match &mut self.kind {
            Kind::Directory(ref mut children) if !children.contains_key(&node.name) => {
                children.insert(node.name.clone(), node);
                Ok(())
            }
            Kind::Directory(_) => Err(InsertError::Exists),
            _ => Err(InsertError::NotDirectory),
        }
    }

    pub fn objects(&self) -> Box<dyn Iterator<Item = Checksum> + '_> {
        match &self.kind {
            Kind::Link(_) => Box::new(std::iter::empty()),
            Kind::File(checksum) => Box::new(std::iter::once(*checksum)),
            Kind::Directory(children) => Box::new(children.values().flat_map(|c| c.objects())),
        }
    }
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub enum Kind {
    File(Checksum),
    Link(PathBuf),
    Directory(BTreeMap<OsString, Node>),
}

impl Default for Kind {
    fn default() -> Self {
        Kind::Directory(Default::default())
    }
}