noosphere-core 0.18.1

Core data types of the Rust Noosphere implementation
Documentation
use anyhow::anyhow;
use std::{convert::Infallible, fmt::Display, str::FromStr};

/// The Noosphere protocol version
pub enum Version {
    #[allow(missing_docs)]
    V0,
    /// All others
    Unknown(String),
}

impl Display for Version {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let value = match self {
            Version::V0 => "0",
            Version::Unknown(header) => header.as_str(),
        };

        write!(f, "{value}")
    }
}

impl FromStr for Version {
    type Err = Infallible;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "0" => Version::V0,
            _ => Version::Unknown(String::from(s)),
        })
    }
}

impl TryFrom<Version> for u32 {
    type Error = anyhow::Error;

    fn try_from(value: Version) -> Result<Self, Self::Error> {
        match value {
            Version::V0 => Ok(0),
            Version::Unknown(version) => Err(anyhow!("Unrecognized version: {}", version)),
        }
    }
}