Store the date when each texture was added
This commit is contained in:
parent
cfc2ff5886
commit
9f5961c3a8
@ -3,6 +3,8 @@
|
|||||||
#![allow(unused_variables)]
|
#![allow(unused_variables)]
|
||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
use serde::de::Error;
|
||||||
|
use serde::{Deserialize, Deserializer, Serializer};
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
mod sha256;
|
mod sha256;
|
||||||
@ -11,16 +13,56 @@ pub use sha256::Sha256;
|
|||||||
mod texture_format;
|
mod texture_format;
|
||||||
pub use texture_format::TextureFormat;
|
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)]
|
#[derive(Eq, PartialEq, Clone, Serialize, Deserialize, Debug, Hash)]
|
||||||
|
|
||||||
pub struct Texture {
|
pub struct Texture {
|
||||||
pub id: String,
|
pub id: String,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub tags: Vec<String>,
|
pub tags: Vec<String>,
|
||||||
pub format: TextureFormat,
|
pub format: TextureFormat,
|
||||||
pub resolution: (usize, usize),
|
pub resolution: (usize, usize),
|
||||||
|
#[serde(
|
||||||
|
serialize_with = "date_serialize",
|
||||||
|
deserialize_with = "date_deserialize"
|
||||||
|
)]
|
||||||
|
pub added_on: Date,
|
||||||
pub texture_hash: Sha256,
|
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 {
|
pub enum ReplaceTextureStatus {
|
||||||
/// Done.
|
/// Done.
|
||||||
Ok,
|
Ok,
|
||||||
|
@ -3,8 +3,8 @@
|
|||||||
#![allow(unused_variables)]
|
#![allow(unused_variables)]
|
||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
|
||||||
use crate::model::*;
|
use crate::model::*;
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
pub struct Query {
|
pub struct Query {
|
||||||
filters: Vec<QueryFilterModifier>,
|
filters: Vec<QueryFilterModifier>,
|
||||||
@ -34,5 +34,21 @@ enum QueryFilter {
|
|||||||
TagName(String),
|
TagName(String),
|
||||||
InName(String),
|
InName(String),
|
||||||
MinResolution(usize),
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -229,6 +229,11 @@ mod test {
|
|||||||
tags: vec!["Wood".to_string(), "Hair".to_string()],
|
tags: vec!["Wood".to_string(), "Hair".to_string()],
|
||||||
format: TextureFormat::PNG,
|
format: TextureFormat::PNG,
|
||||||
resolution: (512, 512),
|
resolution: (512, 512),
|
||||||
|
added_on: Date {
|
||||||
|
year: 2019,
|
||||||
|
month: 5,
|
||||||
|
day: 7,
|
||||||
|
},
|
||||||
texture_hash: Sha256(HASH_BYTES),
|
texture_hash: Sha256(HASH_BYTES),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user