1
/// This file contains `Rust` types that represents an entry on the `CanyonRegister`
2
/// where `Canyon` tracks the user types that has to manage
3

            
4
pub const NUMERIC_PK_DATATYPE: [&str; 6] = ["i16", "u16", "i32", "u32", "i64", "u64"];
5

            
6
/// Gets the necessary identifiers of a CanyonEntity to make it the comparative
7
/// against the database schemas
8
#[derive(Debug, Clone, Default)]
9
pub struct CanyonRegisterEntity<'a> {
10
    pub entity_name: &'a str,
11
    pub entity_db_table_name: &'a str,
12
    pub user_schema_name: Option<&'a str>,
13
    pub entity_fields: Vec<CanyonRegisterEntityField>,
14
}
15

            
16
/// Complementary type for a field that represents a struct field that maps
17
/// some real database column data
18
#[derive(Debug, Clone, Default)]
19
pub struct CanyonRegisterEntityField {
20
    pub field_name: String,
21
    pub field_type: String,
22
    pub annotations: Vec<String>,
23
}
24

            
25
impl CanyonRegisterEntityField {
26
    /// Return if the field is autoincremental
27
    pub fn is_autoincremental(&self) -> bool {
28
        let has_pk_annotation = self
29
            .annotations
30
            .iter()
31
            .find(|a| a.starts_with("Annotation: PrimaryKey"));
32

            
33
        let pk_is_autoincremental = match has_pk_annotation {
34
            Some(annotation) => annotation.contains("true"),
35
            None => false,
36
        };
37

            
38
        NUMERIC_PK_DATATYPE.contains(&self.field_type.as_str()) && pk_is_autoincremental
39
    }
40

            
41
    /// Return the nullability of a the field
42
    pub fn is_nullable(&self) -> bool {
43
        self.field_type.to_uppercase().starts_with("OPTION")
44
    }
45
}