lockbook_shared/
filename.rs

1use crate::file::File;
2
3pub const MAX_FILENAME_LENGTH: usize = 64;
4pub const MAX_ENCRYPTED_FILENAME_LENGTH: usize = MAX_FILENAME_LENGTH + 24;
5
6#[derive(Debug)]
7pub enum DocumentType {
8    Text,
9    Drawing,
10    Other,
11}
12
13// todo: be more exhaustive
14impl DocumentType {
15    pub fn from_file_name_using_extension(name: &str) -> DocumentType {
16        match name.split('.').last() {
17            Some("md") | Some("txt") => DocumentType::Text,
18            Some("draw") => DocumentType::Drawing,
19            _ => DocumentType::Other,
20        }
21    }
22}
23
24#[derive(PartialEq, Eq, Debug, Clone)]
25pub struct NameComponents {
26    pub name: String,
27    pub variant: Option<usize>,
28    pub extension: Option<String>,
29}
30
31impl NameComponents {
32    pub fn from(file_name: &str) -> NameComponents {
33        let extension_location = file_name.rfind('.').and_then(|location| {
34            if location == file_name.len() - 1 {
35                None
36            } else {
37                Some(location)
38            }
39        });
40
41        let name_with_variant = match extension_location {
42            Some(location) => &file_name[..location],
43            None => file_name,
44        };
45
46        let mut variant_location = name_with_variant.rfind('-');
47
48        let variant = variant_location
49            .map(|location| name_with_variant[location + 1..].to_string())
50            .and_then(|maybe_variant| maybe_variant.parse::<usize>().ok());
51
52        if variant.is_none() {
53            variant_location = None
54        }
55
56        let name = {
57            let name_right_bound =
58                variant_location.unwrap_or_else(|| extension_location.unwrap_or(file_name.len()));
59            file_name[0..name_right_bound].to_string()
60        };
61
62        let extension = extension_location.map(|location| file_name[location + 1..].to_string());
63
64        NameComponents { name, variant, extension }
65    }
66
67    pub fn generate_next(&self) -> NameComponents {
68        self.generate_incremented(1)
69    }
70
71    pub fn generate_incremented(&self, n: usize) -> NameComponents {
72        let mut next = self.clone();
73        next.variant = Some(self.variant.unwrap_or(0) + n);
74        next
75    }
76
77    pub fn next_in_children(&self, children: Vec<File>) -> NameComponents {
78        let mut next = self.clone();
79
80        let mut children: Vec<NameComponents> = children
81            .iter()
82            .filter_map(|f| {
83                let nc = NameComponents::from(&f.name);
84                if nc.name == next.name && nc.extension == next.extension {
85                    Some(nc)
86                } else {
87                    None
88                }
89            })
90            .collect();
91
92        children.sort_by(|a, b| {
93            a.variant
94                .unwrap_or_default()
95                .cmp(&b.variant.unwrap_or_default())
96        });
97
98        for (i, child) in children.iter().enumerate() {
99            if let Some(variant) = child.variant {
100                if i == 0 && variant > 0 {
101                    break; //insert self without an increment
102                }
103            }
104
105            if let Some(curr) = children.get(i + 1) {
106                if curr.variant.unwrap_or_default() != child.variant.unwrap_or_default() + 1 {
107                    next = child.generate_next();
108                    break;
109                }
110            } else {
111                next = child.generate_next();
112            }
113        }
114
115        next
116    }
117
118    pub fn to_name(&self) -> String {
119        match (&self.variant, &self.extension) {
120            (Some(variant), Some(extension)) => format!("{}-{}.{}", self.name, variant, extension),
121            (Some(variant), None) => format!("{}-{}", self.name, variant),
122            (None, Some(extension)) => format!("{}.{}", self.name, extension),
123            (None, None) => self.name.to_string(),
124        }
125    }
126}
127
128#[cfg(test)]
129mod unit_tests {
130    use uuid::Uuid;
131
132    use crate::{file::File, filename::NameComponents};
133
134    fn from_components(
135        name: &str, variant: Option<usize>, extension: Option<&str>,
136    ) -> NameComponents {
137        NameComponents {
138            name: name.to_string(),
139            variant,
140            extension: extension.map(|str| str.to_string()),
141        }
142    }
143
144    #[test]
145    fn test_name_components() {
146        assert_eq!(NameComponents::from("test-1.md"), from_components("test", Some(1), Some("md")));
147        assert_eq!(NameComponents::from("test-.md"), from_components("test-", None, Some("md")));
148        assert_eq!(NameComponents::from(".md"), from_components("", None, Some("md")));
149        assert_eq!(NameComponents::from(""), from_components("", None, None));
150        assert_eq!(
151            NameComponents::from("test-file.md"),
152            from_components("test-file", None, Some("md"))
153        );
154        assert_eq!(
155            NameComponents::from("test-file-1.md"),
156            from_components("test-file", Some(1), Some("md"))
157        );
158        assert_eq!(
159            NameComponents::from("test-file-1.md."),
160            from_components("test-file-1.md.", None, None)
161        );
162        assert_eq!(
163            NameComponents::from("test-file-1.m"),
164            from_components("test-file", Some(1), Some("m"))
165        );
166        assert_eq!(
167            NameComponents::from("test-file-100.m"),
168            from_components("test-file", Some(100), Some("m"))
169        );
170        assert_eq!(
171            NameComponents::from("test-file--100.m"),
172            from_components("test-file-", Some(100), Some("m"))
173        );
174        assert_eq!(
175            NameComponents::from("test-file-.-100.m"),
176            from_components("test-file-.", Some(100), Some("m"))
177        );
178        assert_eq!(NameComponents::from("."), from_components(".", None, None));
179        assert_eq!(NameComponents::from("-1."), from_components("-1.", None, None));
180        assert_eq!(NameComponents::from("-1."), from_components("-1.", None, None));
181        assert_eq!(NameComponents::from("test"), from_components("test", None, None));
182        assert_eq!(NameComponents::from("test-32"), from_components("test", Some(32), None));
183    }
184
185    fn assert_symmetry(name: &str) {
186        assert_eq!(NameComponents::from(name).to_name(), name);
187    }
188
189    #[test]
190    fn test_back_to_name() {
191        assert_symmetry("test-1.md");
192        assert_symmetry("test-.md");
193        assert_symmetry(".md");
194        assert_symmetry("");
195        assert_symmetry("test-file.md");
196        assert_symmetry("test-file-1.md");
197        assert_symmetry("test-file-1.md.");
198        assert_symmetry("test-file-1.m");
199        assert_symmetry("test-file-100.m");
200        assert_symmetry("test-file--100.m");
201        assert_symmetry("test-file-.-100.m");
202        assert_symmetry(".");
203        assert_symmetry("-1.");
204        assert_symmetry("-1.");
205        assert_symmetry("test");
206        assert_symmetry("test-32");
207    }
208
209    #[test]
210    fn test_next_variant() {
211        assert_eq!(NameComponents::from("test.md").generate_next().to_name(), "test-1.md");
212        assert_eq!(NameComponents::from("test-2.md").generate_next().to_name(), "test-3.md");
213    }
214    #[test]
215    fn test_next_in_children() {
216        let children = new_files(vec!["untitled.md", "untitled-1.md", "untitled-2.md"]);
217        let next = NameComponents::from("untitled.md").next_in_children(children);
218        assert_eq!(next.to_name(), "untitled-3.md");
219
220        let children =
221            new_files(vec!["untitled-1.md", "untitled-2.md", "untitled-3.md", "untitled-4.md"]);
222        let next = NameComponents::from("untitled.md").next_in_children(children);
223        assert_eq!(next.to_name(), "untitled.md");
224
225        let children = new_files(vec!["untitled.md", "untitled-2.md", "untitled-3.md"]);
226        let next = NameComponents::from("untitled.md").next_in_children(children);
227        assert_eq!(next.to_name(), "untitled-1.md");
228    }
229
230    fn new_files(names: Vec<&str>) -> Vec<File> {
231        names
232            .iter()
233            .map(|&name| File {
234                id: Uuid::default(),
235                parent: Uuid::default(),
236                name: name.to_string(),
237                file_type: crate::file_metadata::FileType::Document,
238                last_modified: u64::default(),
239                last_modified_by: String::default(),
240                shares: vec![],
241            })
242            .collect()
243    }
244}