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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//! Data structure for enums.

use super::modifier::Modifier;
use crate::cons::Cons;
use crate::csharp::Csharp;
use crate::element::Element;
use crate::into_tokens::IntoTokens;
use crate::tokens::Tokens;

/// Model for Csharp Enums.
#[derive(Debug, Clone)]
pub struct Enum<'el> {
    /// Variants of the enum.
    pub variants: Tokens<'el, Csharp<'el>>,
    /// Enum modifiers.
    pub modifiers: Vec<Modifier>,
    /// What this enum implements.
    pub implements: Vec<Csharp<'el>>,
    /// Attributes for the constructor.
    attributes: Tokens<'el, Csharp<'el>>,
    /// Name of enum.
    name: Cons<'el>,
}

impl<'el> Enum<'el> {
    /// Build a new empty interface.
    pub fn new<N>(name: N) -> Enum<'el>
    where
        N: Into<Cons<'el>>,
    {
        Enum {
            variants: Tokens::new(),
            modifiers: vec![Modifier::Public],
            implements: vec![],
            attributes: Tokens::new(),
            name: name.into(),
        }
    }

    /// Push an attribute.
    pub fn attribute<T>(&mut self, attribute: T)
    where
        T: IntoTokens<'el, Csharp<'el>>,
    {
        self.attributes.push(attribute.into_tokens());
    }

    /// Name of enum.
    pub fn name(&self) -> Cons<'el> {
        self.name.clone()
    }
}

into_tokens_impl_from!(Enum<'el>, Csharp<'el>);

impl<'el> IntoTokens<'el, Csharp<'el>> for Enum<'el> {
    fn into_tokens(self) -> Tokens<'el, Csharp<'el>> {
        use self::Element::*;

        let mut sig = Tokens::new();

        sig.extend(self.modifiers.into_tokens());
        sig.append("enum");
        sig.append(self.name.clone());

        let mut extends = Tokens::new();

        extends.extend(self.implements.into_iter().map(Element::from));

        if !extends.is_empty() {
            sig.append(":");
            sig.append(extends.join(", "));
        }

        let mut s = Tokens::new();

        if !self.attributes.is_empty() {
            s.push(self.attributes);
        }

        s.push(toks![sig.join_spacing(), " {"]);

        s.nested({
            let mut body = Tokens::new();

            if !self.variants.is_empty() {
                body.append(self.variants.join(toks![",", PushSpacing]));
            }

            body.join_line_spacing()
        });

        s.push("}");

        s
    }
}

#[cfg(test)]
mod tests {
    use super::Enum;
    use crate::csharp::{self, Csharp};
    use crate::tokens::Tokens;

    #[test]
    fn test_vec() {
        let mut c = Enum::new("Foo");

        c.variants.append("FOO(1)");
        c.variants.append("BAR(2)");

        let t: Tokens<Csharp> = c.into();

        let s = t.to_string();
        let out = s.as_ref().map(|s| s.as_str());
        assert_eq!(Ok("public enum Foo {\n  FOO(1),\n  BAR(2)\n}",), out);
    }

    #[test]
    fn test_implements() {
        let mut c = Enum::new("Foo");

        c.implements = vec![csharp::local("long")];

        c.variants.append("FOO(1)");
        c.variants.append("BAR(2)");

        let t: Tokens<Csharp> = c.into();

        let s = t.to_string();
        let out = s.as_ref().map(|s| s.as_str());
        assert_eq!(Ok("public enum Foo : long {\n  FOO(1),\n  BAR(2)\n}",), out);
    }
}