diff --git a/src/editor/commands/editor.rs b/src/editor/commands/editor.rs index b054738..1a2f03c 100644 --- a/src/editor/commands/editor.rs +++ b/src/editor/commands/editor.rs @@ -8,7 +8,12 @@ use crate::schema::resource::Resource; use std::path::PathBuf; use tracing::{error, info}; -pub fn editor(database_path: PathBuf, save_settings: SaveSettings, resource_lookup: String, create_new_resource: bool) { +pub fn editor( + database_path: PathBuf, + save_settings: SaveSettings, + resource_lookup: String, + create_new_resource: bool, +) { let resources = LookupHandler::load(&database_path).unwrap(); let (index, resource) = match resources.lookup_with_index(resource_lookup.as_str()) { diff --git a/src/editor/open_in_editor.rs b/src/editor/open_in_editor.rs index 75a90ed..374aa7a 100644 --- a/src/editor/open_in_editor.rs +++ b/src/editor/open_in_editor.rs @@ -1,4 +1,4 @@ -//! Utilities for opening a string in the system-defined editor +//! Utilities for opening a string in the system-defined editor use crate::schema::resource::Resource; use std::fmt::{Debug, Display}; diff --git a/src/main.rs b/src/main.rs index 9f6ea47..fb0d139 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,14 +8,8 @@ mod args_parser; mod editor; #[cfg(feature = "editor")] use editor::{ + commands::{editor::editor, fetch::fetch, init::init, list::list, query::query}, try_reload_server::reload, - commands::{ - editor::editor, - init::init, - list::list, - fetch::fetch, - query::query - } }; mod schema; #[cfg(feature = "server")] diff --git a/src/schema/resource.rs b/src/schema/resource.rs index ed77adc..babc1b5 100644 --- a/src/schema/resource.rs +++ b/src/schema/resource.rs @@ -80,27 +80,45 @@ impl Resource { /// and instead replaced with `Some(Default::default())`. Useful to provide an easier to /// extend version of the data for manual editing. pub fn as_completely_serializable(&self) -> Self { - let clone_hashmap_with_option_value_as_complete = |props: &HashMap>| { - HashMap::from_iter(props.iter().map(|(key, value)| { - (key.clone(), Some(value.clone().unwrap_or_default())) - })) - }; + let clone_hashmap_with_option_value_as_complete = + |props: &HashMap>| { + HashMap::from_iter( + props + .iter() + .map(|(key, value)| (key.clone(), Some(value.clone().unwrap_or_default()))), + ) + }; Self { subject: self.subject.clone(), aliases: Some(self.aliases.clone().unwrap_or_default()), - properties: Some(self.properties.as_ref().map(clone_hashmap_with_option_value_as_complete).unwrap_or_default()), - links: Some(self.links.as_ref().map(|links| { - Vec::from_iter(links.iter().map(|link| { - Link { - rel: link.rel.clone(), - media_type: Some(link.media_type.clone().unwrap_or_default()), - href: Some(link.href.clone().unwrap_or_default()), - titles: Some(link.titles.clone().unwrap_or_default()), - properties: Some(link.properties.as_ref().map(clone_hashmap_with_option_value_as_complete).unwrap_or_default()), - } - })) - }).unwrap_or_default()), + properties: Some( + self.properties + .as_ref() + .map(clone_hashmap_with_option_value_as_complete) + .unwrap_or_default(), + ), + links: Some( + self.links + .as_ref() + .map(|links| { + Vec::from_iter(links.iter().map(|link| { + Link { + rel: link.rel.clone(), + media_type: Some(link.media_type.clone().unwrap_or_default()), + href: Some(link.href.clone().unwrap_or_default()), + titles: Some(link.titles.clone().unwrap_or_default()), + properties: Some( + link.properties + .as_ref() + .map(clone_hashmap_with_option_value_as_complete) + .unwrap_or_default(), + ), + } + })) + }) + .unwrap_or_default(), + ), } } @@ -111,32 +129,50 @@ impl Resource { Self { subject: self.subject, aliases: self.aliases.filter(|data| !data.is_empty()), - properties: self.properties.filter(|data| !data.is_empty()).map(|mut data| { - for value in data.values_mut() { - if let Some(ref mut string) = value { - if string.is_empty() { - *value = None; + properties: self + .properties + .filter(|data| !data.is_empty()) + .map(|mut data| { + for value in data.values_mut() { + if let Some(ref mut string) = value { + if string.is_empty() { + *value = None; + } } } - } - data - }), - links: self.links.filter(|links| !links.is_empty()).map(|mut links| { - links.retain(|link| { - // Empty `rel` is invalid, but short-circuiting here would delete records - // that are only partially edited. Better to store invalid data than to delete - // users' work. - let mut is_default = link.rel.is_empty(); - is_default &= link.media_type.as_ref().filter(|media_type| !media_type.is_empty()).is_none(); - is_default &= link.href.as_ref().filter(|href| !href.is_empty()).is_none(); - is_default &= link.titles.as_ref().filter(|titles| !titles.is_empty()).is_none(); - is_default &= link.properties.as_ref().filter(|titles| !titles.is_empty()).is_none(); + data + }), + links: self + .links + .filter(|links| !links.is_empty()) + .map(|mut links| { + links.retain(|link| { + // Empty `rel` is invalid, but short-circuiting here would delete records + // that are only partially edited. Better to store invalid data than to delete + // users' work. + let mut is_default = link.rel.is_empty(); + is_default &= link + .media_type + .as_ref() + .filter(|media_type| !media_type.is_empty()) + .is_none(); + is_default &= link.href.as_ref().filter(|href| !href.is_empty()).is_none(); + is_default &= link + .titles + .as_ref() + .filter(|titles| !titles.is_empty()) + .is_none(); + is_default &= link + .properties + .as_ref() + .filter(|titles| !titles.is_empty()) + .is_none(); - is_default - }); + is_default + }); - links - }) + links + }), } } } @@ -251,7 +287,7 @@ mod tests { for data in [ test_data::barebones_user(), test_data::user_with_matching_subject_and_alias(), - test_data::user_with_single_alias() + test_data::user_with_single_alias(), ] { assert_eq!(data, data.as_completely_serializable().compress()); }