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
mod relation;

pub use relation::Relation;

use super::{attributes::BlockAttribute, field::Field, IdDefinition, IndexDefinition};
use crate::value::{Constant, Documentation, Function};
use std::{borrow::Cow, fmt};

#[derive(Debug, Clone, Copy)]
pub(super) enum Commented {
    On,
    Off,
}

impl fmt::Display for Commented {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Commented::On => f.write_str("// "),
            Commented::Off => Ok(()),
        }
    }
}

/// Defines a model block.
#[derive(Debug)]
pub struct Model<'a> {
    name: Constant<Cow<'a, str>>,
    documentation: Option<Documentation<'a>>,
    commented_out: Commented,
    ignore: Option<BlockAttribute<'a>>,
    id: Option<IdDefinition<'a>>,
    map: Option<BlockAttribute<'a>>,
    fields: Vec<Field<'a>>,
    indexes: Vec<IndexDefinition<'a>>,
    schema: Option<BlockAttribute<'a>>,
}

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

        Self {
            name,
            commented_out: Commented::Off,
            map: None,
            documentation: None,
            ignore: None,
            id: None,
            schema: None,
            fields: Vec::new(),
            indexes: Vec::new(),
        }
    }

    /// Documentation of the model. If called repeatedly, adds the new docs to the end with a
    /// newline. This method is also responsible for avoiding to add the same comment twice (mainly
    /// in reintrospection).
    ///
    /// ```ignore
    /// /// This is the documentation.
    /// model Foo {
    ///   ....
    /// }
    /// ```
    pub fn documentation(&mut self, new_documentation: impl Into<Cow<'a, str>>) {
        let new_documentation: Cow<'_, str> = new_documentation.into();

        if self
            .documentation
            .as_ref()
            .map(|d| d.0.contains(new_documentation.as_ref()))
            .unwrap_or_default()
        {
            return;
        }

        match self.documentation.as_mut() {
            Some(documentation) => documentation.push(new_documentation),
            None => self.documentation = Some(Documentation(new_documentation)),
        }
    }

    /// Ignore the model.
    ///
    /// ```ignore
    /// model Foo {
    ///   @@ignore
    ///   ^^^^^^^^ this
    /// }
    /// ```
    pub fn ignore(&mut self) {
        self.ignore = Some(BlockAttribute(Function::new("ignore")));
    }

    /// Add a model-level id definition.
    ///
    /// ```ignore
    /// model Foo {
    ///   @@id([field1, field2(sort: Desc)])
    ///   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this
    /// }
    /// ```
    pub fn id(&mut self, id: IdDefinition<'a>) {
        self.id = Some(id);
    }

    /// Add a model-level mapping.
    ///
    /// ```ignore
    /// model Foo {
    ///   @@map("1Foo")
    ///   ^^^^^^^^^^^^^ this
    /// }
    /// ```
    pub fn map(&mut self, map: impl Into<Cow<'a, str>>) {
        let mut fun = Function::new("map");
        fun.push_param(map.into());

        self.map = Some(BlockAttribute(fun));
    }

    /// The schema attribute of the model block
    ///
    /// ```ignore
    /// model Foo {
    ///   @@schema("public")
    ///   ^^^^^^^^^^^^^^^^^^ this
    /// }
    /// ```
    pub fn schema(&mut self, schema: impl Into<Cow<'a, str>>) {
        let mut fun = Function::new("schema");
        fun.push_param(schema.into());

        self.schema = Some(BlockAttribute(fun));
    }

    /// Comments the complete model block out.
    ///
    /// ```ignore
    /// // model Foo {
    /// //   id Int @id
    /// // }
    /// ```
    pub fn comment_out(&mut self) {
        self.commented_out = Commented::On
    }

    /// Push a new field to the end of the model.
    ///
    /// ```ignore
    /// model Foo {
    ///   id  Int    @id
    ///   foo String
    ///   ^^^^^^^^^^ this
    /// }
    /// ```
    pub fn push_field(&mut self, field: Field<'a>) {
        self.fields.push(field);
    }

    /// Push a new field to the beginning of the model.
    /// Extremely inefficient, prefer `push_field` if you can.
    ///
    /// ```ignore
    /// model Foo {
    ///   id  Int    @id
    ///   ^^^^^^^^^^^^^^ this
    ///   foo String
    /// }
    /// ```
    pub fn insert_field_front(&mut self, field: Field<'a>) {
        self.fields.insert(0, field);
    }

    /// Push a new index to the model.
    ///
    /// ```ignore
    /// model Foo {
    ///   @@index([field1, field2])
    ///   ^^^^^^^^^^^^^^^^^^^^^^^^^ this
    /// }
    /// ```
    pub fn push_index(&mut self, index: IndexDefinition<'a>) {
        self.indexes.push(index);
    }
}

