1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
extern crate skimmer;

use crate::model::style::CommonStyles;
use crate::model::yaml::pairs::{compose, PairsValue};
use crate::model::{Model, Renderer, Rope, Tagged, TaggedValue};

use std::any::Any;
use std::borrow::Cow;

pub static TAG: &'static str = "tag:yaml.org,2002:omap";

#[derive(Clone, Copy)]
pub struct Omap;

impl Omap {
    pub fn get_tag() -> Cow<'static, str> {
        Cow::from(TAG)
    }
}

impl Model for Omap {
    fn get_tag(&self) -> Cow<'static, str> {
        Self::get_tag()
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_mut_any(&mut self) -> &mut dyn Any {
        self
    }

    fn is_sequence(&self) -> bool {
        true
    }

    fn compose(
        &self,
        renderer: &Renderer,
        value: TaggedValue,
        tags: &mut dyn Iterator<Item = &(Cow<'static, str>, Cow<'static, str>)>,
        children: &mut [Rope],
    ) -> Rope {
        let value: PairsValue =
            match <TaggedValue as Into<Result<OmapValue, TaggedValue>>>::into(value) {
                Ok(value) => value.into(),
                Err(_) => panic!("Not an OmapValue"),
            };

        compose(self, renderer, TaggedValue::from(value), tags, children)
    }
}

#[derive(Debug)]
pub struct OmapValue {
    styles: CommonStyles,
    alias: Option<Cow<'static, str>>,
}

impl OmapValue {
    pub fn new(styles: CommonStyles, alias: Option<Cow<'static, str>>) -> OmapValue {
        OmapValue { styles, alias }
    }

    pub fn take_alias(&mut self) -> Option<Cow<'static, str>> {
        self.alias.take()
    }
}

impl Tagged for OmapValue {
    fn get_tag(&self) -> Cow<'static, str> {
        Cow::from(TAG)
    }

    fn as_any(&self) -> &dyn Any {
        self as &dyn Any
    }

    fn as_mut_any(&mut self) -> &mut dyn Any {
        self as &mut dyn Any
    }
}

impl Into<PairsValue> for OmapValue {
    fn into(self) -> PairsValue {
        PairsValue::new(self.styles, self.alias)
    }
}

#[cfg(all(test, not(feature = "dev")))]
mod tests {
    use super::*;

    #[test]
    fn tag() {
        let omap = Omap; // ::new (&get_charset_utf8 ());

        assert_eq!(omap.get_tag(), TAG);
    }
}