Rename any template to func template ✏

This commit is contained in:
Laurenz 2021-02-12 23:14:29 +01:00
parent 094462cbdd
commit db1659a987
7 changed files with 25 additions and 24 deletions

View File

@ -55,7 +55,7 @@ impl Value {
where where
F: Fn(&mut ExecContext) + 'static, F: Fn(&mut ExecContext) + 'static,
{ {
Self::Template(vec![TemplateNode::Any(TemplateAny::new(name, f))]) Self::Template(vec![TemplateNode::Func(TemplateFunc::new(name, f))])
} }
/// The name of the stored value's type. /// The name of the stored value's type.
@ -121,19 +121,20 @@ pub enum TemplateNode {
}, },
/// A template that was converted from a string. /// A template that was converted from a string.
Str(String), Str(String),
/// A template that can implement custom behaviour. /// A function template that can implement custom behaviour.
Any(TemplateAny), Func(TemplateFunc),
} }
/// A reference-counted dynamic template node (can implement custom behaviour). /// A reference-counted dynamic template node that can implement custom
/// behaviour.
#[derive(Clone)] #[derive(Clone)]
pub struct TemplateAny { pub struct TemplateFunc {
name: String, name: String,
f: Rc<dyn Fn(&mut ExecContext)>, f: Rc<dyn Fn(&mut ExecContext)>,
} }
impl TemplateAny { impl TemplateFunc {
/// Create a new dynamic template value from a rust function or closure. /// Create a new function template from a rust function or closure.
pub fn new<F>(name: impl Into<String>, f: F) -> Self pub fn new<F>(name: impl Into<String>, f: F) -> Self
where where
F: Fn(&mut ExecContext) + 'static, F: Fn(&mut ExecContext) + 'static,
@ -147,14 +148,14 @@ impl TemplateAny {
} }
} }
impl PartialEq for TemplateAny { impl PartialEq for TemplateFunc {
fn eq(&self, _: &Self) -> bool { fn eq(&self, _: &Self) -> bool {
// TODO: Figure out what we want here. // TODO: Figure out what we want here.
false false
} }
} }
impl Deref for TemplateAny { impl Deref for TemplateFunc {
type Target = dyn Fn(&mut ExecContext); type Target = dyn Fn(&mut ExecContext);
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
@ -162,7 +163,7 @@ impl Deref for TemplateAny {
} }
} }
impl Debug for TemplateAny { impl Debug for TemplateFunc {
fn fmt(&self, f: &mut Formatter) -> fmt::Result { fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_struct("TemplateAny").finish() f.debug_struct("TemplateAny").finish()
} }

View File

@ -10,7 +10,7 @@ use std::rc::Rc;
use crate::diag::Pass; use crate::diag::Pass;
use crate::env::Env; use crate::env::Env;
use crate::eval::{ExprMap, TemplateAny, TemplateNode, Value, ValueTemplate}; use crate::eval::{ExprMap, TemplateFunc, TemplateNode, Value, ValueTemplate};
use crate::geom::Spec; use crate::geom::Spec;
use crate::layout::{self, Expansion, NodeSpacing, NodeStack}; use crate::layout::{self, Expansion, NodeSpacing, NodeStack};
use crate::pretty::pretty; use crate::pretty::pretty;
@ -156,12 +156,12 @@ impl Exec for TemplateNode {
match self { match self {
Self::Tree { tree, map } => tree.exec_with_map(ctx, &map), Self::Tree { tree, map } => tree.exec_with_map(ctx, &map),
Self::Str(s) => ctx.push_text(s), Self::Str(s) => ctx.push_text(s),
Self::Any(any) => any.exec(ctx), Self::Func(func) => func.exec(ctx),
} }
} }
} }
impl Exec for TemplateAny { impl Exec for TemplateFunc {
fn exec(&self, ctx: &mut ExecContext) { fn exec(&self, ctx: &mut ExecContext) {
self(ctx); self(ctx);
} }

View File

@ -100,7 +100,7 @@ fn node(p: &mut Parser, at_start: &mut bool) -> Option<Node> {
return Some(Node::Expr(template(p))); return Some(Node::Expr(template(p)));
} }
// Function template. // Bracket function.
Token::HashBracket => { Token::HashBracket => {
*at_start = false; *at_start = false;
return Some(Node::Expr(bracket_call(p)?)); return Some(Node::Expr(bracket_call(p)?));

View File

@ -73,7 +73,7 @@ impl<'s> Iterator for Tokens<'s> {
'{' => Token::LeftBrace, '{' => Token::LeftBrace,
'}' => Token::RightBrace, '}' => Token::RightBrace,
// Keywords, function templates, colors. // Keywords, bracket functions, colors.
'#' => self.hash(start), '#' => self.hash(start),
// Whitespace. // Whitespace.

View File

@ -3,8 +3,8 @@
pub use crate::diag::{Diag, Pass}; pub use crate::diag::{Diag, Pass};
#[doc(no_inline)] #[doc(no_inline)]
pub use crate::eval::{ pub use crate::eval::{
CastResult, Eval, EvalContext, TemplateAny, TemplateNode, Value, ValueAny, ValueArgs, CastResult, Eval, EvalContext, TemplateFunc, TemplateNode, Value, ValueAny,
ValueArray, ValueDict, ValueTemplate, ValueArgs, ValueArray, ValueDict, ValueTemplate,
}; };
#[doc(no_inline)] #[doc(no_inline)]
pub use crate::exec::{Exec, ExecContext}; pub use crate::exec::{Exec, ExecContext};

View File

@ -128,7 +128,7 @@ impl PrettyWithMap for Node {
let value = &map[&(expr as *const _)]; let value = &map[&(expr as *const _)];
value.pretty(p); value.pretty(p);
} else if let Expr::Call(call) = expr { } else if let Expr::Call(call) = expr {
// Format function templates appropriately. // Format bracket functions appropriately.
pretty_bracketed(call, p, false) pretty_bracketed(call, p, false)
} else { } else {
expr.pretty(p); expr.pretty(p);
@ -360,7 +360,7 @@ impl Pretty for ExprCall {
} }
} }
/// Pretty print a function template, with body or chaining when possible. /// Pretty print a bracket function, with body or chaining when possible.
pub fn pretty_bracketed(call: &ExprCall, p: &mut Printer, chained: bool) { pub fn pretty_bracketed(call: &ExprCall, p: &mut Printer, chained: bool) {
if chained { if chained {
p.push_str(" | "); p.push_str(" | ");
@ -539,12 +539,12 @@ impl Pretty for TemplateNode {
match self { match self {
Self::Tree { tree, map } => tree.pretty_with_map(p, Some(map)), Self::Tree { tree, map } => tree.pretty_with_map(p, Some(map)),
Self::Str(s) => p.push_str(s), Self::Str(s) => p.push_str(s),
Self::Any(any) => any.pretty(p), Self::Func(func) => func.pretty(p),
} }
} }
} }
impl Pretty for TemplateAny { impl Pretty for TemplateFunc {
fn pretty(&self, p: &mut Printer) { fn pretty(&self, p: &mut Printer) {
p.push_str("<node "); p.push_str("<node ");
p.push_str(self.name()); p.push_str(self.name());
@ -741,7 +741,7 @@ mod tests {
roundtrip("{v(1)}"); roundtrip("{v(1)}");
roundtrip("{v(a: 1, b)}"); roundtrip("{v(a: 1, b)}");
// Function templates. // Bracket functions.
roundtrip("#[v]"); roundtrip("#[v]");
roundtrip("#[v 1]"); roundtrip("#[v 1]");
roundtrip("#[v 1, 2][*Ok*]"); roundtrip("#[v 1, 2][*Ok*]");
@ -800,7 +800,7 @@ mod tests {
tree: Rc::new(vec![Node::Strong]), tree: Rc::new(vec![Node::Strong]),
map: HashMap::new(), map: HashMap::new(),
}, },
TemplateNode::Any(TemplateAny::new("example", |_| {})), TemplateNode::Func(TemplateFunc::new("example", |_| {})),
], ],
"[*<node example>]", "[*<node example>]",
); );

View File

@ -191,7 +191,7 @@ impl<'s> Token<'s> {
pub fn name(self) -> &'static str { pub fn name(self) -> &'static str {
match self { match self {
Self::LeftBracket => "opening bracket", Self::LeftBracket => "opening bracket",
Self::HashBracket => "start of function template", Self::HashBracket => "start of bracket function",
Self::RightBracket => "closing bracket", Self::RightBracket => "closing bracket",
Self::LeftBrace => "opening brace", Self::LeftBrace => "opening brace",
Self::RightBrace => "closing brace", Self::RightBrace => "closing brace",