use std::fmt::{self, Display};
#[derive(Debug, PartialEq, Clone)]
pub enum Type_ {
NonNull {
ty: Box<Type_>,
},
List {
ty: Box<Type_>,
},
NamedType {
name: String,
},
}
impl Display for Type_ {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Type_::List { ty } => {
write!(f, "[{}]", ty)
}
Type_::NonNull { ty } => {
write!(f, "{}!", ty)
}
Type_::NamedType { name } => write!(f, "{}", name),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn encodes_simple_field_value() {
let field_ty = Type_::NamedType {
name: "String".to_string(),
};
assert_eq!(field_ty.to_string(), "String");
}
#[test]
fn encodes_list_field_value() {
let field_ty = Type_::NamedType {
name: "String".to_string(),
};
let list = Type_::List {
ty: Box::new(field_ty),
};
assert_eq!(list.to_string(), "[String]");
}
#[test]
fn encodes_non_null_list_field_value() {
let field_ty = Type_::NamedType {
name: "String".to_string(),
};
let list = Type_::List {
ty: Box::new(field_ty),
};
let non_null = Type_::NonNull { ty: Box::new(list) };
assert_eq!(non_null.to_string(), "[String]!");
}
#[test]
fn encodes_non_null_list_non_null_list_field_value() {
let field_ty = Type_::NamedType {
name: "String".to_string(),
};
let list = Type_::List {
ty: Box::new(field_ty),
};
let non_null = Type_::NonNull { ty: Box::new(list) };
let list_2 = Type_::List {
ty: Box::new(non_null),
};
let non_null_2 = Type_::NonNull {
ty: Box::new(list_2),
};
assert_eq!(non_null_2.to_string(), "[[String]!]!");
}
}