sea_orm_codegen/entity/
conjunct_relation.rs

1use heck::{ToSnakeCase, ToUpperCamelCase};
2use proc_macro2::Ident;
3use quote::format_ident;
4
5use crate::util::escape_rust_keyword;
6
7#[derive(Clone, Debug)]
8pub struct ConjunctRelation {
9    pub(crate) via: String,
10    pub(crate) to: String,
11}
12
13impl ConjunctRelation {
14    pub fn get_via_snake_case(&self) -> Ident {
15        format_ident!("{}", escape_rust_keyword(self.via.to_snake_case()))
16    }
17
18    pub fn get_to_snake_case(&self) -> Ident {
19        format_ident!("{}", escape_rust_keyword(self.to.to_snake_case()))
20    }
21
22    pub fn get_to_upper_camel_case(&self) -> Ident {
23        format_ident!("{}", self.to.to_upper_camel_case())
24    }
25}
26
27#[cfg(test)]
28mod tests {
29    use crate::ConjunctRelation;
30
31    fn setup() -> Vec<ConjunctRelation> {
32        vec![
33            ConjunctRelation {
34                via: "cake_filling".to_owned(),
35                to: "cake".to_owned(),
36            },
37            ConjunctRelation {
38                via: "cake_filling".to_owned(),
39                to: "filling".to_owned(),
40            },
41        ]
42    }
43
44    #[test]
45    fn test_get_via_snake_case() {
46        let conjunct_relations = setup();
47        let via_vec = vec!["cake_filling", "cake_filling"];
48        for (con_rel, via) in conjunct_relations.into_iter().zip(via_vec) {
49            assert_eq!(con_rel.get_via_snake_case(), via);
50        }
51    }
52
53    #[test]
54    fn test_get_to_snake_case() {
55        let conjunct_relations = setup();
56        let to_vec = vec!["cake", "filling"];
57        for (con_rel, to) in conjunct_relations.into_iter().zip(to_vec) {
58            assert_eq!(con_rel.get_to_snake_case(), to);
59        }
60    }
61
62    #[test]
63    fn test_get_to_upper_camel_case() {
64        let conjunct_relations = setup();
65        let to_vec = vec!["Cake", "Filling"];
66        for (con_rel, to) in conjunct_relations.into_iter().zip(to_vec) {
67            assert_eq!(con_rel.get_to_upper_camel_case(), to);
68        }
69    }
70}