Refactor hydrus page

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/6/head
trivernis 3 years ago
parent 5332ce66e8
commit 5ffae11044
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

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

@ -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>) -> String {
pub(crate) fn string_list_to_json_array(l: Vec<String>) -> String {
format!("[\"{}\"]", l.join("\",\""))
}
pub fn number_list_to_json_array<T: ToString>(l: Vec<T>) -> String {
pub(crate) fn number_list_to_json_array<T: ToString>(l: Vec<T>) -> String {
format!(
"[{}]",
l.into_iter()
@ -23,7 +24,7 @@ pub fn tag_list_to_string_list(tags: Vec<Tag>) -> Vec<String> {
tags.into_iter().map(|t| t.to_string()).collect()
}
pub fn format_datetime<D: Datelike>(datetime: D) -> String {
pub(crate) fn format_datetime<D: Datelike>(datetime: D) -> String {
format!(
"{:04}-{:02}-{:02}",
datetime.year(),
@ -32,7 +33,7 @@ pub fn format_datetime<D: Datelike>(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<FileIdentifier>,
) -> (Vec<u64>, Vec<String>) {
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)
}

@ -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<FileIdentifier>) -> 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() {
hashes.append(&mut self.resolve_file_ids_to_hashes(ids).await?);
self.client
.add_files_to_page(&self.key, [].to_vec(), hashes)
.await
}
async fn resolve_file_ids_to_hashes(&self, ids: Vec<u64>) -> Result<Vec<String>> {
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);
}
}
self.client.add_files_to_page(&self.key, ids, hashes).await
Ok(hashes)
}
}

Loading…
Cancel
Save