ayaka_primitive/
line.rs

1use crate::*;
2use serde::Deserialize;
3use serde_with::rust::maps_duplicate_key_is_error;
4use std::collections::HashMap;
5
6/// Represents a line in a prograph.
7#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
8#[serde(untagged)]
9pub enum Line {
10    /// An empty line, usually fallbacks to the base language one.
11    Empty,
12    /// A text line.
13    Text(Text),
14    /// Some `switches`.
15    Switch {
16        /// The switch items.
17        switches: Vec<String>,
18    },
19    /// Custom line types.
20    #[serde(with = "maps_duplicate_key_is_error")]
21    Custom(HashMap<String, RawValue>),
22}
23
24#[cfg(test)]
25mod test {
26    use crate::{
27        text::test::{str, text},
28        *,
29    };
30    use std::collections::HashMap;
31
32    #[test]
33    fn parse() {
34        let lines = r#"
35- abc
36- exec: $a
37- switches:
38  - a
39  - b
40- video: 0
41-
42        "#;
43        let lines: Vec<Line> = serde_yaml::from_str(lines).unwrap();
44        assert_eq!(lines.len(), 5);
45        assert_eq!(lines[0], Line::Text(text(vec![str("abc")])));
46        assert_eq!(
47            lines[1],
48            Line::Custom(HashMap::from([(
49                "exec".to_string(),
50                RawValue::Str("$a".to_string())
51            )]))
52        );
53        assert_eq!(
54            lines[2],
55            Line::Switch {
56                switches: vec!["a".to_string(), "b".to_string()]
57            }
58        );
59        assert_eq!(
60            lines[3],
61            Line::Custom(HashMap::from([("video".to_string(), RawValue::Num(0))]))
62        );
63        assert_eq!(lines[4], Line::Empty);
64    }
65}