diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-05-06 10:05:11 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-05-06 10:05:11 +0200 |
| commit | bfaf5447a789cd0dbbb1e418bea62fef9edc2b7d (patch) | |
| tree | 0cc1a47b37439fbeda06c57ebef0025becae0066 /src/eval | |
| parent | 49b8574b8d03e52a990f7d7b009c36fbdad0d55a (diff) | |
Cast content from string
Diffstat (limited to 'src/eval')
| -rw-r--r-- | src/eval/args.rs | 28 | ||||
| -rw-r--r-- | src/eval/func.rs | 9 | ||||
| -rw-r--r-- | src/eval/value.rs | 12 |
3 files changed, 30 insertions, 19 deletions
diff --git a/src/eval/args.rs b/src/eval/args.rs index f507e714..9b21cfa2 100644 --- a/src/eval/args.rs +++ b/src/eval/args.rs @@ -44,20 +44,6 @@ impl Args { Self { span, items } } - /// Consume and cast the first positional argument. - /// - /// Returns a `missing argument: {what}` error if no positional argument is - /// left. - pub fn expect<T>(&mut self, what: &str) -> TypResult<T> - where - T: Cast<Spanned<Value>>, - { - match self.eat()? { - Some(v) => Ok(v), - None => bail!(self.span, "missing argument: {}", what), - } - } - /// Consume and cast the first positional argument if there is one. pub fn eat<T>(&mut self) -> TypResult<Option<T>> where @@ -73,6 +59,20 @@ impl Args { Ok(None) } + /// Consume and cast the first positional argument. + /// + /// Returns a `missing argument: {what}` error if no positional argument is + /// left. + pub fn expect<T>(&mut self, what: &str) -> TypResult<T> + where + T: Cast<Spanned<Value>>, + { + match self.eat()? { + Some(v) => Ok(v), + None => bail!(self.span, "missing argument: {}", what), + } + } + /// Find and consume the first castable positional argument. pub fn find<T>(&mut self) -> TypResult<Option<T>> where diff --git a/src/eval/func.rs b/src/eval/func.rs index b72e9f18..73f2cac9 100644 --- a/src/eval/func.rs +++ b/src/eval/func.rs @@ -43,11 +43,11 @@ impl Func { Self(Arc::new(Repr::Native(Native { name, func: |ctx, args| { - let styles = T::set(args)?; + let styles = T::set(args, true)?; let content = T::construct(ctx, args)?; Ok(Value::Content(content.styled_with_map(styles.scoped()))) }, - set: Some(T::set), + set: Some(|args| T::set(args, false)), node: T::SHOWABLE.then(|| NodeId::of::<T>()), }))) } @@ -165,7 +165,10 @@ pub trait Node: 'static { fn construct(ctx: &mut Context, args: &mut Args) -> TypResult<Content>; /// Parse the arguments into style properties for this node. - fn set(args: &mut Args) -> TypResult<StyleMap>; + /// + /// When `constructor` is true, [`construct`](Self::construct) will run + /// after this invocation of `set`. + fn set(args: &mut Args, constructor: bool) -> TypResult<StyleMap>; } /// A user-defined closure. diff --git a/src/eval/value.rs b/src/eval/value.rs index b5607cfd..dd183926 100644 --- a/src/eval/value.rs +++ b/src/eval/value.rs @@ -464,11 +464,19 @@ primitive! { f64: "float", Float, Int(v) => v as f64 } primitive! { RawLength: "length", Length } primitive! { Angle: "angle", Angle } primitive! { Ratio: "ratio", Ratio } -primitive! { Relative<RawLength>: "relative length", Relative, Length(v) => v.into(), Ratio(v) => v.into() } +primitive! { Relative<RawLength>: "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! { Content: "content", + Content, + None => Content::new(), + Str(text) => Content::Text(text) +} primitive! { Array: "array", Array } primitive! { Dict: "dictionary", Dict } primitive! { Func: "function", Func } |
