Skip to main content

serde_generate/
common.rs

1// Copyright (c) Facebook, Inc. and its affiliates
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4#[cfg(any(
5    feature = "csharp",
6    feature = "dart",
7    feature = "golang",
8    feature = "java",
9    feature = "kotlin",
10    feature = "swift",
11    feature = "typescript",
12))]
13pub(crate) fn mangle_type(format: &serde_reflection::Format) -> String {
14    use serde_reflection::Format::*;
15    match format {
16        TypeName(x) => x.to_string(),
17        Unit => "unit".into(),
18        Bool => "bool".into(),
19        I8 => "i8".into(),
20        I16 => "i16".into(),
21        I32 => "i32".into(),
22        I64 => "i64".into(),
23        I128 => "i128".into(),
24        U8 => "u8".into(),
25        U16 => "u16".into(),
26        U32 => "u32".into(),
27        U64 => "u64".into(),
28        U128 => "u128".into(),
29        F32 => "f32".into(),
30        F64 => "f64".into(),
31        Char => "char".into(),
32        Str => "str".into(),
33        Bytes => "bytes".into(),
34
35        Option(format) => format!("option_{}", mangle_type(format)),
36        Seq(format) => format!("vector_{}", mangle_type(format)),
37        Map { key, value } => format!("map_{}_to_{}", mangle_type(key), mangle_type(value)),
38        Tuple(formats) => format!(
39            "tuple{}_{}",
40            formats.len(),
41            formats
42                .iter()
43                .map(mangle_type)
44                .collect::<Vec<_>>()
45                .join("_")
46        ),
47        TupleArray { content, size } => format!("array{}_{}_array", size, mangle_type(content)),
48        Variable(_) => panic!("unexpected value"),
49    }
50}
51
52#[cfg(feature = "ocaml")]
53pub(crate) fn uppercase_first_letter(s: &str) -> String {
54    let mut c = s.chars();
55    match c.next() {
56        None => String::new(),
57        Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
58    }
59}
60
61#[cfg(feature = "swift")]
62pub(crate) fn lowercase_first_letter(s: &str) -> String {
63    let mut c = s.chars();
64    match c.next() {
65        None => String::new(),
66        Some(f) => f.to_lowercase().collect::<String>() + c.as_str(),
67    }
68}