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 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()) {

View file

@ -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};

View file

@ -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")]

View file

@ -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<String, Option<String>>| {
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<String, Option<String>>| {
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());
}