This commit is contained in:
Karcsesz 2024-03-17 21:58:04 +01:00
parent 474b4cafab
commit 61a3c10377
4 changed files with 84 additions and 49 deletions

View file

@ -8,7 +8,12 @@ use crate::schema::resource::Resource;
use std::path::PathBuf; use std::path::PathBuf;
use tracing::{error, info}; 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 resources = LookupHandler::load(&database_path).unwrap();
let (index, resource) = match resources.lookup_with_index(resource_lookup.as_str()) { let (index, resource) = match resources.lookup_with_index(resource_lookup.as_str()) {

View file

@ -8,14 +8,8 @@ mod args_parser;
mod editor; mod editor;
#[cfg(feature = "editor")] #[cfg(feature = "editor")]
use editor::{ use editor::{
commands::{editor::editor, fetch::fetch, init::init, list::list, query::query},
try_reload_server::reload, try_reload_server::reload,
commands::{
editor::editor,
init::init,
list::list,
fetch::fetch,
query::query
}
}; };
mod schema; mod schema;
#[cfg(feature = "server")] #[cfg(feature = "server")]

View file

@ -80,27 +80,45 @@ impl Resource {
/// and instead replaced with `Some(Default::default())`. Useful to provide an easier to /// and instead replaced with `Some(Default::default())`. Useful to provide an easier to
/// extend version of the data for manual editing. /// extend version of the data for manual editing.
pub fn as_completely_serializable(&self) -> Self { pub fn as_completely_serializable(&self) -> Self {
let clone_hashmap_with_option_value_as_complete = |props: &HashMap<String, Option<String>>| { let clone_hashmap_with_option_value_as_complete =
HashMap::from_iter(props.iter().map(|(key, value)| { |props: &HashMap<String, Option<String>>| {
(key.clone(), Some(value.clone().unwrap_or_default())) HashMap::from_iter(
})) props
.iter()
.map(|(key, value)| (key.clone(), Some(value.clone().unwrap_or_default()))),
)
}; };
Self { Self {
subject: self.subject.clone(), subject: self.subject.clone(),
aliases: Some(self.aliases.clone().unwrap_or_default()), 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()), properties: Some(
links: Some(self.links.as_ref().map(|links| { 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| { Vec::from_iter(links.iter().map(|link| {
Link { Link {
rel: link.rel.clone(), rel: link.rel.clone(),
media_type: Some(link.media_type.clone().unwrap_or_default()), media_type: Some(link.media_type.clone().unwrap_or_default()),
href: Some(link.href.clone().unwrap_or_default()), href: Some(link.href.clone().unwrap_or_default()),
titles: Some(link.titles.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()), properties: Some(
link.properties
.as_ref()
.map(clone_hashmap_with_option_value_as_complete)
.unwrap_or_default(),
),
} }
})) }))
}).unwrap_or_default()), })
.unwrap_or_default(),
),
} }
} }
@ -111,7 +129,10 @@ impl Resource {
Self { Self {
subject: self.subject, subject: self.subject,
aliases: self.aliases.filter(|data| !data.is_empty()), aliases: self.aliases.filter(|data| !data.is_empty()),
properties: self.properties.filter(|data| !data.is_empty()).map(|mut data| { properties: self
.properties
.filter(|data| !data.is_empty())
.map(|mut data| {
for value in data.values_mut() { for value in data.values_mut() {
if let Some(ref mut string) = value { if let Some(ref mut string) = value {
if string.is_empty() { if string.is_empty() {
@ -121,22 +142,37 @@ impl Resource {
} }
data data
}), }),
links: self.links.filter(|links| !links.is_empty()).map(|mut links| { links: self
.links
.filter(|links| !links.is_empty())
.map(|mut links| {
links.retain(|link| { links.retain(|link| {
// Empty `rel` is invalid, but short-circuiting here would delete records // Empty `rel` is invalid, but short-circuiting here would delete records
// that are only partially edited. Better to store invalid data than to delete // that are only partially edited. Better to store invalid data than to delete
// users' work. // users' work.
let mut is_default = link.rel.is_empty(); 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
.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.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
is_default &= link.properties.as_ref().filter(|titles| !titles.is_empty()).is_none(); .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 [ for data in [
test_data::barebones_user(), test_data::barebones_user(),
test_data::user_with_matching_subject_and_alias(), 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()); assert_eq!(data, data.as_completely_serializable().compress());
} }