apollo_encoder/
ty.rs

1use std::fmt::{self, Display};
2
3/// Convenience Type_ implementation used when creating a Field.
4/// Can be a `NamedType`, a `NonNull` or a `List`.
5///
6/// This enum is resposible for encoding creating values such as `String!`, `[[[[String]!]!]!]!`, etc.
7///
8/// ### Example
9/// ```rust
10/// use apollo_encoder::{Type_};
11///
12/// let field_ty = Type_::NamedType {
13///     name: "String".to_string(),
14/// };
15///
16/// let list = Type_::List {
17///     ty: Box::new(field_ty),
18/// };
19///
20/// let non_null = Type_::NonNull { ty: Box::new(list) };
21///
22/// assert_eq!(non_null.to_string(), "[String]!");
23/// ```
24#[derive(Debug, PartialEq, Clone)]
25pub enum Type_ {
26    /// The Non-Null field type.
27    NonNull {
28        /// Null inner type.
29        ty: Box<Type_>,
30    },
31    /// The List field type.
32    List {
33        /// List inner type.
34        ty: Box<Type_>,
35    },
36    /// The Named field type.
37    NamedType {
38        /// NamedType type.
39        name: String,
40    },
41}
42
43impl Display for Type_ {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        match self {
46            Type_::List { ty } => {
47                write!(f, "[{ty}]")
48            }
49            Type_::NonNull { ty } => {
50                write!(f, "{ty}!")
51            }
52            Type_::NamedType { name } => write!(f, "{name}"),
53        }
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60    use pretty_assertions::assert_eq;
61
62    #[test]
63    fn encodes_simple_field_value() {
64        let field_ty = Type_::NamedType {
65            name: "String".to_string(),
66        };
67
68        assert_eq!(field_ty.to_string(), "String");
69    }
70
71    #[test]
72    fn encodes_list_field_value() {
73        let field_ty = Type_::NamedType {
74            name: "String".to_string(),
75        };
76
77        let list = Type_::List {
78            ty: Box::new(field_ty),
79        };
80
81        assert_eq!(list.to_string(), "[String]");
82    }
83
84    #[test]
85    fn encodes_non_null_list_field_value() {
86        let field_ty = Type_::NamedType {
87            name: "String".to_string(),
88        };
89
90        let list = Type_::List {
91            ty: Box::new(field_ty),
92        };
93
94        let non_null = Type_::NonNull { ty: Box::new(list) };
95
96        assert_eq!(non_null.to_string(), "[String]!");
97    }
98    #[test]
99    fn encodes_non_null_list_non_null_list_field_value() {
100        let field_ty = Type_::NamedType {
101            name: "String".to_string(),
102        };
103
104        let list = Type_::List {
105            ty: Box::new(field_ty),
106        };
107
108        let non_null = Type_::NonNull { ty: Box::new(list) };
109
110        let list_2 = Type_::List {
111            ty: Box::new(non_null),
112        };
113
114        let non_null_2 = Type_::NonNull {
115            ty: Box::new(list_2),
116        };
117
118        assert_eq!(non_null_2.to_string(), "[[String]!]!");
119    }
120}