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::Trace;67#[derive(Debug, Clone, Copy, PartialEq, Eq, Trace)]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, Trace)]44#[trace(skip)]45pub enum ComplexValType {46 Any,47 Char,48 Simple(ValType),49 BoundedNumber(Option<f64>, Option<f64>),50 Array(Box<ComplexValType>),51 ArrayRef(&'static ComplexValType),52 ObjectRef(&'static [(&'static str, &'static ComplexValType)]),53 AttrsOf(&'static ComplexValType),54 Union(Vec<ComplexValType>),55 UnionRef(&'static [&'static ComplexValType]),56 Sum(Vec<ComplexValType>),57 SumRef(&'static [&'static ComplexValType]),58 Lazy(&'static ComplexValType),59}6061impl From<ValType> for ComplexValType {62 fn from(s: ValType) -> Self {63 Self::Simple(s)64 }65}6667fn write_union<'i>(68 f: &mut std::fmt::Formatter<'_>,69 is_union: bool,70 union: impl Iterator<Item = &'i ComplexValType>,71) -> std::fmt::Result {72 for (i, v) in union.enumerate() {73 let should_add_braces =74 matches!(v, ComplexValType::UnionRef(_) | ComplexValType::Union(_) if !is_union);75 if i != 0 {76 write!(f, " {} ", if is_union { '|' } else { '&' })?;77 }78 if should_add_braces {79 write!(f, "(")?;80 }81 write!(f, "{v}")?;82 if should_add_braces {83 write!(f, ")")?;84 }85 }86 Ok(())87}8889fn print_array(a: &ComplexValType, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {90 if *a == ComplexValType::Any {91 write!(f, "array")?;92 } else {93 write!(f, "Array<{a}>")?;94 }95 Ok(())96}9798impl Display for ComplexValType {99 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {100 match self {101 Self::Any => write!(f, "any")?,102 Self::Simple(s) => write!(f, "{s}")?,103 Self::Char => write!(f, "char")?,104 Self::BoundedNumber(a, b) => write!(105 f,106 "BoundedNumber<{}, {}>",107 a.map(|e| e.to_string())108 .unwrap_or_else(|| "open".to_owned()),109 b.map(|e| e.to_string())110 .unwrap_or_else(|| "open".to_owned())111 )?,112 Self::ArrayRef(a) => print_array(a, f)?,113 Self::Array(a) => print_array(a, f)?,114 Self::ObjectRef(fields) => {115 write!(f, "{{")?;116 for (i, (k, v)) in fields.iter().enumerate() {117 if i != 0 {118 write!(f, ", ")?;119 }120 write!(f, "{k}: {v}")?;121 }122 write!(f, "}}")?;123 }124 Self::AttrsOf(a) => {125 if matches!(a, Self::Any) {126 write!(f, "object")?;127 } else {128 write!(f, "AttrsOf<{a}>")?;129 }130 }131 Self::Union(v) => write_union(f, true, v.iter())?,132 Self::UnionRef(v) => write_union(f, true, v.iter().copied())?,133 Self::Sum(v) => write_union(f, false, v.iter())?,134 Self::SumRef(v) => write_union(f, false, v.iter().copied())?,135 Self::Lazy(lazy) => write!(f, "Lazy<{lazy}>")?,136 }137 Ok(())138 }139}