From 709f8cbf3c5146a0f53a6e9b6a4aeb3abd1aff35 Mon Sep 17 00:00:00 2001 From: Vika Date: Sat, 1 Jul 2023 20:40:38 +0300 Subject: micropub: use the new, better typed updates internally --- kittybox-rs/src/database/file/mod.rs | 58 +++++++++++------------------------- 1 file changed, 17 insertions(+), 41 deletions(-) (limited to 'kittybox-rs/src/database/file') diff --git a/kittybox-rs/src/database/file/mod.rs b/kittybox-rs/src/database/file/mod.rs index 3b373d8..0f63c9d 100644 --- a/kittybox-rs/src/database/file/mod.rs +++ b/kittybox-rs/src/database/file/mod.rs @@ -1,5 +1,6 @@ //#![warn(clippy::unwrap_used)] use crate::database::{filter_post, ErrorKind, Result, settings, Storage, StorageError}; +use crate::micropub::{MicropubUpdate, MicropubPropertyDeletion}; use async_trait::async_trait; use futures::{stream, StreamExt, TryStreamExt}; use serde_json::json; @@ -122,57 +123,32 @@ fn url_to_relative_path(url: &str) -> relative_path::RelativePathBuf { path } -fn modify_post(post: &serde_json::Value, update: &serde_json::Value) -> Result { +fn modify_post(post: &serde_json::Value, update: MicropubUpdate) -> Result { + let mut post = post.clone(); + let mut add_keys: HashMap> = HashMap::new(); let mut remove_keys: Vec = vec![]; let mut remove_values: HashMap> = HashMap::new(); - let mut post = post.clone(); - if let Some(delete) = update["delete"].as_array() { - remove_keys.extend( - delete - .iter() - .filter_map(|v| v.as_str()) - .map(|v| v.to_string()), - ); - } else if let Some(delete) = update["delete"].as_object() { + if let Some(MicropubPropertyDeletion::Properties(delete)) = update.delete { + remove_keys.extend(delete.iter().cloned()); + } else if let Some(MicropubPropertyDeletion::Values(delete)) = update.delete { for (k, v) in delete { - if let Some(v) = v.as_array() { - remove_values - .entry(k.to_string()) - .or_default() - .extend(v.clone()); - } else { - return Err(StorageError::from_static( - ErrorKind::BadRequest, - "Malformed update object", - )); - } + remove_values + .entry(k.to_string()) + .or_default() + .extend(v.clone()); } } - if let Some(add) = update["add"].as_object() { + if let Some(add) = update.add { for (k, v) in add { - if let Some(v) = v.as_array() { - add_keys.insert(k.to_string(), v.clone()); - } else { - return Err(StorageError::from_static( - ErrorKind::BadRequest, - "Malformed update object", - )); - } + add_keys.insert(k.to_string(), v.clone()); } } - if let Some(replace) = update["replace"].as_object() { + if let Some(replace) = update.replace { for (k, v) in replace { remove_keys.push(k.to_string()); - if let Some(v) = v.as_array() { - add_keys.insert(k.to_string(), v.clone()); - } else { - return Err(StorageError::from_static( - ErrorKind::BadRequest, - "Malformed update object", - )); - } + add_keys.insert(k.to_string(), v.clone()); } } @@ -446,7 +422,7 @@ impl Storage for FileStorage { } #[tracing::instrument(skip(self))] - async fn update_post(&self, url: &'_ str, update: serde_json::Value) -> Result<()> { + async fn update_post(&self, url: &str, update: MicropubUpdate) -> Result<()> { let path = url_to_path(&self.root_dir, url); let tempfilename = path.with_extension("tmp"); #[allow(unused_variables)] @@ -463,7 +439,7 @@ impl Storage for FileStorage { let json: serde_json::Value = serde_json::from_str(&content)?; drop(file); // Apply the editing algorithms - let new_json = modify_post(&json, &update)?; + let new_json = modify_post(&json, update)?; temp.write_all(new_json.to_string().as_bytes()).await?; temp.flush().await?; -- cgit 1.4.1