Skip to main content

appstruct_core/
naming.rs

1#[must_use]
2pub fn is_app_name(value: &str) -> bool {
3    value
4        .bytes()
5        .next()
6        .is_some_and(|first| first.is_ascii_lowercase())
7        && value
8            .bytes()
9            .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-')
10}
11
12#[must_use]
13pub fn is_rust_type_name(value: &str) -> bool {
14    value
15        .bytes()
16        .next()
17        .is_some_and(|first| first.is_ascii_uppercase())
18        && value.bytes().all(|byte| byte.is_ascii_alphanumeric())
19        && !is_rust_keyword(value)
20}
21
22#[must_use]
23pub fn is_rust_field_name(value: &str) -> bool {
24    is_sql_name(value) && !is_rust_keyword(value)
25}
26
27fn is_rust_keyword(value: &str) -> bool {
28    matches!(
29        value,
30        "Self"
31            | "abstract"
32            | "as"
33            | "async"
34            | "await"
35            | "become"
36            | "box"
37            | "break"
38            | "const"
39            | "continue"
40            | "crate"
41            | "do"
42            | "dyn"
43            | "else"
44            | "enum"
45            | "extern"
46            | "false"
47            | "final"
48            | "fn"
49            | "for"
50            | "gen"
51            | "if"
52            | "impl"
53            | "in"
54            | "let"
55            | "loop"
56            | "macro"
57            | "match"
58            | "mod"
59            | "move"
60            | "mut"
61            | "override"
62            | "priv"
63            | "pub"
64            | "ref"
65            | "return"
66            | "self"
67            | "static"
68            | "struct"
69            | "super"
70            | "trait"
71            | "true"
72            | "try"
73            | "type"
74            | "typeof"
75            | "unsafe"
76            | "unsized"
77            | "use"
78            | "virtual"
79            | "where"
80            | "while"
81            | "yield"
82    )
83}
84
85#[must_use]
86pub fn is_sql_name(value: &str) -> bool {
87    value
88        .bytes()
89        .next()
90        .is_some_and(|first| first.is_ascii_lowercase())
91        && value
92            .bytes()
93            .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'_')
94}
95
96#[must_use]
97pub fn to_snake_case(value: &str) -> String {
98    let mut output = String::new();
99    for (index, character) in value.chars().enumerate() {
100        if character.is_ascii_uppercase() {
101            if index > 0 {
102                output.push('_');
103            }
104            output.push(character.to_ascii_lowercase());
105        } else {
106            output.push(character);
107        }
108    }
109    output
110}
111
112#[must_use]
113pub fn pluralize(value: &str) -> String {
114    if value.ends_with('s') {
115        format!("{value}es")
116    } else {
117        format!("{value}s")
118    }
119}
120
121#[cfg(test)]
122mod tests {
123    use super::*;
124
125    #[test]
126    fn normalizes_type_names() {
127        assert_eq!(to_snake_case("Project"), "project");
128        assert_eq!(to_snake_case("ProjectMember"), "project_member");
129    }
130
131    #[test]
132    fn validates_names() {
133        assert!(is_app_name("project-hub"));
134        assert!(!is_app_name("ProjectHub"));
135        assert!(is_rust_type_name("Project"));
136        assert!(is_sql_name("project_owner"));
137        assert!(!is_rust_field_name("type"));
138    }
139}