From bbcdeb128cce04cd95714b7bc7af5a23a7e38bd2 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Wed, 29 Jul 2020 18:09:51 +0200 Subject: =?UTF-8?q?Move,=20rename=20and=20switch=20some=20things=20(boring?= =?UTF-8?q?)=20=F0=9F=9A=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Problems -> Diagnostics - Position -> Pos - offset_spans -> Offset trait - Size -> Length (and some more size types renamed) - Paper into its own module - scope::Parser -> parsing::CallParser - Create `Decorations` alias - Remove lots of double newlines - Switch from f32 to f64 --- src/syntax/func/values.rs | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) (limited to 'src/syntax/func/values.rs') diff --git a/src/syntax/func/values.rs b/src/syntax/func/values.rs index 7a1aa912..3269f8e9 100644 --- a/src/syntax/func/values.rs +++ b/src/syntax/func/values.rs @@ -4,13 +4,12 @@ use std::fmt::{self, Display, Formatter}; use toddle::query::{FontStyle, FontWeight}; use crate::layout::prelude::*; -use crate::size::{Size, ScaleSize}; -use crate::style::Paper; +use crate::length::{Length, ScaleLength}; +use crate::paper::Paper; use super::*; use self::AlignmentValue::*; - /// Value types are used to extract the values of positional and keyword /// arguments from [`Tuples`](crate::syntax::expr::Tuple) and /// [`Objects`](crate::syntax::expr::Object). They represent the value part of @@ -24,14 +23,14 @@ use self::AlignmentValue::*; /// An implementation for `bool` might look as follows: /// ``` /// # use typstc::error; -/// # use typstc::problem::Problem; +/// # use typstc::diagnostic::Diagnostic; /// # use typstc::syntax::expr::Expr; /// # use typstc::syntax::func::Value; /// # use typstc::syntax::span::Spanned; /// # struct Bool; /* /// impl Value for bool { /// # */ impl Value for Bool { -/// fn parse(expr: Spanned) -> Result { +/// fn parse(expr: Spanned) -> Result { /// match expr.v { /// # /* /// Expr::Bool(b) => Ok(b), @@ -44,11 +43,11 @@ use self::AlignmentValue::*; pub trait Value: Sized { /// Parse an expression into this value or return an error if the expression /// is valid for this value type. - fn parse(expr: Spanned) -> Result; + fn parse(expr: Spanned) -> Result; } impl Value for Spanned { - fn parse(expr: Spanned) -> Result { + fn parse(expr: Spanned) -> Result { let span = expr.span; V::parse(expr).map(|v| Spanned { v, span }) } @@ -58,7 +57,7 @@ impl Value for Spanned { macro_rules! value { ($type:ty, $name:expr, $($p:pat => $r:expr),* $(,)?) => { impl Value for $type { - fn parse(expr: Spanned) -> Result { + fn parse(expr: Spanned) -> Result { #[allow(unreachable_patterns)] match expr.v { $($p => Ok($r)),*, @@ -77,13 +76,13 @@ value!(Ident, "identifier", Expr::Ident(i) => i); value!(String, "string", Expr::Str(s) => s); value!(f64, "number", Expr::Number(n) => n); value!(bool, "bool", Expr::Bool(b) => b); -value!(Size, "size", Expr::Size(s) => s); +value!(Length, "length", Expr::Length(s) => s); value!(Tuple, "tuple", Expr::Tuple(t) => t); value!(Object, "object", Expr::Object(o) => o); -value!(ScaleSize, "number or size", - Expr::Size(size) => ScaleSize::Absolute(size), - Expr::Number(scale) => ScaleSize::Scaled(scale as f32), +value!(ScaleLength, "number or length", + Expr::Length(length) => ScaleLength::Absolute(length), + Expr::Number(scale) => ScaleLength::Scaled(scale as f64), ); /// A value type that matches [`Expr::Ident`] and [`Expr::Str`] and implements @@ -108,20 +107,20 @@ impl From for String { /// # Example /// ``` /// # use typstc::syntax::func::{FuncArgs, Defaultable}; -/// # use typstc::size::Size; +/// # use typstc::length::Length; /// # let mut args = FuncArgs::new(); /// # let mut errors = vec![]; -/// args.key.get::>(&mut errors, "size"); +/// args.key.get::>(&mut errors, "length"); /// ``` /// This will yield. /// ```typst -/// [func: size=default] => None -/// [func: size=2cm] => Some(Size::cm(2.0)) +/// [func: length=default] => None +/// [func: length=2cm] => Some(Length::cm(2.0)) /// ``` pub struct Defaultable(pub Option); impl Value for Defaultable { - fn parse(expr: Spanned) -> Result { + fn parse(expr: Spanned) -> Result { Ok(Defaultable(match expr.v { Expr::Ident(ident) if ident.as_str() == "default" => None, _ => Some(V::parse(expr)?) @@ -136,7 +135,7 @@ impl From> for Option { } impl Value for FontStyle { - fn parse(expr: Spanned) -> Result { + fn parse(expr: Spanned) -> Result { FontStyle::from_name(Ident::parse(expr)?.as_str()) .ok_or_else(|| error!("invalid font style")) } @@ -145,7 +144,7 @@ impl Value for FontStyle { /// The additional boolean specifies whether a number was clamped into the range /// 100 - 900 to make it a valid font weight. impl Value for (FontWeight, bool) { - fn parse(expr: Spanned) -> Result { + fn parse(expr: Spanned) -> Result { match expr.v { Expr::Number(weight) => { let weight = weight.round(); @@ -170,14 +169,14 @@ impl Value for (FontWeight, bool) { } impl Value for Paper { - fn parse(expr: Spanned) -> Result { + fn parse(expr: Spanned) -> Result { Paper::from_name(Ident::parse(expr)?.as_str()) .ok_or_else(|| error!("invalid paper type")) } } impl Value for Direction { - fn parse(expr: Spanned) -> Result { + fn parse(expr: Spanned) -> Result { Ok(match Ident::parse(expr)?.as_str() { "left-to-right" | "ltr" | "LTR" => LeftToRight, "right-to-left" | "rtl" | "RTL" => RightToLeft, @@ -250,7 +249,7 @@ impl AlignmentValue { } impl Value for AlignmentValue { - fn parse(expr: Spanned) -> Result { + fn parse(expr: Spanned) -> Result { Ok(match Ident::parse(expr)?.as_str() { "origin" => Align(Origin), "center" => Align(Center), -- cgit v1.2.3