use std::any::Any; use std::cmp::Ordering; use std::fmt::{self, Debug, Formatter}; use std::hash::Hash; use std::rc::Rc; use super::{ops, Array, Class, Dict, Function, Node}; use crate::diag::StrResult; use crate::geom::{Angle, Color, Fractional, Length, Linear, Relative, RgbaColor}; use crate::layout::Layout; use crate::syntax::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 relative value: `50%`. Relative(Relative), /// A combination of an absolute length and a relative value: `20% + 5cm`. Linear(Linear), /// A fractional value: `1fr`. Fractional(Fractional), /// A color value: `#f79143ff`. Color(Color), /// A string: `"string"`. Str(EcoString), /// An array of values: `(1, "hi", 12cm)`. Array(Array), /// A dictionary value: `(color: #f79143, pattern: dashed)`. Dict(Dict), /// A node value: `[*Hi* there]`. Node(Node), /// An executable function. Func(Function), /// A class of nodes. Class(Class), /// A dynamic value. Dyn(Dynamic), } impl Value { /// Create an inline-level node value. pub fn inline(node: T) -> Self where T: Layout + Debug + Hash + 'static, { Self::Node(Node::inline(node)) } /// Create a block-level node value. pub fn block(node: T) -> Self where T: Layout + Debug + Hash + 'static, { Self::Node(Node::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::Relative(_) => Relative::TYPE_NAME, Self::Linear(_) => Linear::TYPE_NAME, Self::Fractional(_) => Fractional::TYPE_NAME, Self::Color(_) => Color::TYPE_NAME, Self::Str(_) => EcoString::TYPE_NAME, Self::Array(_) => Array::TYPE_NAME, Self::Dict(_) => Dict::TYPE_NAME, Self::Node(_) => Node::TYPE_NAME, Self::Func(_) => Function::TYPE_NAME, Self::Class(_) => Class::TYPE_NAME, Self::Dyn(v) => v.type_name(), } } /// Try to cast the value into a specific type. pub fn cast(self) -> StrResult where T: Cast, { 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 show(self) -> Node { match self { Value::None => Node::new(), Value::Int(v) => Node::Text(format_eco!("{}", v)), Value::Float(v) => Node::Text(format_eco!("{}", v)), Value::Str(v) => Node::Text(v), Value::Node(v) => v, // For values which can't be shown "naturally", we print the // representation in monospace. v => Node::Text(v.repr()).monospaced(), } } } 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::Relative(v) => Debug::fmt(v, f), Self::Linear(v) => Debug::fmt(v, f), Self::Fractional(v) => Debug::fmt(v, f), Self::Color(v) => Debug::fmt(v, f), Self::Str(v) => Debug::fmt(v, f), Self::Array(v) => Debug::fmt(v, f), Self::Dict(v) => Debug::fmt(v, f), Self::Node(_) => f.pad("