prost_reflect/dynamic/serde/
case.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/// Convert `src` from snake case to camel case, returning an error if roundtripping
/// back to snake case would not be possible.
pub(crate) fn snake_case_to_camel_case(dst: &mut String, src: &str) -> Result<(), ()> {
    let mut ucase_next = false;
    for ch in src.chars() {
        if ch.is_ascii_uppercase() {
            return Err(());
        }

        if ucase_next {
            let upper_ch = ch.to_ascii_uppercase();
            if upper_ch == ch {
                return Err(());
            }

            dst.push(upper_ch);
            ucase_next = false;
        } else if ch == '_' {
            ucase_next = true;
        } else {
            dst.push(ch)
        }
    }

    Ok(())
}

pub(crate) fn camel_case_to_snake_case(result: &mut String, part: &str) -> Result<(), ()> {
    for ch in part.chars() {
        if ch.is_ascii_uppercase() {
            result.push('_');
            result.push(ch.to_ascii_lowercase());
        } else if ch == '_' {
            return Err(());
        } else {
            result.push(ch);
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use proptest::prelude::*;

    use super::*;

    #[test]
    fn snake_to_camel() {
        let mut buf = String::new();

        snake_case_to_camel_case(&mut buf, "foo").unwrap();
        assert_eq!(&buf, "foo");
        buf.clear();

        snake_case_to_camel_case(&mut buf, "foo_bar").unwrap();
        assert_eq!(&buf, "fooBar");
        buf.clear();
    }

    #[test]
    fn camel_to_snake() {
        let mut buf = String::new();

        camel_case_to_snake_case(&mut buf, "foo").unwrap();
        assert_eq!(&buf, "foo");
        buf.clear();

        camel_case_to_snake_case(&mut buf, "fooBar").unwrap();
        assert_eq!(&buf, "foo_bar");
        buf.clear();
    }

    #[test]
    fn bad_roundtrips() {
        let mut buf = String::new();
        assert!(snake_case_to_camel_case(&mut buf, "fooBar").is_err());
        assert!(snake_case_to_camel_case(&mut buf, "foo_3_bar").is_err());
        assert!(snake_case_to_camel_case(&mut buf, "foo__bar").is_err());
    }

    proptest! {
        #[test]
        fn roundtrip_cases(snake_case in "[a-zA-Z0-9]+") {
            let mut camel_case = String::new();
            if snake_case_to_camel_case(&mut camel_case, &snake_case).is_ok() {
                let mut roundtripped_snake_case = String::new();
                camel_case_to_snake_case(&mut roundtripped_snake_case, &camel_case).unwrap();

                prop_assert_eq!(snake_case, roundtripped_snake_case);
            }
        }
    }
}