diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-06-23 09:46:02 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-06-23 09:46:02 -0400 |
commit | 950e49980b5569ad53eaeba2e37587de7a085aa8 (patch) | |
tree | b6a90b503b10f397cb7fb59334551bd2d46c9a60 /src/io | |
parent | 1cfdc24fdfdf05605870c835b3bf72ea7d3268a1 (diff) |
rename io to fs
Diffstat (limited to 'src/io')
-rw-r--r-- | src/io/dirlist.rs | 145 | ||||
-rw-r--r-- | src/io/entry.rs | 74 | ||||
-rw-r--r-- | src/io/metadata.rs | 43 | ||||
-rw-r--r-- | src/io/mod.rs | 7 |
4 files changed, 0 insertions, 269 deletions
diff --git a/src/io/dirlist.rs b/src/io/dirlist.rs deleted file mode 100644 index 1b8d203..0000000 --- a/src/io/dirlist.rs +++ /dev/null @@ -1,145 +0,0 @@ -use std::{fs, path}; - -use crate::io::{JoshutoDirEntry, JoshutoMetadata}; -use crate::sort; -use crate::window::JoshutoPageState; - -#[derive(Debug)] -pub struct JoshutoDirList { - pub index: Option<usize>, - path: path::PathBuf, - outdated: bool, - pub metadata: JoshutoMetadata, - pub contents: Vec<JoshutoDirEntry>, - pub pagestate: JoshutoPageState, -} - -impl JoshutoDirList { - pub fn new( - path: path::PathBuf, - sort_option: &sort::SortOption, - ) -> Result<Self, std::io::Error> { - let mut contents = read_dir_list(path.as_path(), sort_option)?; - contents.sort_by(&sort_option.compare_func()); - - let index = if contents.is_empty() { None } else { Some(0) }; - - let metadata = JoshutoMetadata::from(&path)?; - let pagestate = JoshutoPageState::default(); - - Ok(JoshutoDirList { - index, - path, - outdated: false, - metadata, - contents, - pagestate, - }) - } - - pub fn depreciate(&mut self) { - self.outdated = true; - } - - pub fn need_update(&self) -> bool { - self.outdated - } - - pub fn file_path(&self) -> &path::PathBuf { - &self.path - } - - pub fn update_contents( - &mut self, - sort_option: &sort::SortOption, - ) -> Result<(), std::io::Error> { - let sort_func = sort_option.compare_func(); - let mut contents = read_dir_list(&self.path, sort_option)?; - contents.sort_by(&sort_func); - - let contents_len = contents.len(); - if contents_len == 0 { - self.index = None; - } else { - self.index = match self.index { - Some(index) => { - if index >= contents_len { - Some(contents_len - 1) - } else { - self.index - } - } - None => Some(0), - }; - } - - let metadata = JoshutoMetadata::from(&self.path)?; - self.metadata = metadata; - self.contents = contents; - self.outdated = false; - - Ok(()) - } - - pub fn selected_entries(&self) -> impl Iterator<Item = &JoshutoDirEntry> { - self.contents.iter().filter(|entry| entry.is_selected()) - } - - pub fn get_selected_paths(&self) -> Vec<&path::PathBuf> { - let vec: Vec<&path::PathBuf> = self.selected_entries().map(|e| e.file_path()).collect(); - if vec.is_empty() { - match self.get_curr_ref() { - Some(s) => vec![s.file_path()], - _ => vec![], - } - } else { - vec - } - } - - pub fn get_curr_ref(&self) -> Option<&JoshutoDirEntry> { - self.get_curr_ref_(self.index?) - } - - pub fn get_curr_mut(&mut self) -> Option<&mut JoshutoDirEntry> { - self.get_curr_mut_(self.index?) - } - - fn get_curr_mut_(&mut self, index: usize) -> Option<&mut JoshutoDirEntry> { - if index < self.contents.len() { - Some(&mut self.contents[index]) - } else { - None - } - } - - fn get_curr_ref_(&self, index: usize) -> Option<&JoshutoDirEntry> { - if index < self.contents.len() { - Some(&self.contents[index]) - } else { - None - } - } -} - -fn read_dir_list( - path: &path::Path, - sort_option: &sort::SortOption, -) -> Result<Vec<JoshutoDirEntry>, std::io::Error> { - let filter_func = sort_option.filter_func(); - let results: Vec<JoshutoDirEntry> = fs::read_dir(path)? - .filter(filter_func) - .filter_map(map_entry_default) - .collect(); - Ok(results) -} - -fn map_entry_default(result: Result<fs::DirEntry, std::io::Error>) -> Option<JoshutoDirEntry> { - match result { - Ok(direntry) => match JoshutoDirEntry::from(&direntry) { - Ok(s) => Some(s), - Err(_) => None, - }, - Err(_) => None, - } -} diff --git a/src/io/entry.rs b/src/io/entry.rs deleted file mode 100644 index 4c692dd..0000000 --- a/src/io/entry.rs +++ /dev/null @@ -1,74 +0,0 @@ -use std::{fs, io, path}; - -use crate::io::JoshutoMetadata; - -#[derive(Clone)] -pub struct JoshutoDirEntry { - name: String, - path: path::PathBuf, - pub metadata: JoshutoMetadata, - selected: bool, - marked: bool, -} - -impl JoshutoDirEntry { - pub fn from(direntry: &fs::DirEntry) -> Result<Self, io::Error> { - let name = match direntry.file_name().into_string() { - Ok(s) => s, - Err(_) => { - return Err(std::io::Error::new( - std::io::ErrorKind::Other, - "Failed converting OsString to String", - )) - } - }; - - let path = direntry.path(); - let metadata = JoshutoMetadata::from(&path)?; - - let dir_entry = JoshutoDirEntry { - name, - path, - metadata, - selected: false, - marked: false, - }; - Ok(dir_entry) - } - - pub fn file_name(&self) -> &str { - self.name.as_str() - } - - pub fn file_path(&self) -> &path::PathBuf { - &self.path - } - - /* - pub fn is_marked(&self) -> bool { - self.marked - } - - pub fn set_marked(&mut self, marked: bool) { - self.marked = marked; - } - */ - - pub fn is_selected(&self) -> bool { - self.selected - } - - pub fn set_selected(&mut self, selected: bool) { - self.selected = selected; - } -} - -impl std::fmt::Debug for JoshutoDirEntry { - fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { - write!( - f, - "JoshutoDirEntry {{\n\tfile_name: {:?}, \n\tpath: {:?} \n}}", - self.name, self.path - ) - } -} diff --git a/src/io/metadata.rs b/src/io/metadata.rs deleted file mode 100644 index fb85a6e..0000000 --- a/src/io/metadata.rs +++ /dev/null @@ -1,43 +0,0 @@ -use std::{fs, io, path, time}; - -#[derive(Clone, Debug)] -pub struct JoshutoMetadata { - pub len: u64, - pub modified: time::SystemTime, - pub permissions: fs::Permissions, - pub file_type: fs::FileType, - #[cfg(unix)] - pub uid: u32, - #[cfg(unix)] - pub gid: u32, -} - -impl JoshutoMetadata { - pub fn from(path: &path::Path) -> Result<Self, io::Error> { - #[cfg(unix)] - use std::os::unix::fs::MetadataExt; - - let metadata = fs::symlink_metadata(path)?; - - let len = metadata.len(); - let modified = metadata.modified()?; - let permissions = metadata.permissions(); - let file_type = metadata.file_type(); - - #[cfg(unix)] - let uid = metadata.uid(); - #[cfg(unix)] - let gid = metadata.gid(); - - Ok(JoshutoMetadata { - len, - modified, - permissions, - file_type, - #[cfg(unix)] - uid, - #[cfg(unix)] - gid, - }) - } -} diff --git a/src/io/mod.rs b/src/io/mod.rs deleted file mode 100644 index 632c2ff..0000000 --- a/src/io/mod.rs +++ /dev/null @@ -1,7 +0,0 @@ -mod dirlist; -mod entry; -mod metadata; - -pub use self::dirlist::JoshutoDirList; -pub use self::entry::JoshutoDirEntry; -pub use self::metadata::JoshutoMetadata; |