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
use crate::{
    datamodel::{
        attributes::FieldAttribute, index::UniqueFieldAttribute, DefaultValue, FieldType, IdFieldDefinition, Relation,
    },
    value::{Constant, Documentation, Function},
};
use std::{borrow::Cow, fmt};

/// A field in a model block.
#[derive(Debug)]
pub struct Field<'a> {
    name: Constant<Cow<'a, str>>,
    commented_out: bool,
    r#type: FieldType<'a>,
    documentation: Option<Documentation<'a>>,
    updated_at: Option<FieldAttribute<'a>>,
    unique: Option<UniqueFieldAttribute<'a>>,
    id: Option<IdFieldDefinition<'a>>,
    default: Option<DefaultValue<'a>>,
    map: Option<FieldAttribute<'a>>,
    relation: Option<Relation<'a>>,
    native_type: Option<FieldAttribute<'a>>,
    ignore: Option<FieldAttribute<'a>>,
}

impl<'a> Field<'a> {
    /// Create a new required model field declaration.
    ///
    /// ```ignore
    /// model User {
    ///   name String
    /// //     ^^^^^^ type_name
    /// //^^^^ name
    /// }
    /// ```
    pub fn new(name: impl Into<Cow<'a, str>>, type_name: impl Into<Cow<'a, str>>) -> Self {
        let name = Constant::new_no_validate(name.into());

        Self {
            name,
            commented_out: false,
            r#type: FieldType::required(type_name),
            map: None,
            documentation: None,
            updated_at: None,
            unique: None,
            id: None,
            default: None,
            relation: None,
            native_type: None,
            ignore: None,
        }
    }

    /// Sets the field as optional.
    ///
    /// ```ignore
    /// model Address {
    ///   street String?
    /// //             ^ this
    /// }
    /// ```
    pub fn optional(&mut self) {
        self.r#type.into_optional();
    }

    /// Sets the field to be an array.
    ///
    /// ```ignore
    /// model Address {
    ///   street String[]
    /// //             ^^ this
    /// }
    /// ```
    pub fn array(&mut self) {
        self.r#type.into_array();
    }

    /// Sets the field to be unsupported.
    ///
    /// ```ignore
    /// model Address {
    ///   street Unsupported("foo")
    /// //       ^^^^^^^^^^^^^^^^^^ this
    /// }
    /// ```
    pub fn unsupported(&mut self) {
        self.r#type.into_unsupported();
    }

    /// Sets the field map attribute.
    ///
    /// ```ignore
    /// model Address {
    ///   street String @map("Straße")
    ///                       ^^^^^^ value
    /// }
    /// ```
    pub fn map(&mut self, value: impl Into<Cow<'a, str>>) {
        let mut map = Function::new("map");
        map.push_param(value.into());

        self.map = Some(FieldAttribute::new(map));
    }

    /// Documentation of the field.
    ///
    /// ```ignore
    /// model Foo {
    ///   /// This is the documentation.
    ///   bar Int
    /// }
    /// ```
    pub fn documentation(&mut self, documentation: impl Into<Cow<'a, str>>) {
        match self.documentation.as_mut() {
            Some(docs) => docs.push(documentation.into()),
            None => self.documentation = Some(Documentation(documentation.into())),
        }
    }

    /// Sets the field default attribute.
    ///
    /// ```ignore
    /// model Address {
    ///   street String @default("Prenzlauer Allee")
    ///                           ^^^^^^^^^^^^^^^^ value
    /// }
    /// ```
    pub fn default(&mut self, value: DefaultValue<'a>) {
        self.default = Some(value);
    }

    /// Sets the native type of the field.
    ///
    /// ```ignore
    /// model Address {
    ///   street String @db.VarChar(255)
    /// //                          ^^^ param
    /// //                  ^^^^^^^ type_name
    /// //               ^^ prefix
    /// }
    /// ```
    ///
    /// TODO: `params` as `&[&str]` when we get rid of the DML.
    pub fn native_type(
        &mut self,
        prefix: impl Into<Cow<'a, str>>,
        r#type: impl Into<Cow<'a, str>>,
        params: Vec<String>,
    ) {
        let mut native_type = FieldAttribute::new(Function::new(r#type));

        for param in params {
            native_type.push_param(Constant::new_no_validate(param));
        }

        native_type.prefix(prefix);

        self.native_type = Some(native_type);
    }

    /// Marks the field to hold the update timestamp.
    ///
    /// ```ignore
    /// model Address {
    ///   street String @updatedAt
    /// //              ^^^^^^^^^^ adds this
    /// }
    /// ```
    pub fn updated_at(&mut self) {
        self.updated_at = Some(FieldAttribute::new(Function::new("updatedAt")));
    }

    /// Marks the field to hold a unique constraint.
    ///
    /// ```ignore
    /// model Address {
    ///   street String @unique(sort: Asc, length: 11)
    /// //              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this
    /// }
    /// ```
    pub fn unique(&mut self, options: UniqueFieldAttribute<'a>) {
        self.unique = Some(options);
    }

    /// Marks the field to be the id of the model.
    ///
    /// ```ignore
    /// model Address {
    ///   street String @id
    /// //              ^^^ this
    /// }
    /// ```
    pub fn id(&mut self, definition: IdFieldDefinition<'a>) {
        self.id = Some(definition);
    }

    /// Set the field to be a relation.
    ///
    /// ```ignore
    /// model Address {
    ///   street Street @relation(...)
    /// //              ^^^^^^^^^^^^^^ this
    /// }
    /// ```
    pub fn relation(&mut self, relation: Relation<'a>) {
        self.relation = Some(relation);
    }

    /// Ignores the field.
    ///
    /// ```ignore
    /// model Address {
    ///   street Street @ignore
    /// //              ^^^^^^^ this
    /// }
    /// ```
    pub fn ignore(&mut self) {
        self.ignore = Some(FieldAttribute::new(Function::new("ignore")));
    }

    /// Comments the field out.
    pub fn commented_out(&mut self) {
        self.commented_out = true;
    }
}

impl<'a> fmt::Display for Field<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(ref docs) = self.documentation {
            docs.fmt(f)?;
        }

        if self.commented_out {
            f.write_str("// ")?;
        }

        write!(f, "{} {}", self.name, self.r#type)?;

        if let Some(ref updated_at) = self.updated_at {
            write!(f, " {updated_at}")?;
        }

        if let Some(ref unique) = self.unique {
            write!(f, " {unique}")?;
        }

        if let Some(ref id) = self.id {
            write!(f, " {id}")?;
        }

        if let Some(ref def) = self.default {
            write!(f, " {def}")?;
        }

        if let Some(ref map) = self.map {
            write!(f, " {map}")?;
        }

        if let Some(ref relation) = self.relation {
            write!(f, " {relation}")?;
        }

        if let Some(ref nt) = self.native_type {
            write!(f, " {nt}")?;
        }

        if let Some(ref ignore) = self.ignore {
            write!(f, " {ignore}")?;
        }

        Ok(())
    }
}