1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use std::{collections::HashMap, fmt::Debug};
use crate::Event;
#[derive(Clone, Debug)]
enum FiltersContext {
Contract,
Chain,
MultiChain,
}
/// Represents a set of filters used for querying data.
#[derive(Clone, Debug)]
pub struct Filters {
values: HashMap<String, String>, // A map of filter field names to their values.
context: FiltersContext, // The context in which the filters are applied.
}
impl Filters {
/// Creates a new Filters instance with a single filter.
///
/// # Arguments
///
/// * `field` - The field name of the filter.
/// * `value` - The value of the filter.
///
/// # Example
///
/// ```ignore
/// let filters = Filters::new("token_id", token_id);
/// Nft::read_one(&filters, &context);
/// ```
pub fn new(field: impl ToString, value: impl ToString) -> Self {
Self {
values: HashMap::from([(field.to_string(), value.to_string())]),
context: FiltersContext::Contract,
}
}
/// Adds a new filter to the existing set of filters by moving the
/// original filters
///
/// # Arguments
///
/// * `field` - The field name of the filter.
/// * `value` - The value of the filter.
///
/// # Example
///
/// ```ignore
/// Filters::new("address", address).add("token_id", token_id); // filters is moved
/// ```
pub fn add(mut self, field: impl ToString, value: impl ToString) -> Self {
self.add_mut(field, value);
self
}
/// Adds a new filter to the existing set of filters without moving
/// the original filters
///
/// # Arguments
///
/// * `field` - The field name of the filter.
/// * `value` - The value of the filter.
///
/// # Example
///
/// ```ignore
/// let mut filters = Filters::new("address", address);
///
/// filters.add_mut("token_id", token_id); // filters not moved
/// ```
pub fn add_mut(&mut self, field: impl ToString, value: impl ToString) {
self.values.insert(field.to_string(), value.to_string());
}
/// Sets the context of the filters to Contract
pub fn within_contract(mut self) -> Self {
self.context = FiltersContext::Contract;
self
}
/// Sets the context of the filters to Chain
pub fn within_chain(mut self) -> Self {
self.context = FiltersContext::Chain;
self
}
/// Sets the context of the filters to MultiChain
pub fn within_multi_chain(mut self) -> Self {
self.context = FiltersContext::MultiChain;
self
}
pub(super) fn get(&self, event: &Event) -> HashMap<String, String> {
let mut filters = self.values.clone();
match self.context {
FiltersContext::Contract => {
filters.insert("chain_id".to_string(), event.chain_id.to_string());
filters.insert(
"contract_address".to_string(),
event.contract_address.to_owned(),
);
}
FiltersContext::Chain => {
filters.insert("chain_id".to_string(), event.chain_id.to_string());
}
FiltersContext::MultiChain => {}
}
filters
}
}