config-resolver 0.2.1

A modular configuration resolver for auto-detecting and resolving runtime values (IPs, ports, peer IDs)
Documentation
use std::net::{IpAddr, SocketAddr};
use std::str::FromStr;

/// A value resolved from a configuration specification.
#[derive(Clone, Debug, PartialEq)]
pub enum ResolvedValue {
    /// A 32-bit unsigned integer (e.g. port numbers).
    U32(u32),
    /// A 64-bit unsigned integer (e.g. PeerIDs).
    U64(u64),
    /// An IP address.
    Ip(IpAddr),
    /// A socket address (IP + Port).
    SocketAddr(SocketAddr),
}

impl ResolvedValue {
    /// Attempts to convert the resolved value into a `u32`.
    ///
    /// IPv4 addresses are converted to their 32-bit integer representation.
    pub fn as_u32(&self) -> Option<u32> {
        match self {
            ResolvedValue::U32(v) => Some(*v),
            ResolvedValue::U64(v) => Some(*v as u32),
            ResolvedValue::Ip(IpAddr::V4(v)) => Some(u32::from(*v)),
            _ => None,
        }
    }

    /// Attempts to convert the resolved value into a `u64`.
    ///
    /// IPv4 addresses are converted to their 32-bit integer representation and cast to `u64`.
    pub fn as_u64(&self) -> Option<u64> {
        match self {
            ResolvedValue::U32(v) => Some(*v as u64),
            ResolvedValue::U64(v) => Some(*v),
            ResolvedValue::Ip(IpAddr::V4(v)) => Some(u32::from(*v) as u64),
            _ => None,
        }
    }

    /// Attempts to convert the resolved value into an `IpAddr`.
    pub fn as_ip(&self) -> Option<IpAddr> {
        match self {
            ResolvedValue::Ip(v) => Some(*v),
            _ => None,
        }
    }
}

/// A trait for types that can be resolved from a configuration string.
///
/// Any type implementing this trait must also implement `FromStr` (for static parsing).
pub trait ConfigUnit: Sized + Send + Sync + FromStr {
    /// Converts a `ResolvedValue` into the target type, returning `None` if the conversion fails.
    fn from_resolved(v: ResolvedValue) -> Option<Self>;
}

impl ConfigUnit for u32 {
    fn from_resolved(v: ResolvedValue) -> Option<Self> {
        v.as_u32()
    }
}

impl ConfigUnit for u64 {
    fn from_resolved(v: ResolvedValue) -> Option<Self> {
        v.as_u64()
    }
}

impl ConfigUnit for IpAddr {
    fn from_resolved(v: ResolvedValue) -> Option<Self> {
        v.as_ip()
    }
}

impl ConfigUnit for SocketAddr {
    fn from_resolved(v: ResolvedValue) -> Option<Self> {
        match v {
            ResolvedValue::SocketAddr(v) => Some(v),
            _ => None,
        }
    }
}

/// An AST node representing a configuration resolution step.
#[derive(Debug)]
pub enum ResolverNode {
    /// A static configuration value.
    Static(ResolvedValue),
    /// A value to be resolved dynamically via a registered provider.
    Provider(String),
    /// A socket address consisting of an IP node and a port node.
    Socket {
        /// The IP address resolution node.
        ip: Box<ResolverNode>,
        /// The port resolution node.
        port: Box<ResolverNode>,
    },
    /// An offset calculation base node + an offset integer.
    Calc {
        /// The base value resolution node.
        base: Box<ResolverNode>,
        /// The offset to apply to the base value.
        offset: i64,
    },
}