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

/// Represents a default specified on a field.
#[derive(Clone, PartialEq, Debug)]
pub struct DefaultValue {
    pub kind: DefaultKind,
    pub db_name: Option<String>,
}

/// Represents a default specified on a field.
#[derive(Clone, PartialEq)]
pub enum DefaultKind {
    /// a static value, e.g. `@default(1)`
    Single(PrismaValue),
    /// a dynamic value, e.g. `@default(uuid())`
    Expression(ValueGenerator),
}

impl DefaultKind {
    /// Does this match @default(autoincrement())?
    pub fn is_autoincrement(&self) -> bool {
        matches!(self, DefaultKind::Expression(generator) if generator.name == "autoincrement")
    }

    /// Does this match @default(cuid(_))?
    pub fn is_cuid(&self) -> bool {
        matches!(self, DefaultKind::Expression(generator) if generator.name == "cuid")
    }

    /// Does this match @default(dbgenerated(_))?
    pub fn is_dbgenerated(&self) -> bool {
        matches!(self, DefaultKind::Expression(generator) if generator.name == "dbgenerated")
    }

    /// Does this match @default(nanoid(_))?
    pub fn is_nanoid(&self) -> bool {
        matches!(self, DefaultKind::Expression(generator) if generator.name.starts_with("nanoid("))
    }

    /// Does this match @default(now())?
    pub fn is_now(&self) -> bool {
        matches!(self, DefaultKind::Expression(generator) if generator.name == "now")
    }

    /// Does this match @default(uuid(_))?
    pub fn is_uuid(&self) -> bool {
        matches!(self, DefaultKind::Expression(generator) if generator.name == "uuid")
    }

    pub fn unwrap_single(self) -> PrismaValue {
        match self {
            DefaultKind::Single(val) => val,
            _ => panic!("called DefaultValue::unwrap_single() on wrong variant"),
        }
    }

    // Returns the dbgenerated function for a default value
    // intended for primary key values!
    pub fn to_dbgenerated_func(&self) -> Option<String> {
        match self {
            DefaultKind::Expression(ref expr) if expr.is_dbgenerated() => {
                expr.args.first().map(|val| val.1.to_string())
            }
            _ => None,
        }
    }

    /// Returns either a copy of the contained single value or produces a new
    /// value as defined by the expression.
    #[cfg(feature = "default_generators")]
    pub fn get(&self) -> Option<PrismaValue> {
        match self {
            DefaultKind::Single(ref v) => Some(v.clone()),
            DefaultKind::Expression(ref g) => g.generate(),
        }
    }
}

impl DefaultValue {
    pub fn as_expression(&self) -> Option<&ValueGenerator> {
        match self.kind {
            DefaultKind::Expression(ref expr) => Some(expr),
            _ => None,
        }
    }

    pub fn as_single(&self) -> Option<&PrismaValue> {
        match self.kind {
            DefaultKind::Single(ref v) => Some(v),
            _ => None,
        }
    }

    /// Does this match @default(autoincrement())?
    pub fn is_autoincrement(&self) -> bool {
        self.kind.is_autoincrement()
    }

    /// Does this match @default(cuid(_))?
    pub fn is_cuid(&self) -> bool {
        self.kind.is_cuid()
    }

    /// Does this match @default(dbgenerated(_))?
    pub fn is_dbgenerated(&self) -> bool {
        self.kind.is_dbgenerated()
    }

    /// Does this match @default(nanoid(_))?
    pub fn is_nanoid(&self) -> bool {
        self.kind.is_nanoid()
    }

    /// Does this match @default(now())?
    pub fn is_now(&self) -> bool {
        self.kind.is_now()
    }

    /// Does this match @default(uuid(_))?
    pub fn is_uuid(&self) -> bool {
        self.kind.is_uuid()
    }

    pub fn new_expression(generator: ValueGenerator) -> Self {
        let kind = DefaultKind::Expression(generator);

        Self { kind, db_name: None }
    }

    pub fn new_single(value: PrismaValue) -> Self {
        let kind = DefaultKind::Single(value);

        Self { kind, db_name: None }
    }

    pub fn set_db_name(&mut self, name: impl ToString) {
        self.db_name = Some(name.to_string());
    }

    /// The default value constraint name.
    pub fn db_name(&self) -> Option<&str> {
        self.db_name.as_deref()
    }
}

#[derive(Clone)]
pub struct ValueGenerator {
    name: String,
    args: Vec<(Option<String>, PrismaValue)>,
    generator: ValueGeneratorFn,
}

impl ValueGenerator {
    pub fn new(name: String, args: Vec<(Option<String>, PrismaValue)>) -> Result<Self, String> {
        let generator = ValueGeneratorFn::new(name.as_ref())?;

        Ok(ValueGenerator { name, args, generator })
    }

    pub fn new_autoincrement() -> Self {
        ValueGenerator::new("autoincrement".to_owned(), vec![]).unwrap()
    }

    pub fn new_sequence(args: Vec<(Option<String>, PrismaValue)>) -> Self {
        ValueGenerator::new("sequence".to_owned(), args).unwrap()
    }

    pub fn new_dbgenerated(description: String) -> Self {
        if description.trim_matches('\0').is_empty() {
            ValueGenerator::new("dbgenerated".to_owned(), Vec::new()).unwrap()
        } else {
            ValueGenerator::new("dbgenerated".to_owned(), vec![(None, PrismaValue::String(description))]).unwrap()
        }
    }

    pub fn new_auto() -> Self {
        ValueGenerator::new("auto".to_owned(), Vec::new()).unwrap()
    }

