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}1#![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}