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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// LeapType
// LeapEnum
// Name - StrongString - length, allowed symbols
// todo: rename file to leapspec.rs? file per struct/enum?
// todo: checks - enum variants should have unique names (eg. if need multiple variants of same type wrap in struct first)
// todo: only allow structs to be variants of enum
// todo: checks - type args should be unique relative to struct and enum names, same type arg names can be used in different types
use crate::naming;
use std::collections::HashMap;
use std::fmt;

// todo: trait Name to String
#[derive(Debug, Clone)]
pub struct Name {
    name: String,
    alias: Option<String>,
    // position index of this name in source file
    pub position: usize,
}

#[derive(Debug, Clone)]
pub enum SimpleType {
    String,
    Integer,
    Float,
    Boolean,
}

#[derive(Debug, Clone)]
pub enum PropType {
    Simple(SimpleType),
    List(Box<PropType>),
    TypeArg(Name),
    LeapType { name: Name, args: Vec<PropType> },
}

#[derive(Debug)]
pub struct Prop {
    pub name: Name,
    pub prop_type: PropType,
}

#[derive(Debug)]
pub struct LeapStruct {
    pub name: Name,
    pub args: Vec<Name>,
    pub props: Vec<Prop>,
}

#[derive(Debug)]
pub struct LeapEnum {
    pub name: Name,
    pub args: Vec<Name>,
    pub variants: Vec<Prop>,
}

// todo: info for location of type, names, props for error reporting?
#[derive(Debug)]
pub enum LeapType {
    Struct(LeapStruct),
    Enum(LeapEnum),
}

#[derive(Debug)]
pub struct LeapTypePath {
    pub leap_type: LeapType,
    pub path: String,
}

#[derive(Debug)]
pub struct LeapSpec(Vec<LeapTypePath>);

fn get_alias_of_name(name: &Name, aliases: &HashMap<String, String>) -> Option<String> {
    if let Some(a) = aliases.get(name.get()) {
        Some(a.clone())
    } else {
        None
    }
}

fn aliased_from_aliases(name: &Name, aliases: &HashMap<String, String>) -> Result<Name, String> {
    name.to_aliased_if_some(get_alias_of_name(name, aliases))
}

impl Name {
    pub fn new(name: String, position: usize) -> Result<Self, String> {
        // todo: checks
        // - min, max length?
        // - allowed symbols?
        // - allowed start symbol?
        // - allowed end symbol?
        Ok(Name {
            name,
            alias: None,
            position,
        })
    }

    pub fn to_aliased(&self, alias: String) -> Result<Self, String> {
        // todo: check alias for same `name` rules
        // todo: when adding alias check there is no same name/alias, global type scoped, property names scoped, separate aliases for Types and for Props?
        Ok(Name {
            alias: Some(alias),
            ..self.clone()
        })
    }

    pub fn to_aliased_if_some(&self, alias: Option<String>) -> Result<Self, String> {
        if let Some(a) = alias {
            self.to_aliased(a)
        } else {
            Ok(Self::new(self.name.clone(), self.position)?)
        }
    }

    pub fn get(&self) -> &str {
        &self.name
    }

    fn get_aliased(&self) -> &str {
        if let Some(alias) = &self.alias {
            alias
        } else {
            &self.name
        }
    }

    pub fn snake_case(&self) -> String {
        naming::snake_case(&naming::get_parts(&self.get_aliased()))
    }

    pub fn upper_camel_case(&self) -> String {
        naming::upper_camel_case(&naming::get_parts(&self.get_aliased()))
    }

    pub fn camel_case(&self) -> String {
        naming::camel_case(&naming::get_parts(&self.get_aliased()))
    }

    pub fn joined(&self) -> String {
        naming::joined(&naming::get_parts(&self.get_aliased()))
    }

    pub fn uppercase_joined(&self) -> String {
        naming::uppercase_joined(&naming::get_parts(&self.get_aliased()))
    }
}

impl fmt::Display for Name {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.name)
    }
}

impl PartialEq for Name {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name
    }
}

impl SimpleType {
    pub fn name(&self) -> String {
        match self {
            // todo: replace strings with constants
            SimpleType::String => "str".to_owned(),
            SimpleType::Integer => "int".to_owned(),
            SimpleType::Float => "float".to_owned(),
            SimpleType::Boolean => "bool".to_owned(),
        }
    }
}

impl fmt::Display for PropType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Simple(t) => match t {
                SimpleType::String => write!(f, "str"),
                SimpleType::Integer => write!(f, "int"),
                SimpleType::Float => write!(f, "float"),
                SimpleType::Boolean => write!(f, "bool"),
            },
            Self::List(t) => write!(f, "list[{}]", t),
            Self::TypeArg(n) => write!(f, "{}?", n),
            Self::LeapType { name, args } => {
                if args.is_empty() {
                    write!(f, "{}", name)
                } else {
                    let args = args
                        .iter()
                        .map(|a| format!("{}", a))
                        .collect::<Vec<_>>()
                        .join(" ");
                    write!(f, "{}[{}]", name, args)
                }
            }
        }
    }
}

