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
use crate::value::{Constant, Documentation};
use std::{borrow::Cow, fmt};

use super::Field;

/// A type block in a PSL file.
#[derive(Debug)]
pub struct CompositeType<'a> {
    name: Constant<Cow<'a, str>>,
    documentation: Option<Documentation<'a>>,
    fields: Vec<Field<'a>>,
}

impl<'a> CompositeType<'a> {
    /// Create a new type declaration block. Will not be valid without
    /// adding at least one field.
    ///
    /// ```ignore
    /// type Address {
    /// //   ^^^^^^^ name
    /// }
    /// ```
    pub fn new(name: impl Into<Cow<'a, str>>) -> Self {
        let name = Constant::new_no_validate(name.into());

        Self {
            name,
            documentation: None,
            fields: Vec::new(),
        }
    }

    /// Documentation of the type.
    ///
    /// ```ignore
    /// /// This is the documentation.
    /// type Foo {
    ///   ....
    /// }
    /// ```
    pub fn documentation(&mut self, documentation: impl Into<Cow<'a, str>>) {
        self.documentation = Some(Documentation(documentation.into()));
    }

    /// Add a new field to the type.
    ///
    /// ```ignore
    /// type Foo {
    ///     bar String
    /// //  ^^^^^^^^^^ this
    /// }
    /// ```
    pub fn push_field(&mut self, field: Field<'a>) {
        self.fields.push(field);
    }
}

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

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

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

        f.write_str("}\n")?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use expect_test::expect;

    use crate::datamodel::*;

    #[test]
    fn kitchen_sink() {
        let mut composite_type = CompositeType::new("Address");
        composite_type.documentation("...so many tears 🎵");

        let mut field = Field::new("Street", "String");
        field.native_type("db", "VarChar", vec!["255".into()]);
        field.default(DefaultValue::text("Prenzlauer Allee 193"));
        field.map("Shield");
        composite_type.push_field(field);

        let field = Field::new("Number", "Int");
        composite_type.push_field(field);

        let mut field = Field::new("City", "String");
        field.optional();
        field.documentation("...soooooooo many tears 🎵");
        composite_type.push_field(field);

        let mut field = Field::new("Other", "String");
        field.array();
        composite_type.push_field(field);

        let mut field = Field::new("Invalid", "Float");
        field.map("1Invalid");
        composite_type.push_field(field);

        let mut field = Field::new("11111", "Float");
        field.commented_out();
        field.map("11111");
        composite_type.push_field(field);

        let expected = expect![[r#"
            /// ...so many tears 🎵
            type Address {
              Street  String   @default("Prenzlauer Allee 193") @map("Shield") @db.VarChar(255)
              Number  Int
              /// ...soooooooo many tears 🎵
              City    String?
              Other   String[]
              Invalid Float    @map("1Invalid")
              // 11111 Float @map("11111")
            }
        "#]];

        let rendered = psl::reformat(&format!("{composite_type}"), 2).unwrap();
        expected.assert_eq(&rendered);
    }
}