graphql_client_codegen/
normalization.rs

1use heck::ToUpperCamelCase;
2use std::borrow::Cow;
3
4/// Normalization conventions available for generated code.
5#[derive(Debug, PartialEq, Eq, Clone, Copy)]
6pub enum Normalization {
7    /// Use naming conventions from the schema.
8    None,
9    /// Use Rust naming conventions for generated code.
10    Rust,
11}
12
13impl Normalization {
14    fn camel_case(self, name: &str) -> Cow<'_, str> {
15        match self {
16            Self::None => name.into(),
17            Self::Rust => name.to_upper_camel_case().into(),
18        }
19    }
20
21    pub(crate) fn operation(self, op: &str) -> Cow<'_, str> {
22        self.camel_case(op)
23    }
24
25    pub(crate) fn enum_variant(self, enm: &str) -> Cow<'_, str> {
26        self.camel_case(enm)
27    }
28
29    pub(crate) fn enum_name(self, enm: &str) -> Cow<'_, str> {
30        self.camel_case(enm)
31    }
32
33    fn field_type_impl(self, fty: &str) -> Cow<'_, str> {
34        if fty == "ID" || fty.starts_with("__") {
35            fty.into()
36        } else {
37            self.camel_case(fty)
38        }
39    }
40
41    pub(crate) fn field_type(self, fty: &str) -> Cow<'_, str> {
42        self.field_type_impl(fty)
43    }
44
45    pub(crate) fn input_name(self, inm: &str) -> Cow<'_, str> {
46        self.camel_case(inm)
47    }
48
49    pub(crate) fn scalar_name(self, snm: &str) -> Cow<'_, str> {
50        self.camel_case(snm)
51    }
52}
53
54impl std::str::FromStr for Normalization {
55    type Err = ();
56
57    fn from_str(s: &str) -> Result<Self, ()> {
58        match s.trim() {
59            "none" => Ok(Normalization::None),
60            "rust" => Ok(Normalization::Rust),
61            _ => Err(()),
62        }
63    }
64}