Skip to main content

molframe_spatial/model/
backend_names.rs

1//! The words a spatial backend is spelled with.
2
3use super::SpatialBackend;
4use molframe_core::contract::{PolicyParseError, canonical_spelling};
5use std::fmt;
6use std::str::FromStr;
7
8impl SpatialBackend {
9    /// The words a backend can be spelled with, in declaration order.
10    pub const NAMES: &'static [&'static str] = &[
11        "brute-force",
12        "cell-list",
13        "kd-tree",
14        "neighbor-list",
15        "auto",
16    ];
17
18    /// The canonical spelling of this backend.
19    #[must_use]
20    pub const fn name(self) -> &'static str {
21        match self {
22            Self::BruteForce => "brute-force",
23            Self::CellList => "cell-list",
24            Self::KdTree => "kd-tree",
25            Self::NeighborList => "neighbor-list",
26            Self::Auto => "auto",
27        }
28    }
29}
30
31impl fmt::Display for SpatialBackend {
32    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
33        formatter.write_str(self.name())
34    }
35}
36
37impl FromStr for SpatialBackend {
38    type Err = PolicyParseError;
39
40    /// `cell` is accepted for `cell-list`, and an underscore for a hyphen.
41    fn from_str(value: &str) -> Result<Self, Self::Err> {
42        match canonical_spelling(value).as_str() {
43            "brute-force" => Ok(Self::BruteForce),
44            "cell-list" | "cell" => Ok(Self::CellList),
45            "kd-tree" => Ok(Self::KdTree),
46            "neighbor-list" => Ok(Self::NeighborList),
47            "auto" => Ok(Self::Auto),
48            _ => Err(PolicyParseError::new(
49                "backend",
50                value,
51                &Self::NAMES.join(", "),
52            )),
53        }
54    }
55}