impl<'a> fmt::Display for Model<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // Prefix everything with this, so if the model is commented out, so
        // is your line.
        let comment = self.commented_out;

        if let Some(ref docs) = self.documentation {
            docs.fmt(f)?;
        }

        writeln!(f, "{comment}model {} {{", self.name)?;

        for field in self.fields.iter() {
            writeln!(f, "{comment}{field}")?;
        }

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

        for index in self.indexes.iter() {
            writeln!(f, "{comment}{index}")?;
        }

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

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

        if let Some(ref schema) = self.schema {
            writeln!(f, "{comment}{schema}")?;
        }

        writeln!(f, "{comment}}}")?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use std::borrow::Cow;

    use crate::{datamodel::*, value::Function};
    use expect_test::expect;

    #[test]
    fn kitchen_sink() {
        let mut model = Model::new("Country");
        model.map("1Country");
        model.documentation("Do not fear death\nIf you love the trail of streaking fire\nDo not fear death\nIf you desire a speed king to become!");

        let mut field = Field::new("id", "String");

        let mut opts = IdFieldDefinition::default();

        opts.sort_order("Desc");
        opts.length(32);
        opts.clustered(false);

        field.id(opts);
        field.native_type("db", "VarChar", vec![String::from("255")]);
        field.default(DefaultValue::function(Function::new("uuid")));

        model.push_field(field);

        let mut field = Field::new("value", "Bytes");
        field.optional();
        field.documentation("NOPEUSKUNINGAS");
        field.default(DefaultValue::bytes(&[1u8, 2, 3, 4] as &[u8]));
        model.push_field(field);

        let mut field = Field::new("array", "Int");
        field.array();
        field.map("1array");
        field.default(DefaultValue::array(vec![1, 2, 3, 4]));
        model.push_field(field);

        let mut field = Field::new("konig", "King");
        field.unsupported();
        field.ignore();
        model.push_field(field);

        let mut field = Field::new("information", "Int");
        let mut opts = UniqueFieldAttribute::default();

        opts.sort_order("Desc");
        opts.length(32);
        opts.clustered(true);

        field.unique(opts);

        model.push_field(field);

        let mut relation = Relation::new();
        relation.fields(["information"].into_iter().map(ToOwned::to_owned).map(Cow::Owned));
        relation.references(["id"].into_iter().map(ToOwned::to_owned).map(Cow::Owned));
        relation.on_delete("Cascade");
        relation.on_update("Restrict");

        let mut field = Field::new("relfield", "Planet");
        field.relation(relation);

        model.push_field(field);

        let fields = ["foo", "bar"].iter().enumerate().map(|(i, name)| {
            if i == 1 {
                IndexFieldInput {
                    name: Cow::Borrowed(name),
                    sort_order: Some("Asc".into()),
                    length: Some(32),
                    ops: None,
                }
            } else {
                IndexFieldInput {
                    name: Cow::Borrowed(name),
                    sort_order: None,
                    length: None,
                    ops: None,
                }
            }
        });

        let mut id = IdDefinition::new(fields);
        id.name("primary");
        id.map("PKPK");
        id.clustered(false);
        model.id(id);

        let unique = IndexDefinition::unique(["foo", "bar"].iter().map(|s| IndexFieldInput::new(*s)));
        model.push_index(unique);

        let mut index = IndexDefinition::index(["foo", "bar"].iter().map(|s| IndexFieldInput::new(*s)));
        index.index_type("BTree");
        model.push_index(index);

        let fulltext = IndexDefinition::fulltext(["foo", "bar"].iter().map(|s| IndexFieldInput::new(*s)));
        model.push_index(fulltext);

        model.schema("public");
        model.ignore();

        let expected = expect![[r#"
            /// Do not fear death
            /// If you love the trail of streaking fire
            /// Do not fear death
            /// If you desire a speed king to become!
            model Country {
              id          String              @id(sort: Desc, length: 32, clustered: false) @default(uuid()) @db.VarChar(255)
              /// NOPEUSKUNINGAS
              value       Bytes?              @default("AQIDBA==")
              array       Int[]               @default([1, 2, 3, 4]) @map("1array")
              konig       Unsupported("King") @ignore
              information Int                 @unique(sort: Desc, length: 32, clustered: true)
              relfield    Planet              @relation(fields: [information], references: [id], onDelete: Cascade, onUpdate: Restrict)

              @@id([foo, bar(length: 32, sort: Asc)], name: "primary", map: "PKPK", clustered: false)
              @@unique([foo, bar])
              @@index([foo, bar], type: BTree)
              @@fulltext([foo, bar])
              @@map("1Country")
              @@ignore
              @@schema("public")
            }
        "#]];

        let rendered = psl::reformat(&model.to_string(), 2).unwrap();
        expected.assert_eq(&rendered);
    }

    #[test]
    fn commented_out() {
        let mut model = Model::new("Country");

        let mut field = Field::new("id", "String");
        field.id(IdFieldDefinition::default());
        field.native_type("db", "VarChar", vec![String::from("255")]);
        field.default(DefaultValue::function(Function::new("uuid")));
        model.push_field(field);

        model.schema("public");
        model.comment_out();

        let expected = expect![[r#"
            // model Country {
            // id String @id @default(uuid()) @db.VarChar(255)
            // @@schema("public")
            // }
        "#]];

        let rendered = psl::reformat(&model.to_string(), 2).unwrap();
        expected.assert_eq(&rendered);
    }
}