rfham-config 0.1.0

Shared configuration data for RF-Ham libraries and tools.
Documentation
//!
//! Provides ..., a one-line description
//!
//! More detailed description
//!
//! # Examples
//!
//! ```rust
//! ```
//!

use core::{fmt::Display, str::FromStr};
use rfham_core::{Name, error::CoreError};

// ------------------------------------------------------------------------------------------------
// Public Macros
// ------------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------------
// Public Types
// ------------------------------------------------------------------------------------------------

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ConfigPath(Vec<Name>);

// ------------------------------------------------------------------------------------------------
// Public Functions
// ------------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------------
// Private Macros
// ------------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------------
// Private Types
// ------------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------------
// Implementations
// ------------------------------------------------------------------------------------------------

impl Display for ConfigPath {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            self.0
                .iter()
                .map(Name::to_string)
                .collect::<Vec<_>>()
                .join(".")
        )
    }
}

impl FromStr for ConfigPath {
    type Err = CoreError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let results: Result<Vec<Name>, CoreError> = s.split('.').map(Name::from_str).collect();
        let values = results?;
        if !values.is_empty() {
            Ok(Self(values))
        } else {
            Err(CoreError::InvalidValueFromStr(s.to_string(), "ConfigPath"))
        }
    }
}

impl From<Name> for ConfigPath {
    fn from(value: Name) -> Self {
        Self::from(vec![value])
    }
}

impl From<Vec<Name>> for ConfigPath {
    fn from(values: Vec<Name>) -> Self {
        assert!(
            !values.is_empty(),
            "ConfigPath must have at least one component"
        );
        Self(values)
    }
}

// ------------------------------------------------------------------------------------------------
// Private Functions
// ------------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------------
// Sub-Modules
// ------------------------------------------------------------------------------------------------