Skip to main content

fast_walker/item/
mod.rs

1use std::{
2    fmt::{Debug, Display, Formatter},
3    fs::{DirEntry, File, ReadDir},
4    path::PathBuf,
5};
6
7mod convert;
8
9#[derive(Clone, Debug)]
10pub struct WalkItem {
11    pub path: PathBuf,
12    pub depth: i16,
13}
14
15impl Display for WalkItem {
16    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
17        Display::fmt(&self.path.display(), f)
18    }
19}
20
21impl WalkItem {
22    pub fn new(raw: PathBuf) -> Self {
23        Self { path: raw, depth: 0 }
24    }
25    pub fn with_depth(self, depth: i16) -> Self {
26        Self { depth, ..self }
27    }
28    pub fn is_link(&self) -> bool {
29        self.path.is_symlink()
30    }
31    pub fn read_link(&self) -> std::io::Result<PathBuf> {
32        debug_assert!(self.path.is_symlink());
33        self.path.read_link()
34    }
35    pub fn is_directory(&self) -> bool {
36        self.path.is_dir()
37    }
38    pub fn read_directory(&self) -> std::io::Result<ReadDir> {
39        debug_assert!(self.path.is_dir());
40        self.path.read_dir()
41    }
42    pub fn is_file(&self) -> bool {
43        self.path.is_file()
44    }
45    pub fn read_file(&self) -> std::io::Result<File> {
46        debug_assert!(self.path.is_file());
47        File::open(&self.path)
48    }
49    pub fn read_file_string(&self) -> std::io::Result<String> {
50        debug_assert!(self.path.is_file());
51        std::fs::read_to_string(&self.path)
52    }
53}