ca_rules/rules/
hex.rs

1//! Totalistic hexagonal rules.
2
3use super::Gen;
4use crate::ParseRuleError;
5
6rule_struct!(Hex);
7
8impl Hex {
9    parse_bs!(6);
10    parse_rule!('H');
11}
12
13/// A trait for parsing [totalistic hexagonal rules](http://www.conwaylife.com/wiki/Hexagonal_neighbourhood).
14///
15/// The `b` / `s` data of this type of rules consists of numbers of live neighbors
16/// that cause a cell to be born / survive.
17///
18/// # Examples
19///
20/// ```
21/// use ca_rules::ParseHex;
22///
23/// #[derive(Debug, Eq, PartialEq)]
24/// struct Rule {
25///     b: Vec<u8>,
26///     s: Vec<u8>,
27/// }
28///
29/// impl ParseHex for Rule {
30///     fn from_bs(b: Vec<u8>, s: Vec<u8>) -> Self {
31///         Rule { b, s }
32///     }
33/// }
34///
35/// let life = Rule::parse_rule("B2/S34H").unwrap();
36///
37/// assert_eq!(
38///     life,
39///     Rule {
40///         b: vec![2],
41///         s: vec![3, 4],
42///     }
43/// )
44/// ```
45pub trait ParseHex {
46    /// Construct the rule from `b` / `s` data.
47    fn from_bs(b: Vec<u8>, s: Vec<u8>) -> Self;
48
49    /// The parser.
50    fn parse_rule(input: &str) -> Result<Self, ParseRuleError>
51    where
52        Self: Sized,
53    {
54        let Hex { b, s } = Hex::parse_rule(input)?;
55        Ok(Self::from_bs(b, s))
56    }
57}
58
59/// A trait for parsing [totalistic hexagonal](http://www.conwaylife.com/wiki/Hexagonal_neighbourhood)
60/// [Generations](http://www.conwaylife.com/wiki/Generations) rules.
61///
62/// The `b` / `s` data of this type of rules consists of numbers of live neighbors
63/// that cause a cell to be born / survive.
64///
65/// # Examples
66///
67/// ```
68/// use ca_rules::ParseHexGen;
69///
70/// #[derive(Debug, Eq, PartialEq)]
71/// struct Rule {
72///     b: Vec<u8>,
73///     s: Vec<u8>,
74///     gen: usize,
75/// }
76///
77/// impl ParseHexGen for Rule {
78///     fn from_bsg(b: Vec<u8>, s: Vec<u8>, gen: usize) -> Self {
79///         Rule { b, s, gen }
80///     }
81/// }
82///
83/// let life = Rule::parse_rule("g4b24s13h").unwrap();
84///
85/// assert_eq!(
86///     life,
87///     Rule {
88///         b: vec![2, 4],
89///         s: vec![1, 3],
90///         gen: 4,
91///     }
92/// )
93/// ```
94pub trait ParseHexGen {
95    /// Construct the rule from `b` / `s` data and the number of states.
96    fn from_bsg(b: Vec<u8>, s: Vec<u8>, gen: usize) -> Self;
97
98    /// The parser.
99    fn parse_rule(input: &str) -> Result<Self, ParseRuleError>
100    where
101        Self: Sized,
102    {
103        let Gen {
104            rule: Hex { b, s },
105            gen,
106        } = Hex::parse_rule_gen(input)?;
107        Ok(Self::from_bsg(b, s, gen))
108    }
109}
110
111#[cfg(test)]
112mod tests {
113    use super::*;
114
115    struct Rule;
116
117    impl ParseHex for Rule {
118        fn from_bs(_b: Vec<u8>, _s: Vec<u8>) -> Self {
119            Rule
120        }
121    }
122
123    struct GenRule;
124
125    impl ParseHexGen for GenRule {
126        fn from_bsg(_b: Vec<u8>, _s: Vec<u8>, _gen: usize) -> Self {
127            GenRule
128        }
129    }
130
131    #[test]
132    fn valid_rules() -> Result<(), ParseRuleError> {
133        Rule::parse_rule("B3/S23H")?;
134        Rule::parse_rule("B3S23H")?;
135        Rule::parse_rule("b3s23h")?;
136        Rule::parse_rule("23/3H")?;
137        Rule::parse_rule("23/h")?;
138        Ok(())
139    }
140
141    #[test]
142    fn invalid_rules() {
143        assert_eq!(
144            Rule::parse_rule("B3/S23ha").err(),
145            Some(ParseRuleError::ExtraJunk)
146        );
147        assert_eq!(
148            Rule::parse_rule("B3H/S23").err(),
149            Some(ParseRuleError::Missing('S'))
150        );
151        assert_eq!(
152            Rule::parse_rule("B3/S23").err(),
153            Some(ParseRuleError::Missing('H'))
154        );
155        assert_eq!(
156            Rule::parse_rule("B3/S27H").err(),
157            Some(ParseRuleError::Missing('H'))
158        );
159        assert_eq!(
160            Rule::parse_rule("233h").err(),
161            Some(ParseRuleError::Missing('/'))
162        );
163    }
164
165    #[test]
166    fn valid_rules_gen() -> Result<(), ParseRuleError> {
167        GenRule::parse_rule("B3/S23/C3H")?;
168        GenRule::parse_rule("B3S23G3H")?;
169        GenRule::parse_rule("g3b3s23h")?;
170        GenRule::parse_rule("B3/S23H")?;
171        GenRule::parse_rule("23/3/3h")?;
172        GenRule::parse_rule("23//3H")?;
173        GenRule::parse_rule("23/3h")?;
174        Ok(())
175    }
176
177    #[test]
178    fn invalid_rules_gen() {
179        assert_eq!(
180            GenRule::parse_rule("B3/S23").err(),
181            Some(ParseRuleError::Missing('H'))
182        );
183        assert_eq!(
184            GenRule::parse_rule("B3/S23/H").err(),
185            Some(ParseRuleError::MissingNumber)
186        );
187        assert_eq!(
188            GenRule::parse_rule("g1b3s23h").err(),
189            Some(ParseRuleError::GenLessThan2)
190        );
191        assert_eq!(
192            GenRule::parse_rule("2333h").err(),
193            Some(ParseRuleError::Missing('/'))
194        );
195    }
196}