use std::any::Any; use std::cmp::Ordering; use std::fmt::{self, Debug, Formatter}; use std::hash::{Hash, Hasher}; use std::sync::Arc; use super::{ops, Args, Array, Content, Context, Dict, Func, Layout, StrExt}; use crate::diag::{with_alternative, At, StrResult, TypResult}; use crate::geom::{Angle, Color, Fraction, Length, Ratio, Relative, RgbaColor}; use crate::library::text::RawNode; use crate::syntax::{Span, Spanned}; use crate::util::EcoString; /// A computational value. #[derive(Clone)] pub enum Value { /// The value that indicates the absence of a meaningful value. None, /// A value that indicates some smart default behaviour. Auto, /// A boolean: `true, false`. Bool(bool), /// An integer: `120`. Int(i64), /// A floating-point number: `1.2`, `10e-4`. Float(f64), /// A length: `12pt`, `3cm`. Length(Length), /// An angle: `1.5rad`, `90deg`. Angle(Angle), /// A ratio: `50%`. Ratio(Ratio), /// A relative length, combination of a ratio and a length: `20% + 5cm`. Relative(Relative), /// A fraction: `1fr`. Fraction(Fraction), /// A color value: `#f79143ff`. Color(Color), /// A string: `"string"`. Str(EcoString), /// A content value: `[*Hi* there]`. Content(Content), /// An array of values: `(1, "hi", 12cm)`. Array(Array), /// A dictionary value: `(color: #f79143, pattern: dashed)`. Dict(Dict), /// An executable function. Func(Func), /// Captured arguments to a function. Args(Args), /// A dynamic value. Dyn(Dynamic), } impl Value { /// Create a content value from an inline-level node. pub fn inline(node: T) -> Self where T: Layout + Debug + Hash + Sync + Send + 'static, { Self::Content(Content::inline(node)) } /// Create a content value from a block-level node. pub fn block(node: T) -> Self where T: Layout + Debug + Hash + Sync + Send + 'static, { Self::Content(Content::block(node)) } /// The name of the stored value's type. pub fn type_name(&self) -> &'static str { match self { Self::None => "none", Self::Auto => "auto", Self::Bool(_) => bool::TYPE_NAME, Self::Int(_) => i64::TYPE_NAME, Self::Float(_) => f64::TYPE_NAME, Self::Length(_) => Length::TYPE_NAME, Self::Angle(_) => Angle::TYPE_NAME, Self::Ratio(_) => Ratio::TYPE_NAME, Self::Relative(_) => Relative::TYPE_NAME, Self::Fraction(_) => Fraction::TYPE_NAME, Self::Color(_) => Color::TYPE_NAME, Self::Str(_) => EcoString::TYPE_NAME, Self::Content(_) => Content::TYPE_NAME, Self::Array(_) => Array::TYPE_NAME, Self::Dict(_) => Dict::TYPE_NAME, Self::Func(_) => Func::TYPE_NAME, Self::Args(_) => Args::TYPE_NAME, Self::Dyn(v) => v.type_name(), } } /// Try to cast the value into a specific type. pub fn cast(self) -> StrResult { T::cast(self) } /// Join the value with another value. pub fn join(self, rhs: Self) -> StrResult { ops::join(self, rhs) } /// Return the debug representation of the value. pub fn repr(&self) -> EcoString { format_eco!("{:?}", self) } /// Return the display representation of the value. pub fn display(self) -> Content { match self { Value::None => Content::new(), Value::Int(v) => Content::Text(format_eco!("{}", v)), Value::Float(v) => Content::Text(format_eco!("{}", v)), Value::Str(v) => Content::Text(v), Value::Content(v) => v, // For values which can't be shown "naturally", we return the raw // representation. v => Content::show(RawNode { text: v.repr(), block: false }), } } /// Call a method on the value. pub fn call( &self, ctx: &mut Context, method: &str, span: Span, mut args: Args, ) -> TypResult { let name = self.type_name(); let missing = || Err(missing_method(name, method)).at(span); let output = match self { Value::Str(string) => match method { "len" => Value::Int(string.len() as i64), "trim" => Value::Str(string.trim().into()), "split" => Value::Array(string.split(args.eat()?)), _ => missing()?, }, Value::Array(array) => match method { "len" => Value::Int(array.len()), "slice" => { let start = args.expect("start")?; let mut end = args.eat()?; if end.is_none() { end = args.named("count")?.map(|c: i64| start + c); } Value::Array(array.slice(start, end).at(span)?) } "map" => Value::Array(array.map(ctx, args.expect("function")?)?), "filter" => Value::Array(array.filter(ctx, args.expect("function")?)?), "flatten" => Value::Array(array.flatten()), "find" => { array.find(args.expect("value")?).map_or(Value::None, Value::Int) } "join" => { let sep = args.eat()?; let last = args.named("last")?; array.join(sep, last).at(span)? } "sorted" => Value::Array(array.sorted().at(span)?), _ => missing()?, }, Value::Dict(dict) => match method { "len" => Value::Int(dict.len()), "keys" => Value::Array(dict.keys()), "values" => Value::Array(dict.values()), "pairs" => Value::Array(dict.map(ctx, args.expect("function")?)?), _ => missing()?, }, Value::Func(func) => match method { "with" => Value::Func(func.clone().with(args.take())), _ => missing()?, }, Value::Args(args) => match method { "positional" => Value::Array(args.to_positional()), "named" => Value::Dict(args.to_named()), _ => missing()?, }, _ => missing()?, }; args.finish()?; Ok(output) } /// Call a mutating method on the value. pub fn call_mut( &mut self, _: &mut Context, method: &str, span: Span, mut args: Args, ) -> TypResult<()> { let name = self.type_name(); let missing = || Err(missing_method(name, method)).at(span); match self { Value::Array(array) => match method { "push" => array.push(args.expect("value")?), "pop" => array.pop().at(span)?, "insert" => { array.insert(args.expect("index")?, args.expect("value")?).at(span)? } "remove" => array.remove(args.expect("index")?).at(span)?, _ => missing()?, }, Value::Dict(dict) => match method { "remove" => dict.remove(args.expect("key")?).at(span)?, _ => missing()?, }, _ => missing()?, } args.finish()?; Ok(()) } /// Whether a specific method is mutable. pub fn is_mutable_method(method: &str) -> bool { matches!(method, "push" | "pop" | "insert" | "remove") } } /// The missing method error message. #[cold] fn missing_method(type_name: &str, method: &str) -> String { format!("type {type_name} has no method `{method}`") } impl Default for Value { fn default() -> Self { Value::None } } impl Debug for Value { fn fmt(&self, f: &mut Formatter) -> fmt::Result { match self { Self::None => f.pad("none"), Self::Auto => f.pad("auto"), Self::Bool(v) => Debug::fmt(v, f), Self::Int(v) => Debug::fmt(v, f), Self::Float(v) => Debug::fmt(v, f), Self::Length(v) => Debug::fmt(v, f), Self::Angle(v) => Debug::fmt(v, f), Self::Ratio(v) => Debug::fmt(v, f), Self::Relative(v) => Debug::fmt(v, f), Self::Fraction(v) => Debug::fmt(v, f), Self::Color(v) => Debug::fmt(v, f), Self::Str(v) => Debug::fmt(v, f), Self::Content(_) => f.pad(""), Self::Array(v) => Debug::fmt(v, f), Self::Dict(v) => Debug::fmt(v, f), Self::Func(v) => Debug::fmt(v, f), Self::Args(v) => Debug::fmt(v, f), Self::Dyn(v) => Debug::fmt(v, f), } } } impl PartialEq for Value { fn eq(&self, other: &Self) -> bool { ops::equal(self, other) } } impl PartialOrd for Value { fn partial_cmp(&self, other: &Self) -> Option { ops::compare(self, other) } } impl Hash for Value { fn hash(&self, state: &mut H) { std::mem::discriminant(self).hash(state); match self { Self::None => {} Self::Auto => {} Self::Bool(v) => v.hash(state), Self::Int(v) => v.hash(state), Self::Float(v) => v.to_bits().hash(state), Self::Length(v) => v.hash(state), Self::Angle(v) => v.hash(state), Self::Ratio(v) => v.hash(state), Self::Relative(v) => v.hash(state), Self::Fraction(v) => v.hash(state), Self::Color(v) => v.hash(state), Self::Str(v) => v.hash(state), Self::Content(v) => v.hash(state), Self::Array(v) => v.hash(state), Self::Dict(v) => v.hash(state), Self::Func(v) => v.hash(state), Self::Args(v) => v.hash(state), Self::Dyn(v) => v.hash(state), } } } impl From for Value { fn from(v: i32) -> Self { Self::Int(v as i64) } } impl From for Value { fn from(v: usize) -> Self { Self::Int(v as i64) } } impl From for Value { fn from(v: RgbaColor) -> Self { Self::Color(v.into()) } } impl From<&str> for Value { fn from(v: &str) -> Self { Self::Str(v.into()) } } impl From for Value { fn from(v: String) -> Self { Self::Str(v.into()) } } impl From for Value { fn from(v: Dynamic) -> Self { Self::Dyn(v) } } /// A dynamic value. #[derive(Clone, Hash)] pub struct Dynamic(Arc); impl Dynamic { /// Create a new instance from any value that satisifies the required bounds. pub fn new(any: T) -> Self where T: Type + Debug + PartialEq + Hash + Sync + Send + 'static, { Self(Arc::new(any)) } /// Whether the wrapped type is `T`. pub fn is(&self) -> bool { (*self.0).as_any().is::() } /// Try to downcast to a reference to a specific type. pub fn downcast(&self) -> Option<&T> { (*self.0).as_any().downcast_ref() } /// The name of the stored value's type. pub fn type_name(&self) -> &'static str { self.0.dyn_type_name() } } impl Debug for Dynamic { fn fmt(&self, f: &mut Formatter) -> fmt::Result { Debug::fmt(&self.0, f) } } impl PartialEq for Dynamic { fn eq(&self, other: &Self) -> bool { self.0.dyn_eq(other) } } trait Bounds: Debug + Sync + Send + 'static { fn as_any(&self) -> &dyn Any; fn dyn_eq(&self, other: &Dynamic) -> bool; fn dyn_type_name(&self) -> &'static str; fn hash64(&self) -> u64; } impl Bounds for T where T: Type + Debug + PartialEq + Hash + Sync + Send + 'static, { fn as_any(&self) -> &dyn Any { self } fn dyn_eq(&self, other: &Dynamic) -> bool { if let Some(other) = other.downcast::() { self == other } else { false } } fn dyn_type_name(&self) -> &'static str { T::TYPE_NAME } fn hash64(&self) -> u64 { // Also hash the TypeId since nodes with different types but // equal data should be different. let mut state = fxhash::FxHasher64::default(); self.type_id().hash(&mut state); self.hash(&mut state); state.finish() } } impl Hash for dyn Bounds { fn hash(&self, state: &mut H) { state.write_u64(self.hash64()); } } /// The type of a value. pub trait Type { /// The name of the type. const TYPE_NAME: &'static str; } /// Cast from a value to a specific type. pub trait Cast: Sized { /// Check whether the value is castable to `Self`. fn is(value: &V) -> bool; /// Try to cast the value into an instance of `Self`. fn cast(value: V) -> StrResult; } /// Implement traits for primitives. macro_rules! primitive { ( $type:ty: $name:literal, $variant:ident $(, $other:ident$(($binding:ident))? => $out:expr)* ) => { impl Type for $type { const TYPE_NAME: &'static str = $name; } impl Cast for $type { fn is(value: &Value) -> bool { matches!(value, Value::$variant(_) $(| primitive!(@$other $(($binding))?))*) } fn cast(value: Value) -> StrResult { match value { Value::$variant(v) => Ok(v), $(Value::$other$(($binding))? => Ok($out),)* v => Err(format!( "expected {}, found {}", Self::TYPE_NAME, v.type_name(), )), } } } impl From<$type> for Value { fn from(v: $type) -> Self { Value::$variant(v) } } }; (@$other:ident($binding:ident)) => { Value::$other(_) }; (@$other:ident) => { Value::$other }; } /// Implement traits for dynamic types. macro_rules! dynamic { ($type:ty: $name:literal, $($tts:tt)*) => { impl $crate::eval::Type for $type { const TYPE_NAME: &'static str = $name; } castable! { $type, Expected: ::TYPE_NAME, $($tts)* @this: Self => this.clone(), } impl From<$type> for $crate::eval::Value { fn from(v: $type) -> Self { $crate::eval::Value::Dyn($crate::eval::Dynamic::new(v)) } } }; } /// Make a type castable from a value. macro_rules! castable { ( $type:ty, Expected: $expected:expr, $($pattern:pat => $out:expr,)* $(@$dyn_in:ident: $dyn_type:ty => $dyn_out:expr,)* ) => { impl $crate::eval::Cast<$crate::eval::Value> for $type { fn is(value: &Value) -> bool { #[allow(unused_variables)] match value { $($pattern => true,)* $crate::eval::Value::Dyn(dynamic) => { false $(|| dynamic.is::<$dyn_type>())* } _ => false, } } fn cast(value: $crate::eval::Value) -> $crate::diag::StrResult { let found = match value { $($pattern => return Ok($out),)* $crate::eval::Value::Dyn(dynamic) => { $(if let Some($dyn_in) = dynamic.downcast::<$dyn_type>() { return Ok($dyn_out); })* dynamic.type_name() } v => v.type_name(), }; Err(format!("expected {}, found {}", $expected, found)) } } }; } primitive! { bool: "boolean", Bool } primitive! { i64: "integer", Int } primitive! { f64: "float", Float, Int(v) => v as f64 } primitive! { Length: "length", Length } primitive! { Angle: "angle", Angle } primitive! { Ratio: "ratio", Ratio } primitive! { Relative: "relative length", Relative, Length(v) => v.into(), Ratio(v) => v.into() } primitive! { Fraction: "fraction", Fraction } primitive! { Color: "color", Color } primitive! { EcoString: "string", Str } primitive! { Content: "content", Content, None => Content::new() } primitive! { Array: "array", Array } primitive! { Dict: "dictionary", Dict } primitive! { Func: "function", Func } primitive! { Args: "arguments", Args } impl Cast for Value { fn is(_: &Value) -> bool { true } fn cast(value: Value) -> StrResult { Ok(value) } } impl Cast> for T { fn is(value: &Spanned) -> bool { T::is(&value.v) } fn cast(value: Spanned) -> StrResult { T::cast(value.v) } } impl Cast> for Spanned { fn is(value: &Spanned) -> bool { T::is(&value.v) } fn cast(value: Spanned) -> StrResult { let span = value.span; T::cast(value.v).map(|t| Spanned::new(t, span)) } } /// A value that can be automatically determined. #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] pub enum Smart { /// The value should be determined smartly based on the /// circumstances. Auto, /// A forced, specific value. Custom(T), } impl Smart { /// Returns the contained custom value or a provided default value. pub fn unwrap_or(self, default: T) -> T { match self { Self::Auto => default, Self::Custom(x) => x, } } /// Returns the contained custom value or computes a default value. pub fn unwrap_or_else(self, f: F) -> T where F: FnOnce() -> T, { match self { Self::Auto => f(), Self::Custom(x) => x, } } } impl Default for Smart { fn default() -> Self { Self::Auto } } impl Cast for Option { fn is(value: &Value) -> bool { matches!(value, Value::None) || T::is(value) } fn cast(value: Value) -> StrResult { match value { Value::None => Ok(None), v => T::cast(v).map(Some).map_err(|msg| with_alternative(msg, "none")), } } } impl Cast for Smart { fn is(value: &Value) -> bool { matches!(value, Value::Auto) || T::is(value) } fn cast(value: Value) -> StrResult { match value { Value::Auto => Ok(Self::Auto), v => T::cast(v) .map(Self::Custom) .map_err(|msg| with_alternative(msg, "auto")), } } } #[cfg(test)] mod tests { use super::*; #[track_caller] fn test(value: impl Into, exp: &str) { assert_eq!(format!("{:?}", value.into()), exp); } #[test] fn test_value_debug() { // Primitives. test(Value::None, "none"); test(false, "false"); test(12i64, "12"); test(3.14, "3.14"); test(Length::pt(5.5), "5.5pt"); test(Angle::deg(90.0), "90deg"); test(Ratio::one() / 2.0, "50%"); test(Ratio::new(0.3) + Length::cm(2.0), "30% + 56.69pt"); test(Fraction::one() * 7.55, "7.55fr"); test(Color::Rgba(RgbaColor::new(1, 1, 1, 0xff)), "#010101"); // Collections. test("hello", r#""hello""#); test("\n", r#""\n""#); test("\\", r#""\\""#); test("\"", r#""\"""#); test(array![], "()"); test(array![Value::None], "(none,)"); test(array![1, 2], "(1, 2)"); test(dict![], "(:)"); test(dict!["one" => 1], "(one: 1)"); test(dict!["two" => false, "one" => 1], "(one: 1, two: false)"); // Functions. test( Func::from_fn("nil", |_, _| Ok(Value::None)), "", ); // Dynamics. test(Dynamic::new(1), "1"); } }