1use bincode::Error as BincodeError;
2use serde::{Deserialize, Serialize};
3use std::convert::From;
4use xorf::{Filter, HashProxy, Xor8};
5
6use std::collections::hash_map::DefaultHasher;
7
8type Title = String;
9type Url = String;
10type Meta = Option<String>;
11pub type PostId = (Title, Url, Meta);
12pub type PostFilter = (PostId, HashProxy<String, DefaultHasher, Xor8>);
13pub type Filters = Vec<PostFilter>;
14
15#[derive(Serialize, Deserialize)]
16pub struct Storage {
17 pub filters: Filters,
18}
19
20impl From<Filters> for Storage {
21 fn from(filters: Filters) -> Self {
22 Storage { filters }
23 }
24}
25
26pub trait Score {
27 fn score(&self, terms: &[String]) -> usize;
28}
29
30impl Score for HashProxy<String, DefaultHasher, Xor8> {
33 fn score(&self, terms: &[String]) -> usize {
34 terms.iter().filter(|term| self.contains(term)).count()
35 }
36}
37
38impl Storage {
39 pub fn to_bytes(&self) -> Result<Vec<u8>, BincodeError> {
40 let encoded: Vec<u8> = bincode::serialize(&self)?;
41 Ok(encoded)
42 }
43
44 pub fn from_bytes(bytes: &[u8]) -> Result<Self, BincodeError> {
45 let decoded: Filters = bincode::deserialize(bytes)?;
46 Ok(Storage { filters: decoded })
47 }
48}