mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-31 00:12:06 +00:00
templater: add helper to create Expression with/without labels
These functions aren't suffixed with _property, since Expression::Template() will be flattened into P::Template().
This commit is contained in:
parent
e5a4c2e75b
commit
0f87649696
1 changed files with 13 additions and 4 deletions
|
@ -725,6 +725,16 @@ pub enum Expression<'a, C, P> {
|
||||||
Template(Box<dyn Template<C> + 'a>),
|
Template(Box<dyn Template<C> + 'a>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a, C: 'a, P> Expression<'a, C, P> {
|
||||||
|
fn unlabeled(property: P) -> Self {
|
||||||
|
Expression::Property(property, vec![])
|
||||||
|
}
|
||||||
|
|
||||||
|
fn with_label(property: P, label: impl Into<String>) -> Self {
|
||||||
|
Expression::Property(property, vec![label.into()])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a, C: 'a, P: IntoTemplateProperty<'a, C>> Expression<'a, C, P> {
|
impl<'a, C: 'a, P: IntoTemplateProperty<'a, C>> Expression<'a, C, P> {
|
||||||
pub fn try_into_boolean(self) -> Option<Box<dyn TemplateProperty<C, Output = bool> + 'a>> {
|
pub fn try_into_boolean(self) -> Option<Box<dyn TemplateProperty<C, Output = bool> + 'a>> {
|
||||||
match self {
|
match self {
|
||||||
|
@ -1089,16 +1099,15 @@ pub fn build_expression<'a, L: TemplateLanguage<'a>>(
|
||||||
match &node.kind {
|
match &node.kind {
|
||||||
ExpressionKind::Identifier(name) => {
|
ExpressionKind::Identifier(name) => {
|
||||||
let property = language.build_keyword(name, node.span)?;
|
let property = language.build_keyword(name, node.span)?;
|
||||||
let labels = vec![(*name).to_owned()];
|
Ok(Expression::with_label(property, *name))
|
||||||
Ok(Expression::Property(property, labels))
|
|
||||||
}
|
}
|
||||||
ExpressionKind::Integer(value) => {
|
ExpressionKind::Integer(value) => {
|
||||||
let property = language.wrap_integer(Literal(*value));
|
let property = language.wrap_integer(Literal(*value));
|
||||||
Ok(Expression::Property(property, vec![]))
|
Ok(Expression::unlabeled(property))
|
||||||
}
|
}
|
||||||
ExpressionKind::String(value) => {
|
ExpressionKind::String(value) => {
|
||||||
let property = language.wrap_string(Literal(value.clone()));
|
let property = language.wrap_string(Literal(value.clone()));
|
||||||
Ok(Expression::Property(property, vec![]))
|
Ok(Expression::unlabeled(property))
|
||||||
}
|
}
|
||||||
ExpressionKind::List(nodes) => {
|
ExpressionKind::List(nodes) => {
|
||||||
let templates = nodes
|
let templates = nodes
|
||||||
|
|
Loading…
Reference in a new issue