fast-nnt 0.4.0

Rust implementation of the NeighborNet phylogenetic network algorithm.
Documentation
use clap::ValueEnum;

pub mod ordering_huson2023;
pub mod ordering_multiway;
pub(crate) mod pair_search;

#[derive(ValueEnum, Clone, Debug, Default)]
#[clap(rename_all = "kebab-case")]
pub enum OrderingMethod {
    #[clap(alias = "huson2023", alias = "closest_pair")]
    ClosestPair,
    #[default]
    #[clap(alias = "splitstree4", alias = "splits-tree4", alias = "splits_tree4")]
    Multiway,
}

impl OrderingMethod {
    pub fn as_str(&self) -> &str {
        match self {
            OrderingMethod::ClosestPair => "closest-pair",
            OrderingMethod::Multiway => "multiway",
        }
    }

    pub fn from_option(opt: Option<&str>) -> anyhow::Result<Self> {
        opt.map_or(Ok(OrderingMethod::default()), str::parse)
    }
}

impl std::str::FromStr for OrderingMethod {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> anyhow::Result<Self> {
        let normalized: String = s.to_lowercase().replace(['-', '_'], "");
        match normalized.as_str() {
            "closestpair" | "huson2023" => Ok(OrderingMethod::ClosestPair),
            "multiway" | "splitstree4" => Ok(OrderingMethod::Multiway),
            _ => anyhow::bail!(
                "unknown ordering method '{}' (expected 'multiway' or 'closest-pair')",
                s
            ),
        }
    }
}