    pub fn new_now() -> Self {
        ValueGenerator::new("now".to_owned(), vec![]).unwrap()
    }

    pub fn new_cuid() -> Self {
        ValueGenerator::new("cuid".to_owned(), vec![]).unwrap()
    }

    pub fn new_uuid() -> Self {
        ValueGenerator::new("uuid".to_owned(), vec![]).unwrap()
    }

    pub fn new_nanoid(length: Option<u8>) -> Self {
        if let Some(length) = length {
            ValueGenerator::new(
                format!("nanoid({length})"),
                vec![(None, PrismaValue::Int(length.into()))],
            )
            .unwrap()
        } else {
            ValueGenerator::new("nanoid()".to_owned(), vec![]).unwrap()
        }
    }

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

    pub fn args(&self) -> &[(Option<String>, PrismaValue)] {
        &self.args
    }

    pub fn generator(&self) -> ValueGeneratorFn {
        self.generator
    }

    pub fn as_dbgenerated(&self) -> Option<&str> {
        if !(self.is_dbgenerated()) {
            return None;
        }

        self.args.first().and_then(|v| v.1.as_string())
    }

    #[cfg(feature = "default_generators")]
    pub fn generate(&self) -> Option<PrismaValue> {
        self.generator.invoke()
    }

    pub fn is_dbgenerated(&self) -> bool {
        self.name == "dbgenerated"
    }

    pub fn is_autoincrement(&self) -> bool {
        self.name == "autoincrement" || self.name == "sequence"
    }
}

#[derive(Clone, Copy, PartialEq)]
pub enum ValueGeneratorFn {
    Uuid,
    Cuid,
    Nanoid(Option<u8>),
    Now,
    Autoincrement,
    DbGenerated,
    Auto,
}

impl ValueGeneratorFn {
    fn new(name: &str) -> std::result::Result<Self, String> {
        match name {
            "cuid" => Ok(Self::Cuid),
            "uuid" => Ok(Self::Uuid),
            "now" => Ok(Self::Now),
            "autoincrement" => Ok(Self::Autoincrement),
            "sequence" => Ok(Self::Autoincrement),
            "dbgenerated" => Ok(Self::DbGenerated),
            "auto" => Ok(Self::Auto),
            name if name.starts_with("nanoid(") => Ok(Self::Nanoid(name[7..name.len() - 1].parse::<u8>().ok())),
            _ => Err(format!("The function {name} is not a known function.")),
        }
    }

    #[cfg(feature = "default_generators")]
    fn invoke(&self) -> Option<PrismaValue> {
        match self {
            Self::Uuid => Some(Self::generate_uuid()),
            Self::Cuid => Some(Self::generate_cuid()),
            Self::Nanoid(length) => Some(Self::generate_nanoid(length)),
            Self::Now => Some(Self::generate_now()),
            Self::Autoincrement => None,
            Self::DbGenerated => None,
            Self::Auto => None,
        }
    }

    #[cfg(feature = "default_generators")]
    fn generate_cuid() -> PrismaValue {
        #[allow(deprecated)]
        PrismaValue::String(cuid::cuid().unwrap())
    }

    #[cfg(feature = "default_generators")]
    fn generate_uuid() -> PrismaValue {
        PrismaValue::Uuid(uuid::Uuid::new_v4())
    }

    #[cfg(feature = "default_generators")]
    fn generate_nanoid(length: &Option<u8>) -> PrismaValue {
        if length.is_some() {
            let value: usize = usize::from(length.unwrap());
            PrismaValue::String(nanoid::nanoid!(value))
        } else {
            PrismaValue::String(nanoid::nanoid!())
        }
    }

    #[cfg(feature = "default_generators")]
    fn generate_now() -> PrismaValue {
        PrismaValue::DateTime(chrono::Utc::now().into())
    }
}

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

impl fmt::Debug for DefaultKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self {
            DefaultKind::Single(ref v) => write!(f, "DefaultValue::Single({v:?})"),
            DefaultKind::Expression(g) => write!(f, "DefaultValue::Expression({}(){:?})", g.name(), g.args),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{DefaultValue, ValueGenerator};

    #[test]
    fn default_value_is_autoincrement() {
        let auto_increment_default = DefaultValue::new_expression(ValueGenerator::new_autoincrement());

        assert!(auto_increment_default.is_autoincrement());
    }

    #[test]
    fn default_value_is_now() {
        let now_default = DefaultValue::new_expression(ValueGenerator::new_now());

        assert!(now_default.is_now());
        assert!(!now_default.is_autoincrement());
    }

    #[test]
    fn default_value_is_uuid() {
        let uuid_default = DefaultValue::new_expression(ValueGenerator::new_uuid());

        assert!(uuid_default.is_uuid());
        assert!(!uuid_default.is_autoincrement());
    }

    #[test]
    fn default_value_is_cuid() {
        let cuid_default = DefaultValue::new_expression(ValueGenerator::new_cuid());

        assert!(cuid_default.is_cuid());
        assert!(!cuid_default.is_now());
    }

    #[test]
    fn default_value_is_nanoid() {
        let nanoid_default = DefaultValue::new_expression(ValueGenerator::new_nanoid(None));

        assert!(nanoid_default.is_nanoid());
        assert!(!nanoid_default.is_cuid());
    }

    #[test]
    fn default_value_is_dbgenerated() {
        let db_generated_default = DefaultValue::new_expression(ValueGenerator::new_dbgenerated("test".to_string()));

        assert!(db_generated_default.is_dbgenerated());
        assert!(!db_generated_default.is_now());
        assert!(!db_generated_default.is_autoincrement());
    }
}