git.delta.rocks / jrsonnet / refs/commits / 814b019b0248

difftreelog

feat prettier union type printing

Yaroslav Bolyukin2021-01-12parent: #72f4bf9.patch.diff
in: master

1 file changed

modifiedcrates/jrsonnet-types/src/lib.rsdiffbeforeafterboth
68 );68 );
69 assert_eq!(69 assert_eq!(
70 format!("{}", ty!(((str & num) | (obj & null)))),70 format!("{}", ty!(((str & num) | (obj & null)))),
71 "((str & num) | (obj & null))"71 "string & number | object & null"
72 );72 );
73 assert_eq!(format!("{}", ty!((str | [any]))), "(str | [any])");73 assert_eq!(format!("{}", ty!((str | [any]))), "string | array");
74 assert_eq!(format!("{}", ty!(((str & num) | [any]))), "string & number | array");
74}75}
7576
76#[derive(Debug, Clone, Copy, PartialEq, Eq)]77#[derive(Debug, Clone, Copy, PartialEq, Eq)]
122 }123 }
123}124}
124
125impl ComplexValType {
126 fn needs_brackets(&self) -> bool {
127 matches!(self, ComplexValType::UnionRef(_) | ComplexValType::SumRef(_))
128 }
129}
130125
131fn write_union(126fn write_union(
132 f: &mut std::fmt::Formatter<'_>,127 f: &mut std::fmt::Formatter<'_>,
133 ch: char,128 is_union: bool,
134 union: &[ComplexValType],129 union: &[ComplexValType],
135) -> std::fmt::Result {130) -> std::fmt::Result {
136 write!(f, "(")?;
137 for (i, v) in union.iter().enumerate() {131 for (i, v) in union.iter().enumerate() {
132 let should_add_braces = match v {
133 ComplexValType::UnionRef(_) if !is_union => true,
134 _ => false,
135 };
138 if i != 0 {136 if i != 0 {
139 write!(f, " {} ", ch)?;137 write!(f, " {} ", if is_union { '|' } else { '&' })?;
140 }138 }
139 if should_add_braces {
140 write!(f, "(")?;
141 }
141 write!(f, "{}", v)?;142 write!(f, "{}", v)?;
143 if should_add_braces {
144 write!(f, ")")?;
145 }
142 }146 }
143 write!(f, ")")?;
144 Ok(())147 Ok(())
145}148}
146149
152 ComplexValType::Char => write!(f, "char")?,155 ComplexValType::Char => write!(f, "char")?,
153 ComplexValType::BoundedNumber(a, b) => write!(156 ComplexValType::BoundedNumber(a, b) => write!(
154 f,157 f,
155 "number({}..{})",158 "BoundedNumber<{}, {}>",
156 a.map(|e| e.to_string()).unwrap_or_else(|| "".into()),159 a.map(|e| e.to_string()).unwrap_or_else(|| "".into()),
157 b.map(|e| e.to_string()).unwrap_or_else(|| "".into())160 b.map(|e| e.to_string()).unwrap_or_else(|| "".into())
158 )?,161 )?,
159 ComplexValType::ArrayRef(a) => {162 ComplexValType::ArrayRef(a) => {
160 if a.needs_brackets() {163 if **a == ComplexValType::Any {
161 write!(f, "(")?;
162 }
163 write!(f, "{}", a)?;164 write!(f, "array")?
164 if a.needs_brackets() {165 } else {
165 write!(f, ")")?;166 write!(f, "Array<{}>", a)?
166 }167 }
167 write!(f, "[]")?;
168 }168 }
169 ComplexValType::ObjectRef(fields) => {169 ComplexValType::ObjectRef(fields) => {
170 write!(f, "{{")?;170 write!(f, "{{")?;
176 }176 }
177 write!(f, "}}")?;177 write!(f, "}}")?;
178 }178 }
179 ComplexValType::UnionRef(v) => write_union(f, '|', v)?,179 ComplexValType::UnionRef(v) => write_union(f, true, v)?,
180 ComplexValType::SumRef(v) => write_union(f, '&', v)?,180 ComplexValType::SumRef(v) => write_union(f, false, v)?,
181 };181 };
182 Ok(())182 Ok(())
183 }183 }