shape/display.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
use std::fmt::Debug;
use std::fmt::Display;
use super::helpers::quote_string;
use super::NamedShapePathKey;
use super::Shape;
use super::ShapeCase;
impl Shape {
/// Returns a string representation of the [`Shape`].
///
/// Please note: this display format does not imply an input syntax or
/// parser for the shape language. To create new [`Shape`] elements, use the
/// various `Shape::*` helper functions.
pub fn pretty_print(&self) -> String {
self.case.pretty_print()
}
}
impl ShapeCase {
pub fn pretty_print(&self) -> String {
match self {
Self::Bool(Some(b)) => b.to_string(),
Self::Bool(None) => "Bool".to_string(),
Self::String(Some(s)) => quote_string(s.as_str()),
Self::String(None) => "String".to_string(),
Self::Int(Some(i)) => i.to_string(),
Self::Int(None) => "Int".to_string(),
Self::Float => "Float".to_string(),
Self::Null => "null".to_string(), // No typo: JSON null is lowercase.
// There may be some argument for using lower-case "none" here,
// since None is not a reserved/built-in GraphQL type name like
// Bool, String, Int, and Float are, so someone could define a
// custom GraphQL None type that would collide with this Shape name.
Self::None => "None".to_string(),
Self::Array { prefix, tail } => {
if prefix.is_empty() {
if tail.is_none() {
"[]".to_string()
} else {
format!("List<{}>", tail.pretty_print())
}
} else {
let mut result = "[".to_string();
for (i, shape) in prefix.iter().enumerate() {
if i > 0 {
result.push_str(", ");
}
result.push_str(&shape.pretty_print());
}
if !tail.is_none() {
result.push_str(&format!(", ...List<{}>", tail.pretty_print()));
}
result.push_str("]");
result
}
}
Self::Object { fields, rest } => {
if fields.is_empty() && !rest.is_none() {
format!("Dict<{}>", rest.pretty_print())
} else {
let mut result = "{".to_string();
if !fields.is_empty() {
result.push(' ');
let mut sorted_field_names = fields.keys().collect::<Vec<_>>();
sorted_field_names.sort();
for (i, field_name) in sorted_field_names.into_iter().enumerate() {
if i > 0 {
result.push_str(", ");
}
result.push_str(&format!(
"{}: {}",
field_name,
fields[field_name].pretty_print()
));
}
}
if !rest.is_none() {
if !fields.is_empty() {
result.push_str(",");
}
result.push_str(format!(" ...Dict<{}>", rest.pretty_print()).as_str());
}
if result.starts_with("{ ") {
result.push_str(" }");
} else {
result.push_str("}");
}
result
}
}
Self::One(shapes) => {
let mut result = "One<".to_string();
for (i, shape) in shapes.iter().enumerate() {
if i > 0 {
result.push_str(", ");
}
result.push_str(shape.pretty_print().as_str());
}
result.push_str(">");
result
}
Self::All(shapes) => {
let mut result = "All<".to_string();
for (i, shape) in shapes.iter().enumerate() {
if i > 0 {
result.push_str(", ");
}
result.push_str(shape.pretty_print().as_str());
}
result.push_str(">");
result
}
Self::Name(name, path) => {
let mut dotted_path = name.clone();
dotted_path.push_str(NamedShapePathKey::path_to_string(&path).as_str());
dotted_path
}
Self::Error {
message, partial, ..
} => {
let mut result = format!("Error<{}", quote_string(message.as_str()));
if let Some(partial) = partial {
result.push_str(&format!(", {}", partial.pretty_print()));
}
result.push_str(">");
result
}
}
}
}
impl Display for Shape {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.pretty_print())
}
}
impl Debug for Shape {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.pretty_print())
}
}
impl Display for ShapeCase {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.pretty_print())
}
}
impl Debug for ShapeCase {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.pretty_print())
}
}