Skip to main content

fast_nnt/ordering/
mod.rs

1use clap::ValueEnum;
2
3pub mod ordering_huson2023;
4pub mod ordering_multiway;
5pub(crate) mod pair_search;
6
7#[derive(ValueEnum, Clone, Debug, Default)]
8#[clap(rename_all = "kebab-case")]
9pub enum OrderingMethod {
10    #[clap(alias = "huson2023", alias = "closest_pair")]
11    ClosestPair,
12    #[default]
13    #[clap(alias = "splitstree4", alias = "splits-tree4", alias = "splits_tree4")]
14    Multiway,
15}
16
17impl OrderingMethod {
18    pub fn as_str(&self) -> &str {
19        match self {
20            OrderingMethod::ClosestPair => "closest-pair",
21            OrderingMethod::Multiway => "multiway",
22        }
23    }
24
25    pub fn from_option(opt: Option<&str>) -> anyhow::Result<Self> {
26        opt.map_or(Ok(OrderingMethod::default()), str::parse)
27    }
28}
29
30impl std::str::FromStr for OrderingMethod {
31    type Err = anyhow::Error;
32
33    fn from_str(s: &str) -> anyhow::Result<Self> {
34        let normalized: String = s.to_lowercase().replace(['-', '_'], "");
35        match normalized.as_str() {
36            "closestpair" | "huson2023" => Ok(OrderingMethod::ClosestPair),
37            "multiway" | "splitstree4" => Ok(OrderingMethod::Multiway),
38            _ => anyhow::bail!(
39                "unknown ordering method '{}' (expected 'multiway' or 'closest-pair')",
40                s
41            ),
42        }
43    }
44}