Skip to main content

config_resolver/
types.rs

1use std::net::{IpAddr, SocketAddr};
2use std::str::FromStr;
3
4/// A value resolved from a configuration specification.
5#[derive(Clone, Debug, PartialEq)]
6pub enum ResolvedValue {
7    /// A 32-bit unsigned integer (e.g. port numbers).
8    U32(u32),
9    /// A 64-bit unsigned integer (e.g. PeerIDs).
10    U64(u64),
11    /// An IP address.
12    Ip(IpAddr),
13    /// A socket address (IP + Port).
14    SocketAddr(SocketAddr),
15}
16
17impl ResolvedValue {
18    /// Attempts to convert the resolved value into a `u32`.
19    ///
20    /// IPv4 addresses are converted to their 32-bit integer representation.
21    pub fn as_u32(&self) -> Option<u32> {
22        match self {
23            ResolvedValue::U32(v) => Some(*v),
24            ResolvedValue::U64(v) => Some(*v as u32),
25            ResolvedValue::Ip(IpAddr::V4(v)) => Some(u32::from(*v)),
26            _ => None,
27        }
28    }
29
30    /// Attempts to convert the resolved value into a `u64`.
31    ///
32    /// IPv4 addresses are converted to their 32-bit integer representation and cast to `u64`.
33    pub fn as_u64(&self) -> Option<u64> {
34        match self {
35            ResolvedValue::U32(v) => Some(*v as u64),
36            ResolvedValue::U64(v) => Some(*v),
37            ResolvedValue::Ip(IpAddr::V4(v)) => Some(u32::from(*v) as u64),
38            _ => None,
39        }
40    }
41
42    /// Attempts to convert the resolved value into an `IpAddr`.
43    pub fn as_ip(&self) -> Option<IpAddr> {
44        match self {
45            ResolvedValue::Ip(v) => Some(*v),
46            _ => None,
47        }
48    }
49}
50
51/// A trait for types that can be resolved from a configuration string.
52///
53/// Any type implementing this trait must also implement `FromStr` (for static parsing).
54pub trait ConfigUnit: Sized + Send + Sync + FromStr {
55    /// Converts a `ResolvedValue` into the target type, returning `None` if the conversion fails.
56    fn from_resolved(v: ResolvedValue) -> Option<Self>;
57}
58
59impl ConfigUnit for u32 {
60    fn from_resolved(v: ResolvedValue) -> Option<Self> {
61        v.as_u32()
62    }
63}
64
65impl ConfigUnit for u64 {
66    fn from_resolved(v: ResolvedValue) -> Option<Self> {
67        v.as_u64()
68    }
69}
70
71impl ConfigUnit for IpAddr {
72    fn from_resolved(v: ResolvedValue) -> Option<Self> {
73        v.as_ip()
74    }
75}
76
77impl ConfigUnit for SocketAddr {
78    fn from_resolved(v: ResolvedValue) -> Option<Self> {
79        match v {
80            ResolvedValue::SocketAddr(v) => Some(v),
81            _ => None,
82        }
83    }
84}
85
86/// An AST node representing a configuration resolution step.
87#[derive(Debug)]
88pub enum ResolverNode {
89    /// A static configuration value.
90    Static(ResolvedValue),
91    /// A value to be resolved dynamically via a registered provider.
92    Provider(String),
93    /// A socket address consisting of an IP node and a port node.
94    Socket {
95        /// The IP address resolution node.
96        ip: Box<ResolverNode>,
97        /// The port resolution node.
98        port: Box<ResolverNode>,
99    },
100    /// An offset calculation base node + an offset integer.
101    Calc {
102        /// The base value resolution node.
103        base: Box<ResolverNode>,
104        /// The offset to apply to the base value.
105        offset: i64,
106    },
107}