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
//! Model for registered types.

use std::fmt;
use {Flavor, RpName};

/// Marker for the existence of a registered type of the given kind.
#[derive(Debug, Clone)]
pub enum RpReg {
    Type,
    Tuple,
    Interface,
    SubType,
    Enum,
    EnumVariant,
    Service,
}

impl RpReg {
    pub fn ident<PackageFn, InnerFn, F: 'static>(
        &self,
        name: &RpName<F>,
        package_fn: PackageFn,
        inner_fn: InnerFn,
    ) -> String
    where
        PackageFn: Fn(Vec<&str>) -> String,
        InnerFn: Fn(Vec<&str>) -> String,
        F: Flavor,
    {
        use self::RpReg::*;

        match *self {
            Type | Interface | Enum | Tuple | Service => {
                let p = name.parts.iter().map(String::as_str).collect();
                package_fn(p)
            }
            SubType | EnumVariant => {
                let mut v: Vec<&str> = name.parts.iter().map(String::as_str).collect();
                let at = v.len().saturating_sub(2);
                let last = inner_fn(v.split_off(at));

                let mut parts = v.clone();
                parts.push(last.as_str());

                inner_fn(parts)
            }
        }
    }

    /// Check if registered type is an enum.
    pub fn is_enum(&self) -> bool {
        use self::RpReg::*;

        match *self {
            Enum => true,
            _ => false,
        }
    }
}

impl fmt::Display for RpReg {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        use self::RpReg::*;

        match *self {
            Type => write!(fmt, "type"),
            Interface => write!(fmt, "interface"),
            Enum => write!(fmt, "enum"),
            Tuple => write!(fmt, "tuple"),
            Service => write!(fmt, "service"),
            SubType => write!(fmt, "subtype"),
            EnumVariant => write!(fmt, "variant"),
        }
    }
}