From 9f5961c3a822bd4f17dd82882519ded8b5c6a16d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20F=C3=BCrderer?= Date: Tue, 7 May 2019 16:52:40 +0200 Subject: [PATCH] Store the date when each texture was added --- server/texture-sync-server/src/model/mod.rs | 42 +++++++++++++++++++ .../src/persistency/search/mod.rs | 20 ++++++++- .../src/protocol/implementation/connection.rs | 5 +++ 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/server/texture-sync-server/src/model/mod.rs b/server/texture-sync-server/src/model/mod.rs index 0f1851a..00d3037 100644 --- a/server/texture-sync-server/src/model/mod.rs +++ b/server/texture-sync-server/src/model/mod.rs @@ -3,6 +3,8 @@ #![allow(unused_variables)] #![allow(dead_code)] +use serde::de::Error; +use serde::{Deserialize, Deserializer, Serializer}; use std::io; mod sha256; @@ -11,16 +13,56 @@ pub use sha256::Sha256; mod texture_format; pub use texture_format::TextureFormat; +#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] +pub struct Date { + // Keep the order for correct comparison + pub year: u16, + pub month: u16, + pub day: u16, +} + #[derive(Eq, PartialEq, Clone, Serialize, Deserialize, Debug, Hash)] + pub struct Texture { pub id: String, pub name: String, pub tags: Vec, pub format: TextureFormat, pub resolution: (usize, usize), + #[serde( + serialize_with = "date_serialize", + deserialize_with = "date_deserialize" + )] + pub added_on: Date, pub texture_hash: Sha256, } +fn date_serialize(date: &Date, serializer: S) -> Result +where + S: Serializer, +{ + let vector = vec![date.year, date.month, date.day]; + serializer.serialize_some(&vector) +} + +fn date_deserialize<'d, D>(deserializer: D) -> Result +where + D: Deserializer<'d>, +{ + let data = Vec::::deserialize(deserializer)?; + if data.len() != 3 { + Err(D::Error::custom( + "expected an array with three elements to form a date", + )) + } else { + Ok(Date { + year: data[0], + month: data[1], + day: data[2], + }) + } +} + pub enum ReplaceTextureStatus { /// Done. Ok, diff --git a/server/texture-sync-server/src/persistency/search/mod.rs b/server/texture-sync-server/src/persistency/search/mod.rs index 1c5b5ec..412ae5a 100644 --- a/server/texture-sync-server/src/persistency/search/mod.rs +++ b/server/texture-sync-server/src/persistency/search/mod.rs @@ -3,8 +3,8 @@ #![allow(unused_variables)] #![allow(dead_code)] - use crate::model::*; +use std::cmp::Ordering; pub struct Query { filters: Vec, @@ -34,5 +34,21 @@ enum QueryFilter { TagName(String), InName(String), MinResolution(usize), - BeforeDate { year: u16, month: u16, day: u16 }, + BeforeDate(Date), +} + +impl QueryFilter { + fn matches(&self, tex: &Texture) -> bool { + match self { + QueryFilter::TagName(tag_name) => { + tex.tags.iter().find(|tag| tag == &tag_name).is_some() + } + QueryFilter::InName(name_part) => tex.name.contains(name_part), + QueryFilter::MinResolution(minimum) => { + let resolution = std::cmp::min(tex.resolution.0, tex.resolution.1); + &resolution >= minimum + } + QueryFilter::BeforeDate(date) => &tex.added_on < date, + } + } } diff --git a/server/texture-sync-server/src/protocol/implementation/connection.rs b/server/texture-sync-server/src/protocol/implementation/connection.rs index f885a0d..1579770 100644 --- a/server/texture-sync-server/src/protocol/implementation/connection.rs +++ b/server/texture-sync-server/src/protocol/implementation/connection.rs @@ -229,6 +229,11 @@ mod test { tags: vec!["Wood".to_string(), "Hair".to_string()], format: TextureFormat::PNG, resolution: (512, 512), + added_on: Date { + year: 2019, + month: 5, + day: 7, + }, texture_hash: Sha256(HASH_BYTES), } }