bernardo-tui 0.2.7

A keyboard-only, distraction-free TUI widget library
Documentation
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
use std::collections::HashMap;
use std::fmt::{Debug, Formatter};
use std::fs::Metadata;
use std::path::{Component, Path, PathBuf};
use std::sync::RwLock;
use std::{fs, io};

use log::{debug, error};
use streaming_iterator::StreamingIterator;

use crate::fs::dir_entry::DirEntry;
use crate::fs::filesystem_front::FilesystemFront;
use crate::fs::fsf_ref::FsfRef;
use crate::fs::read_error::{ListError, ReadError};
use crate::fs::write_error::WriteError;

pub enum Record {
    File(Vec<u8>),
    /*
    PathBuf represents here only the *last* component of full PathBuf
     */
    Dir(HashMap<PathBuf, Record>),
}

impl Record {
    // If creating == true, it creates a Dir, but since it returns a mut ref you can immediately
    // override it with File.
    fn get_mut(&mut self, path: &[Component], creating: bool) -> Option<&mut Record> {
        if path.is_empty() {
            Some(self)
        } else {
            let first = PathBuf::new().join(path[0]);
            match self {
                Record::File(_) => None,
                Record::Dir(ref mut items) => {
                    if items.contains_key(&first) {
                        return items.get_mut(&first).unwrap().get_mut(&path[1..], creating);
                    }

                    if creating {
                        items.insert(first.clone(), Record::Dir(HashMap::new()));
                        return items.get_mut(&first).unwrap().get_mut(&path[1..], creating);
                    }

                    None
                }
            }
        }
    }

    fn get(&self, path: &[Component]) -> Option<&Record> {
        if path.is_empty() {
            Some(self)
        } else {
            let first = PathBuf::new().join(path[0]);
            match self {
                Record::File(_) => None,
                Record::Dir(ref items) => items.get(&first).and_then(|r| r.get(&path[1..])),
            }
        }
    }

    fn is_empty_dir(&self) -> bool {
        match &self {
            Record::File(_) => false,
            Record::Dir(contents) => contents.is_empty(),
        }
    }

    fn is_dir(&self) -> bool {
        match &self {
            Record::File(_) => false,
            Record::Dir(_contents) => true,
        }
    }

    fn is_file(&self) -> bool {
        !self.is_dir()
    }

    fn create_dir(&mut self, path: &Path) -> bool {
        let components: Vec<Component> = path.components().collect();

        if self.get(&components).is_some() {
            return false;
        }

        self.get_mut(&components, true).is_some()
    }

    // fn overwrite_file(&mut self, path: &Path, contents: Vec<u8>) -> bool {
    //     let components: Vec<Component> = path.components().collect();
    //
    //     if let Some(rec) = self.get_mut(&components, false) {
    //         if rec.is_file() {
    //             *rec = Record::File(contents);
    //             true
    //         } else {
    //             false
    //         }
    //     } else {
    //         false
    //     }
    // }

    fn create_file(&mut self, path: &Path, contents: Vec<u8>) -> bool {
        let components: Vec<Component> = path.components().collect();

        if self.get(&components).is_some() {
            return false;
        }

        self.get_mut(&components, true)
            .map(|maybe_last| {
                if maybe_last.is_empty_dir() {
                    *maybe_last = Record::File(contents);
                    true
                } else {
                    false
                }
            })
            .unwrap_or(false)
    }

    fn list(&self) -> Option<Vec<PathBuf>> {
        match self {
            Record::File(_) => None,
            Record::Dir(e) => {
                let files: Vec<_> = e.keys().cloned().collect();
                Some(files)
            }
        }
    }

    fn from_real(path: &Path) -> std::io::Result<Record> {
        debug_assert!(path.is_absolute());

        if path.is_file() {
            fs::read(path).map(Record::File)
        } else {
            let read_dir = fs::read_dir(path)?;
            let mut dir_contents: HashMap<PathBuf, Record> = Default::default();
            for dir_entry_res in read_dir {
                match dir_entry_res {
                    Ok(dir_entry) => {
                        let name: PathBuf = dir_entry.file_name().into();
                        let item = match Self::from_real(&dir_entry.path()) {
                            Ok(i) => i,
                            Err(e) => {
                                error!("failed creating item {:?} because: {:?}", name, e);
                                continue;
                            }
                        };
                        dir_contents.insert(name, item);
                    }
                    Err(e) => {
                        error!("failed retrieving dir_entry, skipping: {:?}", e);
                        continue;
                    }
                }
            }
            Ok(Record::Dir(dir_contents))
        }
    }
}

