1use std::collections::HashMap;
2use thiserror::Error;
3
4#[derive(Error, Debug)]
6pub enum LblError {
7 #[error("Lbl error: {0}")]
9 LblError(String),
10
11 #[error("File system error: {0}")]
13 IoError(#[from] std::io::Error),
14
15 #[error("Polars error: {0}")]
17 PolarsError(#[from] polars::prelude::PolarsError),
18
19 #[error("Hex error: {0}")]
21 HexError(#[from] hex::FromHexError),
22}
23
24pub trait TryIntoBytes {
28 fn try_into_bytes(self) -> Result<Vec<u8>, LblError>;
30}
31
32impl TryIntoBytes for Vec<u8> {
33 fn try_into_bytes(self) -> Result<Vec<u8>, LblError> {
34 Ok(self)
35 }
36}
37
38impl<'a> TryIntoBytes for &'a str {
39 fn try_into_bytes(self) -> Result<Vec<u8>, LblError> {
40 Ok(hex::decode(self)?)
41 }
42}
43
44impl TryIntoBytes for String {
45 fn try_into_bytes(self) -> Result<Vec<u8>, LblError> {
46 Ok(hex::decode(self)?)
47 }
48}
49
50#[derive(Default, Debug)]
54pub struct Query {
55 pub collections: Option<Vec<String>>,
57 pub networks: Option<Vec<String>>,
59 pub addresses: Option<Vec<Vec<u8>>>,
61 pub names: Option<Vec<String>>,
63 pub functions: Option<Vec<String>>,
65 pub extra_data_contains: Option<HashMap<String, serde_json::Value>>,
67 pub extra_data_equals: Option<HashMap<String, serde_json::Value>>,
69 pub added_bys: Option<Vec<String>>,
71 pub added_before: Option<u64>,
73 pub added_after: Option<u64>,
75 pub added_at: Option<u64>,
77}
78
79impl Query {
80 pub fn new() -> Self {
82 Query::default()
83 }
84
85 pub fn network(mut self, network: &str) -> Self {
87 match self.networks {
88 Some(ref mut networks) => networks.push(network.to_string()),
89 None => self.networks = Some(vec![network.to_string()]),
90 }
91 self
92 }
93
94 pub fn collection(mut self, collection: &str) -> Self {
96 match self.collections {
97 Some(ref mut collections) => collections.push(collection.to_string()),
98 None => self.collections = Some(vec![collection.to_string()]),
99 }
100 self
101 }
102
103 pub fn name(mut self, name: &str) -> Self {
105 match self.names {
106 Some(ref mut names) => names.push(name.to_string()),
107 None => self.names = Some(vec![name.to_string()]),
108 }
109 self
110 }
111
112 pub fn address<T: TryIntoBytes>(mut self, address: T) -> Result<Self, LblError> {
114 let address = address.try_into_bytes()?;
115 match self.addresses {
116 Some(ref mut addresses) => addresses.push(address),
117 None => self.addresses = Some(vec![address]),
118 }
119 Ok(self)
120 }
121
122 pub fn function(mut self, function: &str) -> Self {
124 match self.functions {
125 Some(ref mut functions) => functions.push(function.to_string()),
126 None => self.functions = Some(vec![function.to_string()]),
127 }
128 self
129 }
130
131 pub fn extra_data_equals(mut self, extra_data: HashMap<String, serde_json::Value>) -> Self {
133 self.extra_data_equals = Some(extra_data);
134 self
135 }
136
137 pub fn extra_data_contains(mut self, extra_data: HashMap<String, serde_json::Value>) -> Self {
139 self.extra_data_contains = Some(extra_data);
140 self
141 }
142
143 pub fn added_by(mut self, added_by: &str) -> Self {
145 match self.added_bys {
146 Some(ref mut added_bys) => added_bys.push(added_by.to_string()),
147 None => self.added_bys = Some(vec![added_by.to_string()]),
148 }
149 self
150 }
151
152 pub fn added_before(mut self, added_before: u64) -> Self {
154 self.added_before = Some(added_before);
155 self
156 }
157
158 pub fn added_after(mut self, added_after: u64) -> Self {
160 self.added_after = Some(added_after);
161 self
162 }
163
164 pub fn added_at(mut self, added_at: u64) -> Self {
166 self.added_at = Some(added_at);
167 self
168 }
169}
170
171#[derive(Default, Debug)]
173pub struct CollectionData {
174 pub collection: Option<String>,
176 pub network: Option<String>,
178}