apollo_encoder/
enum_value.rs1use std::fmt;
2
3use crate::{Directive, StringValue};
4
5#[derive(Debug, PartialEq, Clone)]
34pub struct EnumValue {
35 name: String,
37 description: Option<StringValue>,
39 directives: Vec<Directive>,
41}
42
43impl EnumValue {
44 pub fn new(name: String) -> Self {
46 Self {
47 name,
48 description: None,
49 directives: Vec::new(),
50 }
51 }
52
53 pub fn description(&mut self, description: String) {
55 self.description = Some(StringValue::Field {
56 source: description,
57 });
58 }
59
60 pub fn directive(&mut self, directive: Directive) {
62 self.directives.push(directive)
63 }
64}
65
66impl fmt::Display for EnumValue {
67 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68 if let Some(description) = &self.description {
69 writeln!(f, "{description}")?;
70 }
71 write!(f, " {}", self.name)?;
72
73 for directive in &self.directives {
74 write!(f, " {directive}")?;
75 }
76
77 Ok(())
78 }
79}
80
81#[cfg(test)]
82mod tests {
83 use crate::{Argument, Value};
84
85 use super::*;
86 use pretty_assertions::assert_eq;
87
88 #[test]
89 fn it_encodes_an_enum_value() {
90 let enum_ty = EnumValue::new("CAT_TREE".to_string());
91 assert_eq!(enum_ty.to_string(), " CAT_TREE");
92 }
93
94 #[test]
95 fn it_encodes_an_enum_value_with_desciption() {
96 let mut enum_ty = EnumValue::new("CAT_TREE".to_string());
97 enum_ty.description("Top bunk of a cat tree.".to_string());
98 assert_eq!(
99 enum_ty.to_string(),
100 r#" "Top bunk of a cat tree."
101 CAT_TREE"#
102 );
103 }
104
105 #[test]
106 fn it_encodes_an_enum_value_with_directive() {
107 let mut enum_ty = EnumValue::new("CARDBOARD_BOX".to_string());
108 let mut directive = Directive::new(String::from("testDirective"));
109 directive.arg(Argument::new(
110 String::from("first"),
111 Value::List(vec![Value::Int(1), Value::Int(2)]),
112 ));
113 enum_ty.description("Box nap\nspot.".to_string());
114 enum_ty.directive(directive);
115
116 assert_eq!(
117 enum_ty.to_string(),
118 r#" """
119 Box nap
120 spot.
121 """
122 CARDBOARD_BOX @testDirective(first: [1, 2])"#
123 );
124 }
125
126 #[test]
127 fn it_encodes_an_enum_value_with_deprecated_block_string_value() {
128 let mut enum_ty = EnumValue::new("CARDBOARD_BOX".to_string());
129 enum_ty.description("Box nap\nspot.".to_string());
130 let mut deprecated_directive = Directive::new(String::from("deprecated"));
131 deprecated_directive.arg(Argument::new(
132 String::from("reason"),
133 Value::String(String::from(r#"Box was "recycled"."#)),
134 ));
135 enum_ty.directive(deprecated_directive);
136
137 assert_eq!(
138 enum_ty.to_string(),
139 r#" """
140 Box nap
141 spot.
142 """
143 CARDBOARD_BOX @deprecated(reason: """Box was "recycled".""")"#
144 );
145 }
146}