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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use core::{Loc, OptionEntry, RpModifier, RpNumber, RpPackage, VersionReq};
use std::result;

#[derive(Debug, PartialEq, Eq)]
pub enum Type {
    Double,
    Float,
    Signed { size: Option<usize> },
    Unsigned { size: Option<usize> },
    Boolean,
    String,
    Bytes,
    Any,
    /// ISO-8601 for date and time.
    DateTime,
    Name { name: Name },
    Array { inner: Box<Type> },
    Map { key: Box<Type>, value: Box<Type> },
}

#[derive(Debug, PartialEq, Eq)]
pub enum Decl<'input> {
    Type(Loc<TypeBody<'input>>),
    Tuple(Loc<TupleBody<'input>>),
    Interface(Loc<InterfaceBody<'input>>),
    Enum(Loc<EnumBody<'input>>),
    Service(Loc<ServiceBody<'input>>),
}

impl<'input> Decl<'input> {
    pub fn name(&self) -> &str {
        use self::Decl::*;

        match *self {
            Type(ref body) => &body.name,
            Tuple(ref body) => &body.name,
            Interface(ref body) => &body.name,
            Enum(ref body) => &body.name,
            Service(ref body) => &body.name,
        }
    }
}

#[derive(Debug, PartialEq, Eq)]
pub struct EnumBody<'input> {
    pub name: &'input str,
    pub comment: Vec<&'input str>,
    pub ty: Option<Loc<Type>>,
    pub variants: Vec<Loc<EnumVariant<'input>>>,
    pub members: Vec<Loc<Member<'input>>>,
}

#[derive(Debug, PartialEq, Eq)]
pub struct EnumVariant<'input> {
    pub name: Loc<&'input str>,
    pub comment: Vec<&'input str>,
    pub argument: Option<Loc<Value<'input>>>,
}

#[derive(Debug, PartialEq, Eq)]
pub struct Field<'input> {
    pub modifier: RpModifier,
    pub name: &'input str,
    pub comment: Vec<&'input str>,
    pub ty: Type,
    pub field_as: Option<String>,
}

#[derive(Debug, PartialEq, Eq)]
pub struct File<'input> {
    pub options: Vec<Loc<OptionDecl<'input>>>,
    pub uses: Vec<Loc<UseDecl<'input>>>,
    pub decls: Vec<Loc<Decl<'input>>>,
}

impl<'input> Field<'input> {
    pub fn is_optional(&self) -> bool {
        match self.modifier {
            RpModifier::Optional => true,
            _ => false,
        }
    }
}

#[derive(Debug, PartialEq, Eq)]
pub enum Name {
    Relative { parts: Vec<String> },
    Absolute {
        prefix: Option<String>,
        parts: Vec<String>,
    },
}

#[derive(Debug, PartialEq, Eq)]
pub struct InterfaceBody<'input> {
    pub name: &'input str,
    pub comment: Vec<&'input str>,
    pub members: Vec<Loc<Member<'input>>>,
    pub sub_types: Vec<Loc<SubType<'input>>>,
}

#[derive(Debug, PartialEq, Eq)]
pub enum Member<'input> {
    Field(Field<'input>),
    Code(&'input str, Vec<String>),
    Option(OptionDecl<'input>),
    InnerDecl(Decl<'input>),
}

#[derive(Debug, PartialEq, Eq)]
pub struct OptionDecl<'input> {
    pub name: &'input str,
    pub value: Loc<Value<'input>>,
}

impl<'input> OptionEntry for OptionDecl<'input> {
    fn name(&self) -> &str {
        &self.name
    }

    fn as_string(&self) -> result::Result<String, &'static str> {
        match *self.value.value() {
            Value::String(ref string) => Ok(string.to_string()),
            _ => Err("expected string"),
        }
    }

    fn as_number(&self) -> result::Result<RpNumber, &'static str> {
        match *self.value.value() {
            Value::Number(ref number) => Ok(number.clone()),
            _ => Err("expected number"),
        }
    }

    fn as_identifier(&self) -> result::Result<String, &'static str> {
        match *self.value.value() {
            Value::Identifier(ref identifier) => Ok(identifier.to_string()),
            _ => Err("expected identifier"),
        }
    }
}

#[derive(Debug, PartialEq, Eq)]
pub enum PathSegment<'input> {
    Literal { value: Loc<String> },
    Variable {
        name: Loc<&'input str>,
        ty: Loc<Type>,
    },
}

#[derive(Debug, PartialEq, Eq)]
pub struct PathSpec<'input> {
    pub segments: Vec<PathSegment<'input>>,
}

#[derive(Debug, PartialEq, Eq)]
pub struct ServiceBody<'input> {
    pub name: &'input str,
    pub comment: Vec<&'input str>,
    pub children: Vec<ServiceNested<'input>>,
}

#[derive(Debug, PartialEq, Eq)]
pub enum ServiceNested<'input> {
    Endpoint {
        method: Option<Loc<&'input str>>,
        path: Option<Loc<PathSpec<'input>>>,
        comment: Vec<&'input str>,
        options: Vec<Loc<OptionDecl<'input>>>,
        children: Vec<ServiceNested<'input>>,
    },
    Returns {
        comment: Vec<&'input str>,
        status: Option<Loc<RpNumber>>,
        produces: Option<Loc<String>>,
        ty: Option<Loc<Type>>,
        options: Vec<Loc<OptionDecl<'input>>>,
    },
    Accepts {
        comment: Vec<&'input str>,
        accepts: Option<Loc<String>>,
        alias: Option<Loc<&'input str>>,
        ty: Option<Loc<Type>>,
        options: Vec<Loc<OptionDecl<'input>>>,
    },
}

impl<'input> ServiceNested<'input> {
    pub fn is_terminus(&self) -> bool {
        match *self {
            ServiceNested::Returns { .. } => true,
            ServiceNested::Accepts { .. } => true,
            _ => false,
        }
    }
}

/// Sub-types in interface declarations.
#[derive(Debug, PartialEq, Eq)]
pub struct SubType<'input> {
    pub name: &'input str,
    pub comment: Vec<&'input str>,
    pub members: Vec<Loc<Member<'input>>>,
}

#[derive(Debug, PartialEq, Eq)]
pub struct TupleBody<'input> {
    pub name: &'input str,
    pub comment: Vec<&'input str>,
    pub members: Vec<Loc<Member<'input>>>,
}

#[derive(Debug, PartialEq, Eq)]
pub struct TypeBody<'input> {
    pub name: &'input str,
    pub comment: Vec<&'input str>,
    pub members: Vec<Loc<Member<'input>>>,
}

#[derive(Debug, PartialEq, Eq)]
pub struct UseDecl<'input> {
    pub package: Loc<RpPackage>,
    pub version_req: Option<Loc<VersionReq>>,
    pub alias: Option<Loc<&'input str>>,
}

#[derive(Debug, PartialEq, Eq)]
pub enum Value<'input> {
    String(String),
    Number(RpNumber),
    Boolean(bool),
    Identifier(&'input str),
    Array(Vec<Loc<Value<'input>>>),
}