impl PropType {
    pub fn to_aliased(&self, aliases: &HashMap<String, String>) -> Result<Self, String> {
        match self {
            Self::List(t) => Ok(Self::List(Box::new(t.to_aliased(aliases)?))),
            Self::TypeArg(n) => Ok(Self::TypeArg(aliased_from_aliases(n, aliases)?)),
            Self::LeapType { name, args } => Ok(Self::LeapType {
                name: aliased_from_aliases(name, aliases)?,
                args: args
                    .iter()
                    .map(|a| a.to_aliased(aliases))
                    .collect::<Result<_, _>>()?,
            }),
            _ => Ok(self.clone()),
        }
    }

    pub fn name(&self) -> String {
        match self {
            Self::Simple(t) => t.name(),
            Self::List(_) => "list".to_owned(),
            Self::TypeArg(n) => n.get().to_owned(),
            Self::LeapType { name, .. } => name.get().to_owned(),
        }
    }
}

impl fmt::Display for Prop {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}: {}", self.name, self.prop_type)
    }
}

impl Prop {
    pub fn to_aliased(&self, aliases: &HashMap<String, String>) -> Result<Self, String> {
        Ok(Self {
            name: aliased_from_aliases(&self.name, aliases)?,
            prop_type: self.prop_type.to_aliased(aliases)?,
        })
    }
}

impl fmt::Display for LeapStruct {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let args = self
            .args
            .iter()
            .map(|a| a.get())
            .collect::<Vec<_>>()
            .join(" ");
        let props = self
            .props
            .iter()
            .map(|p| format!("{}", p))
            .collect::<Vec<_>>()
            .join(", ");
        write!(
            f,
            "Struct({} args: [{}], props: [{}])",
            self.name, args, props
        )
    }
}

impl LeapStruct {
    pub fn to_aliased(&self, aliases: &HashMap<String, String>) -> Result<Self, String> {
        Ok(Self {
            name: aliased_from_aliases(&self.name, aliases)?,
            args: self
                .args
                .iter()
                .map(|a| aliased_from_aliases(a, aliases))
                .collect::<Result<_, _>>()?,
            props: self
                .props
                .iter()
                .map(|p| p.to_aliased(aliases))
                .collect::<Result<_, _>>()?,
        })
    }
}

impl fmt::Display for LeapEnum {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let args = self
            .args
            .iter()
            .map(|a| a.get())
            .collect::<Vec<_>>()
            .join(" ");
        let variants = self
            .variants
            .iter()
            .map(|v| format!("{}", v))
            .collect::<Vec<_>>()
            .join(", ");
        write!(
            f,
            "Enum({} args: [{}], variants: [{}])",
            self.name, args, variants
        )
    }
}

impl LeapEnum {
    pub fn to_aliased(&self, aliases: &HashMap<String, String>) -> Result<Self, String> {
        Ok(Self {
            name: aliased_from_aliases(&self.name, aliases)?,
            args: self
                .args
                .iter()
                .map(|a| aliased_from_aliases(a, aliases))
                .collect::<Result<_, _>>()?,
            variants: self
                .variants
                .iter()
                .map(|v| v.to_aliased(aliases))
                .collect::<Result<_, _>>()?,
        })
    }
}

impl fmt::Display for LeapType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Struct(t) => write!(f, "Type({})", t),
            Self::Enum(e) => write!(f, "Type({})", e),
        }
    }
}

impl LeapType {
    pub fn to_aliased(&self, aliases: &HashMap<String, String>) -> Result<Self, String> {
        Ok(match self {
            Self::Struct(s) => Self::Struct(s.to_aliased(aliases)?),
            Self::Enum(e) => Self::Enum(e.to_aliased(aliases)?),
        })
    }

    pub fn name(&self) -> &Name {
        match self {
            Self::Enum(e) => &e.name,
            Self::Struct(s) => &s.name,
        }
    }

    pub fn args(&self) -> &[Name] {
        match self {
            Self::Enum(e) => &e.args,
            Self::Struct(s) => &s.args,
        }
    }
}

impl fmt::Display for LeapSpec {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Spec({})",
            self.0
                .iter()
                .map(|t| format!("{}", t))
                .collect::<Vec<_>>()
                .join(", ")
        )
    }
}

impl IntoIterator for LeapSpec {
    type Item = LeapTypePath;
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl LeapSpec {
    pub fn new(types: Vec<LeapTypePath>) -> Self {
        Self(types)
    }

    pub fn iter_types(&self) -> impl Iterator<Item = &LeapType> {
        self.0.iter().map(|t| &t.leap_type)
    }

    pub fn to_aliased(&self, aliases: &HashMap<String, String>) -> Result<Self, String> {
        Ok(Self(
            self.0
                .iter()
                .map(|t| t.to_aliased(aliases))
                .collect::<Result<_, _>>()?,
        ))
    }
}

impl fmt::Display for LeapTypePath {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.leap_type)
    }
}

impl LeapTypePath {
    pub fn new(leap_type: LeapType, path: String) -> Self {
        Self { leap_type, path }
    }

    pub fn to_aliased(&self, aliases: &HashMap<String, String>) -> Result<Self, String> {
        Ok(Self {
            leap_type: self.leap_type.to_aliased(aliases)?,
            path: self.path.clone(),
        })
    }
}