partialy implement Query, also add date
This commit is contained in:
@ -1,13 +1,7 @@
|
||||
// TODO: remove on implementation
|
||||
#![allow(unused_imports)]
|
||||
#![allow(unused_variables)]
|
||||
#![allow(dead_code)]
|
||||
|
||||
|
||||
use crate::model::*;
|
||||
|
||||
pub struct Query {
|
||||
filters: Vec<QueryFilterModifier>,
|
||||
filters: Vec<QueryFilter>,
|
||||
}
|
||||
|
||||
pub type QueryParserResult = Result<Query, QuerySyntaxError>;
|
||||
@ -17,6 +11,18 @@ pub enum QuerySyntaxError {
|
||||
|
||||
impl Query {
|
||||
pub fn parse(input: &[String]) -> QueryParserResult {
|
||||
let mut result = Query { filters: vec![] };
|
||||
|
||||
for fltr in input {
|
||||
let qryfltr = Self::parse_single(fltr)?;
|
||||
|
||||
result.filters.push(qryfltr);
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn parse_single(input: &str) -> Result<QueryFilter, QuerySyntaxError> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
@ -25,14 +31,60 @@ pub fn search(input: &[Texture], query: &Query) -> Vec<Texture> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
enum QueryFilterModifier {
|
||||
None(QueryFilter),
|
||||
Not(QueryFilter),
|
||||
enum QueryFilter {
|
||||
Not(Box<QueryFilter>),
|
||||
Tag(String),
|
||||
SpecialInName(String),
|
||||
SpecialBeforeDate(Date),
|
||||
SpecialAfterDate(Date),
|
||||
SpecialMinResolution(u64),
|
||||
}
|
||||
|
||||
enum QueryFilter {
|
||||
TagName(String),
|
||||
InName(String),
|
||||
MinResolution(usize),
|
||||
BeforeDate { year: u16, month: u16, day: u16 },
|
||||
enum Score {
|
||||
RequiredMatch(bool),
|
||||
Match(bool),
|
||||
}
|
||||
|
||||
use std::ops::Not;
|
||||
impl Not for Score {
|
||||
type Output = Score;
|
||||
|
||||
fn not(self) -> Score {
|
||||
match self {
|
||||
Score::RequiredMatch(b) => Score::RequiredMatch(!b),
|
||||
Score::Match(b) => Score::Match(!b),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl QueryFilter {
|
||||
pub fn score(&self, texture: &Texture) -> Score {
|
||||
use QueryFilter::*;
|
||||
match self {
|
||||
Not(inner) => inner.score(texture).not(),
|
||||
|
||||
Tag(tag) => Score::Match(
|
||||
texture
|
||||
.tags
|
||||
.iter()
|
||||
.find(|tt| tt.to_lowercase() == tag.to_lowercase())
|
||||
.is_some(),
|
||||
),
|
||||
|
||||
SpecialInName(name) => Score::RequiredMatch(
|
||||
//
|
||||
texture.name.contains(name),
|
||||
),
|
||||
|
||||
SpecialBeforeDate(date) => Score::RequiredMatch(texture.added_on <= *date),
|
||||
|
||||
SpecialAfterDate(date) => Score::RequiredMatch(texture.added_on > *date),
|
||||
|
||||
SpecialMinResolution(required) => {
|
||||
let smaller_resolution = u64::min(texture.resolution.0, texture.resolution.1);
|
||||
|
||||
Score::RequiredMatch(smaller_resolution >= *required)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user