Skip to main content

cynic_codegen/schema/
names.rs

1use std::borrow::Cow;
2
3use crate::idents::RenamableFieldIdent;
4
5#[derive(Debug, Clone, PartialEq, Eq, Hash)]
6#[cfg_attr(
7    feature = "rkyv",
8    derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
9)]
10pub struct FieldName<'a> {
11    #[cfg_attr(feature = "rkyv", rkyv(with = rkyv::with::AsOwned))]
12    pub(super) graphql_name: Cow<'a, str>,
13}
14
15impl<'a> FieldName<'a> {
16    pub fn new(graphql_name: &'a str) -> Self {
17        FieldName {
18            graphql_name: Cow::Borrowed(graphql_name),
19        }
20    }
21
22    pub fn as_str(&'a self) -> &'a str {
23        self.graphql_name.as_ref()
24    }
25
26    pub fn to_literal(&self) -> proc_macro2::Literal {
27        proc_macro2::Literal::string(self.graphql_name.as_ref())
28    }
29}
30
31impl PartialEq<proc_macro2::Ident> for FieldName<'_> {
32    fn eq(&self, other: &proc_macro2::Ident) -> bool {
33        other == self.graphql_name.as_ref()
34    }
35}
36
37impl PartialEq<str> for FieldName<'_> {
38    fn eq(&self, other: &str) -> bool {
39        self.graphql_name == other
40    }
41}
42
43impl PartialEq<String> for FieldName<'_> {
44    fn eq(&self, other: &String) -> bool {
45        self.graphql_name.as_ref() == other
46    }
47}
48
49impl PartialEq<RenamableFieldIdent> for FieldName<'_> {
50    fn eq(&self, other: &RenamableFieldIdent) -> bool {
51        self.graphql_name == other.graphql_name()
52    }
53}