difftreelog
refactor types are acyclic
in: master
1 file changed
crates/jrsonnet-types/src/lib.rsdiffbeforeafterboth1#![allow(clippy::redundant_closure_call)]23use std::fmt::Display;45use jrsonnet_gcmodule::Acyclic;67#[derive(Debug, Clone, Copy, PartialEq, Eq, Acyclic)]8pub enum ValType {9 Bool,10 Null,11 Str,12 Num,13 #[cfg(feature = "exp-bigint")]14 BigInt,15 Arr,16 Obj,17 Func,18}1920impl ValType {21 pub const fn name(&self) -> &'static str {22 use ValType::*;23 match self {24 Bool => "boolean",25 Null => "null",26 Str => "string",27 Num => "number",28 #[cfg(feature = "exp-bigint")]29 BigInt => "bigint",30 Arr => "array",31 Obj => "object",32 Func => "function",33 }34 }35}3637impl Display for ValType {38 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {39 write!(f, "{}", self.name())40 }41}4243#[derive(Debug, Clone, PartialEq, Acyclic)]44pub enum ComplexValType {45 Any,46 Char,47 Simple(ValType),48 BoundedNumber(Option<f64>, Option<f64>),49 Array(Box<ComplexValType>),50 ArrayRef(&'static ComplexValType),51 ObjectRef(&'static [(&'static str, &'static ComplexValType)]),52 AttrsOf(&'static ComplexValType),53 Union(Vec<ComplexValType>),54 UnionRef(&'static [&'static ComplexValType]),55 Sum(Vec<ComplexValType>),56 SumRef(&'static [&'static ComplexValType]),57 Lazy(&'static ComplexValType),58}5960impl From<ValType> for ComplexValType {61 fn from(s: ValType) -> Self {62 Self::Simple(s)63 }64}6566fn write_union<'i>(67 f: &mut std::fmt::Formatter<'_>,68 is_union: bool,69 union: impl Iterator<Item = &'i ComplexValType>,70) -> std::fmt::Result {71 for (i, v) in union.enumerate() {72 let should_add_braces =73 matches!(v, ComplexValType::UnionRef(_) | ComplexValType::Union(_) if !is_union);74 if i != 0 {75 write!(f, " {} ", if is_union { '|' } else { '&' })?;76 }77 if should_add_braces {78 write!(f, "(")?;79 }80 write!(f, "{v}")?;81 if should_add_braces {82 write!(f, ")")?;83 }84 }85 Ok(())86}8788fn print_array(a: &ComplexValType, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {89 if *a == ComplexValType::Any {90 write!(f, "array")?;91 } else {92 write!(f, "Array<{a}>")?;93 }94 Ok(())95}9697impl Display for ComplexValType {98 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {99 match self {100 Self::Any => write!(f, "any")?,101 Self::Simple(s) => write!(f, "{s}")?,102 Self::Char => write!(f, "char")?,103 Self::BoundedNumber(a, b) => write!(104 f,105 "BoundedNumber<{}, {}>",106 a.map(|e| e.to_string())107 .unwrap_or_else(|| "open".to_owned()),108 b.map(|e| e.to_string())109 .unwrap_or_else(|| "open".to_owned())110 )?,111 Self::ArrayRef(a) => print_array(a, f)?,112 Self::Array(a) => print_array(a, f)?,113 Self::ObjectRef(fields) => {114 write!(f, "{{")?;115 for (i, (k, v)) in fields.iter().enumerate() {116 if i != 0 {117 write!(f, ", ")?;118 }119 write!(f, "{k}: {v}")?;120 }121 write!(f, "}}")?;122 }123 Self::AttrsOf(a) => {124 if matches!(a, Self::Any) {125 write!(f, "object")?;126 } else {127 write!(f, "AttrsOf<{a}>")?;128 }129 }130 Self::Union(v) => write_union(f, true, v.iter())?,131 Self::UnionRef(v) => write_union(f, true, v.iter().copied())?,132 Self::Sum(v) => write_union(f, false, v.iter())?,133 Self::SumRef(v) => write_union(f, false, v.iter().copied())?,134 Self::Lazy(lazy) => write!(f, "Lazy<{lazy}>")?,135 }136 Ok(())137 }138}