From 5ffae110442b8d99f85ae12b041832e15d48a333 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 14 Aug 2021 12:36:01 +0200 Subject: [PATCH] Refactor hydrus page Signed-off-by: trivernis --- src/lib.rs | 2 +- src/utils.rs | 24 ++++++++++++++++++++---- src/wrapper/page.rs | 36 ++++++++++++++++++------------------ 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index cbf98e5..e42b011 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,5 +74,5 @@ pub use wrapper::hydrus::Hydrus; pub mod api_core; pub mod error; -pub(crate) mod utils; +pub mod utils; pub mod wrapper; diff --git a/src/utils.rs b/src/utils.rs index e615d37..2fa9107 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,11 +1,12 @@ +use crate::api_core::common::FileIdentifier; use crate::wrapper::tag::Tag; use chrono::{Datelike, Duration}; -pub fn string_list_to_json_array(l: Vec) -> String { +pub(crate) fn string_list_to_json_array(l: Vec) -> String { format!("[\"{}\"]", l.join("\",\"")) } -pub fn number_list_to_json_array(l: Vec) -> String { +pub(crate) fn number_list_to_json_array(l: Vec) -> String { format!( "[{}]", l.into_iter() @@ -23,7 +24,7 @@ pub fn tag_list_to_string_list(tags: Vec) -> Vec { tags.into_iter().map(|t| t.to_string()).collect() } -pub fn format_datetime(datetime: D) -> String { +pub(crate) fn format_datetime(datetime: D) -> String { format!( "{:04}-{:02}-{:02}", datetime.year(), @@ -32,7 +33,7 @@ pub fn format_datetime(datetime: D) -> String { ) } -pub fn format_duration(duration: Duration) -> String { +pub(crate) fn format_duration(duration: Duration) -> String { let mut expression = String::new(); let days = duration.num_days(); let hours = duration.num_hours() % 24; @@ -56,3 +57,18 @@ pub fn format_duration(duration: Duration) -> String { expression } + +pub(crate) fn split_file_identifiers_into_hashes_and_ids( + files: Vec, +) -> (Vec, Vec) { + let mut ids = Vec::new(); + let mut hashes = Vec::new(); + + for file in files { + match file { + FileIdentifier::ID(id) => ids.push(id), + FileIdentifier::Hash(hash) => hashes.push(hash), + } + } + (ids, hashes) +} diff --git a/src/wrapper/page.rs b/src/wrapper/page.rs index 93669a2..48f37ea 100644 --- a/src/wrapper/page.rs +++ b/src/wrapper/page.rs @@ -1,5 +1,6 @@ use crate::api_core::common::{FileIdentifier, PageInformation}; use crate::error::Result; +use crate::utils::split_file_identifiers_into_hashes_and_ids; use crate::Client; #[derive(Clone)] @@ -40,27 +41,26 @@ impl HydrusPage { /// Adds files to a page pub async fn add_files(&self, files: Vec) -> Result<()> { - let mut hashes = Vec::new(); - let mut ids = Vec::new(); + let (ids, mut hashes) = split_file_identifiers_into_hashes_and_ids(files); - for file in files { - match file { - FileIdentifier::ID(id) => ids.push(id), - FileIdentifier::Hash(hash) => hashes.push(hash), - } - } // resolve file ids to hashes - if ids.len() > 0 && hashes.len() > 0 { - while let Some(id) = ids.pop() { - let metadata = self - .client - .get_file_metadata_by_identifier(FileIdentifier::ID(id)) - .await?; - hashes.push(metadata.hash); - } - } + hashes.append(&mut self.resolve_file_ids_to_hashes(ids).await?); - self.client.add_files_to_page(&self.key, ids, hashes).await + self.client + .add_files_to_page(&self.key, [].to_vec(), hashes) + .await + } + + async fn resolve_file_ids_to_hashes(&self, ids: Vec) -> Result> { + let mut hashes = Vec::new(); + for id in ids { + let metadata = self + .client + .get_file_metadata_by_identifier(FileIdentifier::ID(id)) + .await?; + hashes.push(metadata.hash); + } + Ok(hashes) } }