Skip to main content

use_wythoff/
lib.rs

1#![forbid(unsafe_code)]
2#![doc = include_str!("../README.md")]
3
4use core::fmt;
5
6/// A lightweight Wythoff construction symbol.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct WythoffSymbol {
9    notation: String,
10}
11
12impl WythoffSymbol {
13    /// Creates a Wythoff symbol from non-empty notation.
14    #[must_use]
15    pub fn new(notation: impl Into<String>) -> Option<Self> {
16        let notation = notation.into();
17
18        if notation.trim().is_empty() {
19            None
20        } else {
21            Some(Self { notation })
22        }
23    }
24
25    /// Returns the notation string.
26    #[must_use]
27    pub fn notation(&self) -> &str {
28        &self.notation
29    }
30}
31
32impl fmt::Display for WythoffSymbol {
33    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
34        formatter.write_str(&self.notation)
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::WythoffSymbol;
41
42    #[test]
43    fn stores_wythoff_notation() {
44        let symbol = WythoffSymbol::new("3 | 4 2").expect("valid symbol");
45
46        assert_eq!(symbol.notation(), "3 | 4 2");
47        assert_eq!(symbol.to_string(), "3 | 4 2");
48        assert_eq!(WythoffSymbol::new("   "), None);
49    }
50}