From 975a0e68d78846cffd4ced49a9f0eb56ed29c876 Mon Sep 17 00:00:00 2001 From: hacknus Date: Tue, 26 Nov 2024 23:01:43 +0100 Subject: [PATCH] create metadata struct --- .../src/main.rs | 4 +- src/data/directory_content.rs | 68 ++++++++----------- src/data/information_panel.rs | 14 ++-- 3 files changed, 36 insertions(+), 50 deletions(-) diff --git a/examples/select_file_with_information_view/src/main.rs b/examples/select_file_with_information_view/src/main.rs index 15d65a44..9c134c88 100644 --- a/examples/select_file_with_information_view/src/main.rs +++ b/examples/select_file_with_information_view/src/main.rs @@ -17,12 +17,12 @@ impl MyApp { information_panel: InformationPanel::default() .add_file_preview("csv", |ui, item| { ui.label("CSV preview:"); - if let Some(content) = item.content() { + if let Some(mut content) = item.content() { egui::ScrollArea::vertical() .max_height(150.0) .show(ui, |ui| { ui.add( - egui::TextEdit::multiline(&mut content.clone()).code_editor(), + egui::TextEdit::multiline(&mut content).code_editor(), ); }); } diff --git a/src/data/directory_content.rs b/src/data/directory_content.rs index f211ccc3..8fe91a30 100644 --- a/src/data/directory_content.rs +++ b/src/data/directory_content.rs @@ -18,17 +18,24 @@ pub fn format_pixels(pixels: u32) -> String { } /// Contains the metadata of a directory item. +#[derive(Debug, Default, Clone)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +pub struct Metadata { + pub size: Option, + pub last_modified: Option, + pub created: Option, + pub file_type: Option, +} + +/// Contains the information of a directory item. /// -/// This struct is mainly there so that the metadata can be loaded once and not that +/// This struct is mainly there so that the information and metadata can be loaded once and not that /// a request has to be sent to the OS every frame using, for example, `path.is_file()`. #[derive(Debug, Default, Clone)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub struct DirectoryEntry { path: PathBuf, - size: Option, - last_modified: Option, - created: Option, - file_type: Option, + metadata: Metadata, is_directory: bool, is_system_file: bool, #[cfg(feature = "metadata_view")] @@ -41,24 +48,18 @@ pub struct DirectoryEntry { impl DirectoryEntry { /// Creates a new directory entry from a path pub fn from_path(config: &FileDialogConfig, path: &Path) -> Self { - let mut size = None; - let mut last_modified = None; - let mut created = None; - let mut file_type = None; - - if let Ok(metadata) = fs::metadata(path) { - size = Some(metadata.len()); - last_modified = metadata.modified().ok(); - created = metadata.created().ok(); - file_type = Some(format!("{:?}", metadata.file_type())); + let mut metadata = Metadata::default(); + + if let Ok(md) = fs::metadata(path) { + metadata.size = Some(md.len()); + metadata.last_modified = md.modified().ok(); + metadata.created = md.created().ok(); + metadata.file_type = Some(format!("{:?}", md.file_type())); } Self { path: path.to_path_buf(), - size, - last_modified, - created, - file_type, + metadata, is_directory: path.is_dir(), is_system_file: !path.is_dir() && !path.is_file(), #[cfg(feature = "metadata_view")] @@ -68,6 +69,11 @@ impl DirectoryEntry { } } + /// Returns the metadata of the directory entry. + pub const fn metadata(&self) -> &Metadata { + &self.metadata + } + /// Checks if the path of the current directory entry matches the other directory entry. pub fn path_eq(&self, other: &Self) -> bool { other.as_path() == self.as_path() @@ -107,26 +113,6 @@ impl DirectoryEntry { self.path.clone() } - /// Returns the size of the directory item. - pub const fn size(&self) -> Option { - self.size - } - - /// Clones the `FileType` struct of the directory item. - pub fn file_type(&self) -> Option { - self.file_type.clone() - } - - /// Returns the created date of the directory item. - pub const fn created(&self) -> Option { - self.created - } - - /// Returns the last modified date of the directory item. - pub const fn last_modified(&self) -> Option { - self.last_modified - } - /// Returns the file name of the directory item. pub fn file_name(&self) -> &str { self.path @@ -171,8 +157,8 @@ impl DirectoryEntry { #[cfg(feature = "metadata_view")] impl DirectoryEntry { /// Clones the content of the directory item, if available - pub fn content(&self) -> Option { - self.content.clone() + pub fn content(&self) -> Option<&str> { + self.content.as_deref() } /// Sets the content of the directory item diff --git a/src/data/information_panel.rs b/src/data/information_panel.rs index 85ddde66..0f27794e 100644 --- a/src/data/information_panel.rs +++ b/src/data/information_panel.rs @@ -70,12 +70,12 @@ impl Default for InformationPanel { supported_files.insert( text_extension.to_string(), Box::new(|ui: &mut Ui, item: &DirectoryEntry| { - if let Some(content) = item.content() { + if let Some(mut content) = item.content() { egui::ScrollArea::vertical() .max_height(100.0) .show(ui, |ui| { ui.add( - egui::TextEdit::multiline(&mut content.clone()).code_editor(), + egui::TextEdit::multiline(&mut content).code_editor(), ); }); } @@ -232,12 +232,12 @@ impl InformationPanel { self.supported_preview_files.get_mut(&ext.to_lowercase()) { show_preview(ui, item); - } else if let Some(content) = item.content() { + } else if let Some(mut content) = item.content() { egui::ScrollArea::vertical() .max_height(100.0) .show(ui, |ui| { ui.add( - egui::TextEdit::multiline(&mut content.clone()).code_editor(), + egui::TextEdit::multiline(&mut content).code_editor(), ); }); } else { @@ -268,7 +268,7 @@ impl InformationPanel { ui.label(item.file_name().to_string()); ui.end_row(); - if let Some(size) = item.size() { + if let Some(size) = item.metadata().size { ui.label("File Size: "); if item.is_file() { ui.label(format_bytes(size)); @@ -278,14 +278,14 @@ impl InformationPanel { ui.end_row(); } - if let Some(date) = item.created() { + if let Some(date) = item.metadata().created { ui.label("Created: "); let created: DateTime = date.into(); ui.label(format!("{}", created.format("%Y-%m-%d %H:%M:%S"))); ui.end_row(); } - if let Some(date) = item.last_modified() { + if let Some(date) = item.metadata().last_modified { ui.label("Last Modified: "); let modified: DateTime = date.into(); ui.label(format!("{}", modified.format("%Y-%m-%d %H:%M:%S")));