diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-08-19 20:49:01 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-08-19 20:49:01 +0200 |
| commit | 77dac270a8a99f24a6fc0eb9e92256bcc07c586c (patch) | |
| tree | 8e240b798a5c1aabd77c823e65828f3c6d2557f1 /src/compute/value.rs | |
| parent | 6d7e7d945b315469b80bca3466a96534b2a17639 (diff) | |
Make compute functions possible 💻
Ships with the amazing new `rgb` function!
Diffstat (limited to 'src/compute/value.rs')
| -rw-r--r-- | src/compute/value.rs | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/src/compute/value.rs b/src/compute/value.rs index 32f2778b..c11a3d31 100644 --- a/src/compute/value.rs +++ b/src/compute/value.rs @@ -7,11 +7,11 @@ use std::rc::Rc; use fontdock::{FontStyle, FontWeight, FontWidth}; use crate::color::RgbaColor; -use crate::layout::{Commands, Dir, LayoutContext, SpecAlign}; +use crate::layout::{Command, Commands, Dir, LayoutContext, SpecAlign}; use crate::length::{Length, ScaleLength}; use crate::paper::Paper; use crate::syntax::span::{Span, Spanned}; -use crate::syntax::tree::SyntaxTree; +use crate::syntax::tree::{SyntaxTree, SyntaxNode}; use crate::syntax::Ident; use crate::{DynFuture, Feedback, Pass}; use super::table::{SpannedEntry, Table}; @@ -61,6 +61,47 @@ impl Value { } } +impl Spanned<Value> { + /// Transform this value into something layoutable. + /// + /// If this is already a command-value, it is simply unwrapped, otherwise + /// the value is represented as layoutable content in a reasonable way. + pub fn into_commands(self) -> Commands { + match self.v { + Value::Commands(commands) => commands, + Value::Tree(tree) => vec![Command::LayoutSyntaxTree(tree)], + + // Forward to each entry, separated with spaces. + Value::Table(table) => { + let mut commands = vec![]; + let mut end = None; + for entry in table.into_values() { + if let Some(last_end) = end { + let span = Span::new(last_end, entry.key.start); + commands.push(Command::LayoutSyntaxTree(vec![ + Spanned::new(SyntaxNode::Spacing, span) + ])); + } + + end = Some(entry.val.span.end); + commands.extend(entry.val.into_commands()); + } + commands + } + + // Format with debug. + val => vec![ + Command::LayoutSyntaxTree(vec![ + Spanned::new( + SyntaxNode::Text(format!("{:?}", val)), + self.span, + ) + ]) + ], + } + } +} + impl Debug for Value { fn fmt(&self, f: &mut Formatter) -> fmt::Result { use Value::*; |
