get sorting of query result right
This commit is contained in:
parent
10b775cdc9
commit
cde0238d62
|
@ -10,14 +10,17 @@ pub use date::Date;
|
|||
mod texture_format;
|
||||
pub use texture_format::TextureFormat;
|
||||
|
||||
#[derive(Eq, PartialEq, Clone, Serialize, Deserialize, Debug, Hash)]
|
||||
#[derive(Eq, PartialEq, PartialOrd, Ord, Clone, Serialize, Deserialize, Debug, Hash)]
|
||||
pub struct Texture {
|
||||
pub id: String,
|
||||
// Mainly sort by: (Ord is derived.)
|
||||
pub name: String,
|
||||
pub tags: Vec<String>,
|
||||
pub format: TextureFormat,
|
||||
pub added_on: Date,
|
||||
// other attributes
|
||||
pub id: String,
|
||||
pub tags: Vec<String>,
|
||||
pub resolution: (u64, u64),
|
||||
//
|
||||
pub format: TextureFormat,
|
||||
pub texture_hash: Sha256,
|
||||
}
|
||||
|
||||
|
@ -47,3 +50,30 @@ impl From<io::Error> for ProtocolError {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use std::str::FromStr;
|
||||
|
||||
/// just a shorthand
|
||||
fn tex(id: i32, name: &str, tags: &str, added_on: &str, resolution: u64) -> Texture {
|
||||
Texture {
|
||||
id: format!("{}", id), // Id should actaly be a uuid, but for testing this is fine.
|
||||
name: name.to_string(),
|
||||
tags: tags.split(",").map(|s| s.trim().to_string()).collect(),
|
||||
added_on: Date::from_str(added_on).unwrap(),
|
||||
resolution: (resolution, resolution),
|
||||
format: TextureFormat::JPEG,
|
||||
texture_hash: Sha256::from_data(b"Some Hash"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn textures_are_sorted_by_name() {
|
||||
let tex1 = tex(1, "AAAAA", "Z", "2019-05-15", 2);
|
||||
let tex2 = tex(0, "B", "A", "2000-01-11", 1);
|
||||
|
||||
assert!(tex1 < tex2);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize, Eq, Hash, PartialEq)]
|
||||
#[derive(Clone, Debug, PartialOrd, Ord, Deserialize, Serialize, Eq, Hash, PartialEq)]
|
||||
pub struct Sha256(#[serde(serialize_with = "as_hex", deserialize_with = "from_hex")] pub [u8; 32]);
|
||||
|
||||
fn hash_to_hex_string(hash: &[u8; 32]) -> String {
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize, Eq, Hash, PartialEq)]
|
||||
#[derive(Clone, Debug, Deserialize, Serialize, Eq, Hash, PartialEq, PartialOrd, Ord)]
|
||||
pub enum TextureFormat {
|
||||
#[serde(rename = "png")]
|
||||
PNG,
|
||||
|
|
|
@ -36,11 +36,13 @@ impl Query {
|
|||
}
|
||||
|
||||
if score >= required_score {
|
||||
results.push((score, texture))
|
||||
// multiply by -1 to get order right.
|
||||
results.push((score * -1, texture))
|
||||
}
|
||||
}
|
||||
|
||||
results.sort_by_key(|(score, _)| score * -1);
|
||||
// We can sort unstable, since at least the IDs are different.
|
||||
results.sort_unstable();
|
||||
|
||||
results
|
||||
.iter()
|
||||
|
|
Loading…
Reference in New Issue