use std::any::{Any, TypeId}; use std::cmp::Ordering; use std::fmt::{self, Debug, Formatter}; use std::hash::{Hash, Hasher}; use std::sync::Arc; use ecow::{eco_format, EcoString}; use serde::de::value::{MapAccessDeserializer, SeqAccessDeserializer}; use serde::de::{Error, MapAccess, SeqAccess, Visitor}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use typst_syntax::{ast, Span}; use typst_utils::ArcExt; use crate::diag::{HintedStrResult, HintedString, StrResult}; use crate::foundations::{ fields, ops, repr, Args, Array, AutoValue, Bytes, CastInfo, Content, Datetime, Decimal, Dict, Duration, Fold, FromValue, Func, IntoValue, Label, Module, NativeElement, NativeType, NoneValue, Plugin, Reflect, Repr, Resolve, Scope, Str, Styles, Symbol, Type, Version, }; use crate::layout::{Abs, Angle, Em, Fr, Length, Ratio, Rel}; use crate::text::{RawContent, RawElem, TextElem}; use crate::visualize::{Color, Gradient, Tiling}; /// A computational value. #[derive(Default, Clone)] pub enum Value { /// The value that indicates the absence of a meaningful value. #[default] 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`, `1.5em`, `1em - 2pt`. 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(Rel), /// A fraction: `1fr`. Fraction(Fr), /// A color value: `#f79143ff`. Color(Color), /// A gradient value: `gradient.linear(...)`. Gradient(Gradient), /// A tiling fill: `tiling(...)`. Tiling(Tiling), /// A symbol: `arrow.l`. Symbol(Symbol), /// A version. Version(Version), /// A string: `"string"`. Str(Str), /// Raw bytes. Bytes(Bytes), /// A label: ``. Label(Label), /// A datetime Datetime(Datetime), /// A decimal value: `decimal("123.4500")` Decimal(Decimal), /// A duration Duration(Duration), /// A content value: `[*Hi* there]`. Content(Content), // Content styles. Styles(Styles), /// An array of values: `(1, "hi", 12cm)`. Array(Array), /// A dictionary value: `(a: 1, b: "hi")`. Dict(Dict), /// An executable function. Func(Func), /// Captured arguments to a function. Args(Args), /// A type. Type(Type), /// A module. Module(Module), /// A WebAssembly plugin. Plugin(Plugin), /// A dynamic value. Dyn(Dynamic), } impl Value { /// Create a new dynamic value. pub fn dynamic(any: T) -> Self where T: Debug + Repr + NativeType + PartialEq + Hash + Sync + Send + 'static, { Self::Dyn(Dynamic::new(any)) } /// Create a numeric value from a number with a unit. pub fn numeric(pair: (f64, ast::Unit)) -> Self { let (v, unit) = pair; match unit { ast::Unit::Pt => Abs::pt(v).into_value(), ast::Unit::Mm => Abs::mm(v).into_value(), ast::Unit::Cm => Abs::cm(v).into_value(), ast::Unit::In => Abs::inches(v).into_value(), ast::Unit::Rad => Angle::rad(v).into_value(), ast::Unit::Deg => Angle::deg(v).into_value(), ast::Unit::Em => Em::new(v).into_value(), ast::Unit::Fr => Fr::new(v).into_value(), ast::Unit::Percent => Ratio::new(v / 100.0).into_value(), } } /// The type of this value. pub fn ty(&self) -> Type { match self { Self::None => Type::of::(), Self::Auto => Type::of::(), Self::Bool(_) => Type::of::(), Self::Int(_) => Type::of::(), Self::Float(_) => Type::of::(), Self::Length(_) => Type::of::(), Self::Angle(_) => Type::of::(), Self::Ratio(_) => Type::of::(), Self::Relative(_) => Type::of::>(), Self::Fraction(_) => Type::of::(), Self::Color(_) => Type::of::(), Self::Gradient(_) => Type::of::(), Self::Tiling(_) => Type::of::(), Self::Symbol(_) => Type::of::(), Self::Version(_) => Type::of::(), Self::Str(_) => Type::of::(), Self::Bytes(_) => Type::of::(), Self::Label(_) => Type::of::