taitan_orm_trait/
field.rs

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
use std::borrow::Cow;

#[derive(Debug, PartialEq, Clone)]
pub struct FieldName {
    pub name: Cow<'static, str>,
    pub is_null: bool,
    database_field_alias: Option<Cow<'static, str>>,
}

impl FieldName {
    pub fn new(name: Cow<'static, str>, is_null: bool) -> Self {
        Self { name, is_null, database_field_alias: None }
    }

    pub fn from_str(name: &'static str, is_null: bool) -> Self {
        Self {
            name: Cow::Borrowed(name),
            is_null,
            database_field_alias: None,
        }
    }

    pub fn with_alias(name: Cow<'static, str>, is_null: bool, database_field_alias: Option<&'static str>) -> Self {
        match database_field_alias {
            Some(database_field_alias) => {
                Self {
                    name,
                    is_null,
                    database_field_alias: Some(Cow::Borrowed(database_field_alias)),
                }
            }
            None => {
                Self {
                    name,
                    is_null,
                    database_field_alias: None
                }
            }
        }
    }

    pub fn database_field_name(&self) -> &str {
        match &self.database_field_alias {
            Some(database_field_name) => database_field_name,
            None => &self.name,
        }
    }
}

#[cfg(test)]
mod test {
    use crate::field::FieldName;

    #[test]
    pub fn test_field_name() {
        let field_name = FieldName::from_str("foo", true);
    }
}