pub struct MockFS {
    root_path: PathBuf,
    root_dir: RwLock<Record>,
}

impl MockFS {
    pub fn new<T: Into<PathBuf>>(root_path: T) -> Self {
        let root_path: PathBuf = root_path.into();
        let root_path = if root_path.is_absolute() {
            root_path
        } else {
            root_path.canonicalize().unwrap() // TODO unwrap
        };

        MockFS {
            root_path,
            root_dir: RwLock::new(Record::Dir(HashMap::default())),
        }
    }

    pub fn with_file<P: AsRef<Path>, B: Into<Vec<u8>>>(mut self, path: P, bytes: B) -> Self {
        self.add_file(path.as_ref(), bytes.into())
            .unwrap_or_else(|_| error!("failed creating file in mockfs"));
        self
    }

    pub fn with_dir<P: AsRef<Path>>(self, path: P) -> Self {
        self.add_dir(path.as_ref())
            .unwrap_or_else(|_| error!("failed creating dir in mockfs"));
        self
    }

    pub fn add_dir(&self, path: &Path) -> Result<(), ()> {
        if self.root_dir.try_write().unwrap().create_dir(path) {
            Ok(())
        } else {
            Err(())
        }
    }

    pub fn add_file(&mut self, path: &Path, bytes: Vec<u8>) -> Result<(), ()> {
        if self.root_dir.try_write().unwrap().create_file(path, bytes) {
            Ok(())
        } else {
            Err(())
        }
    }

    pub fn blocking_overwrite_with_bytes(&self, path: &Path, bytes: &[u8], must_exist: bool) -> Result<usize, WriteError> {
        let comp: Vec<_> = path.components().collect();

        if let Some(record) = self.root_dir.try_read().unwrap().get(&comp) {
            if record.is_dir() {
                return Err(WriteError::NotAFile);
            }
        } else if must_exist {
            return Err(WriteError::FileNotFound);
        }

        let len_bytes = bytes.len();

        let mut binding = self.root_dir.try_write().unwrap();
        let record = binding.get_mut(&comp, true).unwrap();

        *record = Record::File(bytes.to_vec());
        Ok(len_bytes)
    }

    pub fn generate_from_real<P: AsRef<Path>>(path: P) -> io::Result<Self> {
        let path: &Path = path.as_ref();
        let path = if path.is_absolute() {
            path.to_path_buf()
        } else {
            path.canonicalize()?
        };

        let root = Record::from_real(path.as_ref())?;

        if !root.is_dir() {
            return Err(io::ErrorKind::InvalidInput.into());
        }

        Ok(MockFS {
            root_path: path,
            root_dir: RwLock::new(root),
        })
    }
}

impl Debug for MockFS {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "MockFilesystem({})", self.root_path.to_string_lossy())
    }
}

impl FilesystemFront for MockFS {
    fn root_path(&self) -> &PathBuf {
        debug_assert!(self.root_path.is_absolute());
        &self.root_path
    }

    fn blocking_read_entire_file(&self, path: &Path) -> Result<Vec<u8>, ReadError> {
        let comp: Vec<_> = path.components().collect();
        if let Some(rec) = self.root_dir.read().unwrap().get(&comp) {
            match rec {
                Record::File(contents) => Ok(contents.clone()),
                Record::Dir(_) => Err(ReadError::NotAFilePath),
            }
        } else {
            Err(ReadError::FileNotFound)
        }
    }

    fn is_dir(&self, path: &Path) -> bool {
        let comp: Vec<_> = path.components().collect();
        self.root_dir.read().unwrap().get(&comp).map(|r| r.is_dir()).unwrap_or(false)
    }

    fn is_file(&self, path: &Path) -> bool {
        let comp: Vec<_> = path.components().collect();
        self.root_dir.read().unwrap().get(&comp).map(|r| r.is_file()).unwrap_or(false)
    }

    fn hash_seed(&self) -> usize {
        2
    }

    fn blocking_list(&self, path: &Path) -> Result<Vec<DirEntry>, ListError> {
        if !self.exists(path) {
            return Err(ListError::PathNotFound);
        }

        if !self.is_dir(path) {
            return Err(ListError::NotADir);
        }

        let comp: Vec<_> = path.components().collect();
        let items = if comp.is_empty() {
            self.root_dir.read().unwrap().list()
        } else {
            match self.root_dir.read().unwrap().get(&comp) {
                None => {
                    error!("this test was redundant and still failed!");
                    return Err(ListError::PathNotFound);
                }
                Some(dir) => dir.list(),
            }
        };

        match items {
            None => {
                error!("this test was redundant 2 and still failed!");
                Err(ListError::NotADir)
            }
            Some(mut items) => {
                items.sort();
                Ok(items.into_iter().map(DirEntry::new).collect())
            }
        }
    }

