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
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.
@ -121,19 +121,20 @@ pub enum TemplateNode {
},
/// A template that was converted from a string.
Str(String),
/// A template that can implement custom behaviour.
Any(TemplateAny),
/// A function template that can implement custom behaviour.
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)]
pub struct TemplateAny {
pub struct TemplateFunc {
name: String,
f: Rc<dyn Fn(&mut ExecContext)>,
}
impl TemplateAny {
/// Create a new dynamic template value from a rust function or closure.
impl TemplateFunc {
/// Create a new function template from a rust function or closure.
pub fn new<F>(name: impl Into<String>, f: F) -> Self
where
F: Fn(&mut ExecContext) + 'static,
@ -147,14 +148,14 @@ impl TemplateAny {
}
}
impl PartialEq for TemplateAny {
impl PartialEq for TemplateFunc {
fn eq(&self, _: &Self) -> bool {
// TODO: Figure out what we want here.
false
}
}
impl Deref for TemplateAny {
impl Deref for TemplateFunc {
type Target = dyn Fn(&mut ExecContext);
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 {
f.debug_struct("TemplateAny").finish()
}

View File

@ -10,7 +10,7 @@ use std::rc::Rc;
use crate::diag::Pass;
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::layout::{self, Expansion, NodeSpacing, NodeStack};
use crate::pretty::pretty;
@ -156,12 +156,12 @@ impl Exec for TemplateNode {
match self {
Self::Tree { tree, map } => tree.exec_with_map(ctx, &map),
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) {
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)));
}
// Function template.
// Bracket function.
Token::HashBracket => {
*at_start = false;
return Some(Node::Expr(bracket_call(p)?));

View File

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

View File

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

View File

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

View File

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