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
use serde::{Deserialize, Serialize};
use welds::detect::ColumnDef;

#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
pub struct Column {
    pub db_name: String,
    pub db_type: String,
    pub model_name: String,
    #[serde(default)]
    pub is_null: bool,
    #[serde(default)]
    pub primary_key: bool,
    #[serde(default)]
    pub writeable: bool,
}

impl Column {
    pub fn new(def: &ColumnDef) -> Self {
        use inflector::Inflector;
        Column {
            db_name: def.name().to_owned(),
            db_type: def.ty().to_owned(),
            model_name: def.name().to_snake_case(),
            is_null: def.null(),
            primary_key: def.primary_key(),
            writeable: def.updatable(),
        }
    }

    pub fn update_from(&mut self, def: &ColumnDef) {
        self.db_name = def.name().to_owned();
        self.db_type = def.ty().to_owned();
        self.is_null = def.null();
        self.primary_key = def.primary_key();
        self.writeable = def.updatable();
    }
}