diff options
author | rabite <rabite@posteo.de> | 2019-01-21 14:44:34 +0100 |
---|---|---|
committer | rabite <rabite@posteo.de> | 2019-01-21 15:02:14 +0100 |
commit | 67c973c0af1d35f0f832d4eb594c12868ef78008 (patch) | |
tree | efa11d04f19aec6bd269c931b0ce6a54e42a9371 /src/files.rs |
First commit, did some refactoring around widgets, etc, etc
Diffstat (limited to 'src/files.rs')
-rw-r--r-- | src/files.rs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/files.rs b/src/files.rs new file mode 100644 index 0000000..a35a4b1 --- /dev/null +++ b/src/files.rs @@ -0,0 +1,62 @@ +use std::ops::Index; + +pub struct Files(Vec<File>); + +impl Index<usize> for Files { + type Output = File; + fn index(&self, pos: usize) -> &Self::Output { + &self.0[pos] + } +} + +impl Files { + pub fn iter(&self) -> std::slice::Iter<File> { + self.0.iter() + } + + pub fn len(&self) -> usize { + self.0.len() + } +} + +#[derive(Debug)] +pub struct File { + pub name: String, + pub path: String, + pub size: Option<usize>, + // owner: Option<String>, + // group: Option<String>, + // flags: Option<String>, + // ctime: Option<u32>, + // mtime: Option<u32>, +} + + +impl File { + pub fn new(name: &str, path: &str, size: usize) -> File { + File { + name: name.to_string(), + path: path.to_string(), + size: Some(size), + // owner: None, + // group: None, + // flags: None, + // ctime: None, + // mtime: None + } + } +} + +pub fn get_files(dir: &str) -> Result<Files, std::io::Error> { + let mut files = Vec::new(); + for file in std::fs::read_dir(dir)? { + let name = file.as_ref().unwrap().file_name().into_string().unwrap(); + file.as_ref().unwrap().path().pop(); + let path = file.as_ref().unwrap().path().into_os_string().into_string().unwrap(); + let size = file.unwrap().metadata()?.len() / 1024; + files.push(File::new(&name, &path, size as usize)); + } + Ok(Files(files)) +} + + |