diff options
author | Ben Bridle <bridle.benjamin@gmail.com> | 2022-08-25 21:27:39 +1200 |
---|---|---|
committer | Ben Bridle <bridle.benjamin@gmail.com> | 2022-08-25 21:27:39 +1200 |
commit | 8f410d1ead74b979481f1488a4dcddd33ea829c7 (patch) | |
tree | 2f22fd930c8d0cdb4de53fef473f7770073e14d5 /src/entry.rs | |
download | vagabond-79748a9b6c03b6d1926a765c2f0944ec2575f4cd.zip |
Initial commitv1.0
Diffstat (limited to 'src/entry.rs')
-rw-r--r-- | src/entry.rs | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/entry.rs b/src/entry.rs new file mode 100644 index 0000000..2679e42 --- /dev/null +++ b/src/entry.rs @@ -0,0 +1,86 @@ +use crate::EntryReadError; +use std::path::{Path, PathBuf}; + +#[derive(PartialEq)] +pub enum EntryType { + File, + Directory, +} + +pub struct Entry { + pub entry_type: EntryType, + pub is_symlink: bool, + pub name: String, + pub extension: String, + pub path: PathBuf, + pub original_path: PathBuf, +} +impl Entry { + pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self, EntryReadError> { + let path = path.as_ref(); + let metadata = path.metadata()?; + let canonical_path = std::fs::canonicalize(path)?; + let canonical_metadata = canonical_path.metadata()?; + let entry_type = if canonical_metadata.is_file() { + EntryType::File + } else if canonical_metadata.is_dir() { + EntryType::Directory + } else { + return Err(EntryReadError::NotFound); + }; + + let name = match path.file_name() { + Some(osstr) => osstr.to_string_lossy().to_string(), + None => unreachable!(), + }; + let extension = match path.extension() { + Some(extension) => extension.to_string_lossy().into(), + None => String::default(), + }; + Ok(Entry { + entry_type, + name, + extension, + path: canonical_path, + original_path: path.to_path_buf(), + is_symlink: metadata.is_symlink(), + }) + } + + /// Splits the filename on the last period, ignoring any period at the + /// start of the filename. If no extension is found, the extension is empty. + pub fn split_name(&self) -> (String, String) { + match self.name.rsplit_once(".") { + Some(("", _)) | None => (self.name.to_string(), String::new()), + Some((prefix, extension)) => (prefix.to_string(), extension.to_string()), + } + } + + pub fn is_file(&self) -> bool { + match self.entry_type { + EntryType::File => true, + _ => false, + } + } + + pub fn is_directory(&self) -> bool { + match self.entry_type { + EntryType::Directory => true, + _ => false, + } + } + + pub fn read_as_bytes(&self) -> Result<Vec<u8>, EntryReadError> { + return Ok(std::fs::read(&self.path)?); + } + + pub fn read_as_utf8_string(&self) -> Result<String, EntryReadError> { + return Ok(String::from_utf8_lossy(&self.read_as_bytes()?).to_string()); + } +} + +impl AsRef<Path> for Entry { + fn as_ref(&self) -> &Path { + &self.path + } +} |