pak_db/
index.rs

1use std::collections::HashMap;
2use serde::{Deserialize, Serialize};
3use crate::{group::DeserializeGroup, pointer::PakUntypedPointer, query::PakQuery, value::IntoPakValue};
4
5use super::value::PakValue;
6
7pub type PakIndices = HashMap<PakValue, Vec<PakUntypedPointer>>;
8
9//==============================================================================================
10//        PakItem Traits
11//==============================================================================================
12
13pub trait PakSearchable {
14    fn get_indices(&self, indices : &mut Indices);
15}
16
17#[derive(Default)]
18pub struct Indices(Vec<PakIndex>);
19
20impl Indices {
21    pub fn add<I, V>(&mut self, key : I, value : V) where I : PakIndexIdentifier, V : IntoPakValue {
22        self.0.push(PakIndex::new(key, value));
23    }
24    
25    pub(crate) fn unwrap(self) -> Vec<PakIndex> {
26        self.0
27    }
28}
29
30//==============================================================================================
31//        PakIndex
32//==============================================================================================
33
34#[derive(PartialEq, Debug, Clone, PartialOrd, Deserialize, Serialize)]
35pub(crate) struct PakIndex {
36    pub key : String,
37    pub value : PakValue
38}
39
40impl PakIndex {
41    pub fn new<I, V>(key : I, value : V) -> Self where I : PakIndexIdentifier, V : IntoPakValue {
42        Self {
43            key: key.identifier().to_string(),
44            value: value.into_pak_value(),
45        }
46    }
47}
48
49//==============================================================================================
50//        PakIndexIdentifier
51//==============================================================================================
52
53pub trait PakIndexIdentifier {
54    fn identifier(&self) -> &str;
55    
56    fn equals<T, V>(&self, other: V) -> PakQuery<T> where T : DeserializeGroup, V : IntoPakValue {
57        PakQuery::equals(self.identifier(), other.into_pak_value())
58    }
59    
60    fn less_than<T, V>(&self, other: V) -> PakQuery<T> where T : DeserializeGroup, V : IntoPakValue {
61        PakQuery::less_than(self.identifier(), other.into_pak_value())
62    }
63    
64    fn greater_than<T, V>(&self, other: V) -> PakQuery<T> where T : DeserializeGroup, V : IntoPakValue {
65        PakQuery::greater_than(self.identifier(), other.into_pak_value())
66    }
67    
68    fn greater_than_or_equal<T, V>(&self, other: V) -> PakQuery<T> where T : DeserializeGroup, V : IntoPakValue {
69        PakQuery::greater_than_or_equal(self.identifier(), other.into_pak_value())
70    }
71    
72    fn less_than_or_equal<T, V>(&self, other: V) -> PakQuery<T> where T : DeserializeGroup, V : IntoPakValue {
73        PakQuery::less_than_or_equal(self.identifier(), other.into_pak_value())
74    }
75    
76    fn contains_value<T, V>(&self, other : V) -> PakQuery<T> where T : DeserializeGroup, V : IntoPakValue {
77        PakQuery::contains(self.identifier(), other.into_pak_value())
78    }
79}
80
81impl PakIndexIdentifier for String {
82    fn identifier(&self) -> &str {
83        self
84    }
85}
86
87impl <'id> PakIndexIdentifier for &'id str {
88    fn identifier(&self) -> &str {
89        self
90    }
91}