Store the date when each texture was added

This commit is contained in:
Lukas Fürderer 2019-05-07 16:52:40 +02:00
parent cfc2ff5886
commit 9f5961c3a8
3 changed files with 65 additions and 2 deletions

View File

@ -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<String>,
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<S>(date: &Date, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let vector = vec![date.year, date.month, date.day];
serializer.serialize_some(&vector)
}
fn date_deserialize<'d, D>(deserializer: D) -> Result<Date, D::Error>
where
D: Deserializer<'d>,
{
let data = Vec::<u16>::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,

View File

@ -3,8 +3,8 @@
#![allow(unused_variables)]
#![allow(dead_code)]
use crate::model::*;
use std::cmp::Ordering;
pub struct Query {
filters: Vec<QueryFilterModifier>,
@ -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,
}
}
}

View File

@ -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),
}
}