lbl/
types.rs

1use std::collections::HashMap;
2use thiserror::Error;
3
4/// Lbl Error
5#[derive(Error, Debug)]
6pub enum LblError {
7    /// String error
8    #[error("Lbl error: {0}")]
9    LblError(String),
10
11    /// IoError
12    #[error("File system error: {0}")]
13    IoError(#[from] std::io::Error),
14
15    /// PolarsError
16    #[error("Polars error: {0}")]
17    PolarsError(#[from] polars::prelude::PolarsError),
18
19    /// HexError
20    #[error("Hex error: {0}")]
21    HexError(#[from] hex::FromHexError),
22}
23
24// into bytes
25
26/// Convert String, &str, or Vec<u8> into Vec<u8>
27pub trait TryIntoBytes {
28    /// convert into Vec<u8>
29    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// queries
51
52/// Label query
53#[derive(Default, Debug)]
54pub struct Query {
55    /// query labels in these collections
56    pub collections: Option<Vec<String>>,
57    /// query labels in these networks
58    pub networks: Option<Vec<String>>,
59    /// query labels in these addresses
60    pub addresses: Option<Vec<Vec<u8>>>,
61    /// query labels with these names
62    pub names: Option<Vec<String>>,
63    /// query labels with these functions
64    pub functions: Option<Vec<String>>,
65    /// query labels with these extra data values
66    pub extra_data_contains: Option<HashMap<String, serde_json::Value>>,
67    /// query labels with these extra data values
68    pub extra_data_equals: Option<HashMap<String, serde_json::Value>>,
69    /// query labels added by these labelers
70    pub added_bys: Option<Vec<String>>,
71    /// query labels added before this timestamp
72    pub added_before: Option<u64>,
73    /// query labels added after this timestamp
74    pub added_after: Option<u64>,
75    /// query labels added at this timestamp
76    pub added_at: Option<u64>,
77}
78
79impl Query {
80    /// create new Query
81    pub fn new() -> Self {
82        Query::default()
83    }
84
85    /// set query network
86    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    /// set query collection
95    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    /// set query name
104    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    /// set query address
113    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    /// set query function
123    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    /// set query extra data equals
132    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    /// set query extra data contains
138    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    /// set query extra data added by
144    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    /// set query extra data added before
153    pub fn added_before(mut self, added_before: u64) -> Self {
154        self.added_before = Some(added_before);
155        self
156    }
157
158    /// set query extra data added after
159    pub fn added_after(mut self, added_after: u64) -> Self {
160        self.added_after = Some(added_after);
161        self
162    }
163
164    /// set query extra data added at
165    pub fn added_at(mut self, added_at: u64) -> Self {
166        self.added_at = Some(added_at);
167        self
168    }
169}
170
171/// collection data saved in filesystem
172#[derive(Default, Debug)]
173pub struct CollectionData {
174    /// collection of labels
175    pub collection: Option<String>,
176    /// network of labels
177    pub network: Option<String>,
178}