    fn metadata(&self, path: &Path) -> Result<Metadata, ReadError> {
        Err(ReadError::UnmappedError("not imlpemented".to_string()))
    }

    fn file_size(&self, path: &Path) -> Result<u64, ReadError> {
        self.blocking_read_entire_file(path).map(|b| b.len() as u64)
    }

    fn exists(&self, path: &Path) -> bool {
        let comp: Vec<_> = path.components().collect();
        self.root_dir.read().unwrap().get(&comp).is_some()
    }

    fn blocking_overwrite_with_stream(
        &self,
        path: &Path,
        stream: &mut dyn StreamingIterator<Item = [u8]>,
        must_exist: bool,
    ) -> Result<usize, WriteError> {
        debug!("writing to [{:?}]", path);
        let mut bytes = Vec::<u8>::new();
        while let Some(chunk) = stream.next() {
            bytes.append(&mut Vec::from(chunk));
        }

        self.blocking_overwrite_with_bytes(path, &bytes, must_exist)
    }

    fn blocking_overwrite_with_bytes(&self, path: &Path, s: &[u8], must_exist: bool) -> Result<usize, WriteError> {
        self.blocking_overwrite_with_bytes(path, s, must_exist)
    }

    fn to_fsf(self) -> FsfRef {
        FsfRef::new(self)
    }
}

// these are purely API tests, like "does it have semantics I like", not "does it work well"
#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use std::path::{Path, PathBuf};

    use crate::de;
    use crate::fs::filesystem_front::FilesystemFront;
    use crate::fs::mock_fs::{MockFS, Record};
    use crate::fs::read_error::ReadError;

    #[test]
    fn make_some_records() {
        let record = Record::Dir(HashMap::new());

        let some_path = PathBuf::from("hello/some/path/item.txt");
        let comps: Vec<_> = some_path.components().collect();

        assert!(record.get(&comps[0..1]).is_none());
    }

    #[test]
    fn make_some_files() {
        let mockfs = MockFS::new("/tmp")
            .with_file("folder1/file1.txt", "some text")
            .with_file("folder2/file2.txt", "some text2");

        assert!(mockfs.is_dir(Path::new("folder1")));
        assert!(mockfs.is_dir(Path::new("folder2")));
        assert!(!mockfs.is_dir(Path::new("folder3")));
        assert!(mockfs.is_dir(Path::new("")));

        assert!(mockfs.is_file(Path::new("folder1/file1.txt")));
        assert!(mockfs.is_file(Path::new("folder2/file2.txt")));
        assert!(!mockfs.is_file(Path::new("folder1")));
        assert!(!mockfs.is_file(Path::new("folder2")));
        assert!(!mockfs.is_file(Path::new("")));

        assert_eq!(mockfs.blocking_list(Path::new("")).unwrap(), vec![de!("folder1"), de!("folder2")]);

        assert_eq!(mockfs.blocking_read_entire_file(Path::new("")), Err(ReadError::NotAFilePath));
        assert_eq!(
            mockfs.blocking_read_entire_file(Path::new("/folder3")),
            Err(ReadError::FileNotFound)
        );
        assert_eq!(mockfs.blocking_read_entire_file(Path::new("folder2")), Err(ReadError::NotAFilePath));
        assert_eq!(
            mockfs.blocking_read_entire_file(Path::new("folder1/file1.txt")),
            Ok("some text".as_bytes().to_vec())
        );
        assert_eq!(
            mockfs.blocking_read_entire_file(Path::new("folder1/file3.txt")),
            Err(ReadError::FileNotFound)
        );
    }

    #[test]
    fn mock_save_load() {
        let mockfs = MockFS::new("/tmp")
            .with_file("folder1/file1.txt", "some text")
            .with_file("folder2/file2.txt", "some text2")
            .to_fsf();

        let new_content = "replaced text 1";
        let spath = mockfs.descendant_checked("folder1/file1.txt").unwrap();
        assert!(mockfs.overwrite_with_str(&spath, new_content, true).is_ok());

        let binding = mockfs.blocking_read_entire_file(&spath).unwrap();
        let read_content = std::str::from_utf8(binding.as_slice()).unwrap();

        assert_eq!(read_content, new_content)
    }
}