lyrical_meter/
meter.rs

1crate::ix!();
2
3/// Enum representing any type of meter, either standard or other.
4#[derive(RandConstruct,Hash,Debug,Clone,Serialize,Deserialize,PartialEq,Eq)]
5pub enum Meter {
6
7    #[rand_construct(p=0.7)]
8    Standard(LyricalMeter),
9
10    #[rand_construct(p=0.3)]
11    Other(OtherMeter),
12}
13
14impl AIDescriptor for Meter {
15
16    //TODO: this is a temporary hack for now until the 
17    //      ai-descriptor-derive crate is updated
18    fn ai(&self) -> Cow<'_,str> {
19        match self {
20            Meter::Standard(ref meter) => meter.ai(),
21            Meter::Other(ref meter) => meter.text(),
22        }
23    }
24
25    fn ai_alt(&self) -> Cow<'_,str> {
26        match self {
27            Meter::Standard(ref meter) => meter.ai_alt(),
28            Meter::Other(ref meter) => meter.text(),
29        }
30    }
31}
32
33impl Default for Meter {
34    fn default() -> Self {
35        Self::Standard(LyricalMeter::default())
36    }
37}
38
39impl Meter {
40    /// Checks if two meters are of the same type.
41    pub fn is_same_type(&self, other: &Meter) -> bool {
42        matches!((self, other), (Meter::Standard(_), Meter::Standard(_)) | (Meter::Other(_), Meter::Other(_)))
43    }
44
45    /// Converts the `Meter` into a `LyricalMeter` if it is `Standard`.
46    pub fn as_standard(&self) -> Option<&LyricalMeter> {
47        if let Meter::Standard(ref lyrical_meter) = self {
48            Some(lyrical_meter)
49        } else {
50            None
51        }
52    }
53
54    /// Converts the `Meter` into an `OtherMeter` if it is `Other`.
55    pub fn as_other(&self) -> Option<&OtherMeter> {
56        if let Meter::Other(ref other_meter) = self {
57            Some(other_meter)
58        } else {
59            None
60        }
61    }
62}