Store the date when each texture was added

This commit is contained in:
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,