Skip to main content

radixip/
types.rs

1use std::collections::HashMap;
2
3use ipnetwork::IpNetwork;
4use serde::{Deserialize, Serialize};
5
6/// User payload stored at a terminal prefix.
7///
8/// The string map keeps the ABI and Redis payloads simple while still allowing
9/// callers to attach labels such as "action", "asn", "country", or "reason".
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
11pub struct Metadata {
12    pub value: String,
13    pub attributes: HashMap<String, String>,
14}
15
16impl Metadata {
17    pub fn new(value: impl Into<String>) -> Self {
18        Self {
19            value: value.into(),
20            attributes: HashMap::new(),
21        }
22    }
23
24    pub fn with_attribute(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
25        self.attributes.insert(key.into(), value.into());
26        self
27    }
28}
29
30impl From<&str> for Metadata {
31    fn from(value: &str) -> Self {
32        Self::new(value)
33    }
34}
35
36impl From<String> for Metadata {
37    fn from(value: String) -> Self {
38        Self::new(value)
39    }
40}
41
42#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
43pub struct SubnetRule {
44    pub prefix: IpNetwork,
45    pub metadata: Metadata,
46}
47
48impl SubnetRule {
49    pub fn new(prefix: IpNetwork, metadata: Metadata) -> Self {
50        Self { prefix, metadata }
51    }
52}
53
54#[derive(Debug, Clone, Default, PartialEq, Eq)]
55pub struct EngineStats {
56    pub inserts: usize,
57    pub lookups: usize,
58    pub hits: usize,
59    pub misses: usize,
60    pub removals: usize,
61    pub size: usize,
62}