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
use std::fmt;

/// Represents a TypeScript `interface` declaration.
#[derive(Debug, PartialEq)]
pub struct Interface {
    /// The name of the interface.
    pub name: String,
    /// The fields of the interface.
    pub fields: Vec<Field>,
}

impl Interface {
    /// Create a new interface with the given name
    /// and an empty list of fields.
    pub fn new(name: &str) -> Self {
        Self {
            name: String::from(name),
            fields: Vec::new(),
        }
    }

    /// Add a field to the interface.
    pub fn field(mut self, field: Field) -> Self {
        self.fields.push(field);
        self
    }
}

impl From<camo::Struct> for Interface {
    fn from(structure: camo::Struct) -> Self {
        Self {
            name: structure.name,
            fields: structure.fields.into_iter().map(Field::from).collect(),
        }
    }
}

impl fmt::Display for Interface {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(f, "interface {} {{", self.name)?;
        for field in &self.fields {
            write!(f, "\t{}", field)?;
        }
        writeln!(f, "}}")
    }
}

/// Represents an `interface` field, e.g:
/// `<name>: <ty>`
#[derive(Debug, PartialEq)]
pub struct Field {
    /// The name of the field.
    pub name: String,
    /// The type of the field.
    pub ty: Type,
}

impl Field {
    /// Create a new field with the given name and type.
    pub fn new(name: &str, ty: Type) -> Self {
        Self {
            name: String::from(name),
            ty,
        }
    }
}

impl From<camo::Field> for Field {
    fn from(field: camo::Field) -> Self {
        Self {
            name: field.name,
            ty: Type::from(field.ty),
        }
    }
}

impl fmt::Display for Field {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(f, "{name}: {ty};", name = self.name, ty = self.ty)
    }
}

/// Represents a type use, e. g. in an interface definition,
/// function type definition, or type alias.
#[derive(Debug, PartialEq)]
pub enum Type {
    Builtin(Builtin),
}

impl From<camo::Type> for Type {
    fn from(ty: camo::Type) -> Self {
        match ty {
            camo::Type::Builtin(builtin) => Type::Builtin(Builtin::from(builtin)),
        }
    }
}

impl fmt::Display for Type {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Type::Builtin(builtin) => write!(f, "{}", builtin),
        }
    }
}

/// The built-in types.
#[derive(Debug, PartialEq)]
pub enum Builtin {
    /// The `number` type.
    Number,
    /// The `boolean` type.
    Boolean,
    /// The `string` type.
    String,
}

impl From<camo::Builtin> for Builtin {
    fn from(builtin: camo::Builtin) -> Self {
        match builtin {
            camo::Builtin::Bool => Builtin::Boolean,
            camo::Builtin::U8
            | camo::Builtin::U16
            | camo::Builtin::U32
            | camo::Builtin::U64
            | camo::Builtin::U128
            | camo::Builtin::Usize
            | camo::Builtin::I8
            | camo::Builtin::I16
            | camo::Builtin::I32
            | camo::Builtin::I64
            | camo::Builtin::I128
            | camo::Builtin::Isize
            | camo::Builtin::F32
            | camo::Builtin::F64 => Builtin::Number,
            camo::Builtin::Char | camo::Builtin::Str => Builtin::String,
        }
    }
}

impl fmt::Display for Builtin {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Builtin::Number => write!(f, "number"),
            Builtin::Boolean => write!(f, "boolean"),
            Builtin::String => write!(f, "string"),
        }
    }
}