forked from mirrors/jj
templater: generalize Literal/Constant wrapper for any context-less template
Now we have 'impl Template<()> for String', 'LiteralTemplate(String)' is a bit redundant. Let's generalize it for any 'Template<()>'. I noticed 'ConstantTemplateProperty' serves as a similar role, so unified these.
This commit is contained in:
parent
23ab89d200
commit
431395a7ee
2 changed files with 17 additions and 22 deletions
|
@ -24,11 +24,10 @@ use crate::formatter::PlainTextFormatter;
|
|||
use crate::templater::{
|
||||
AuthorProperty, BranchProperty, ChangeIdProperty, CommitIdProperty, CommitOrChangeId,
|
||||
CommitOrChangeIdShort, CommitOrChangeIdShortPrefixAndBrackets, CommitterProperty,
|
||||
ConditionalTemplate, ConflictProperty, ConstantTemplateProperty, DescriptionProperty,
|
||||
DivergentProperty, DynamicLabelTemplate, EmptyProperty, GitRefsProperty, IsGitHeadProperty,
|
||||
IsWorkingCopyProperty, LabelTemplate, ListTemplate, LiteralTemplate, SignatureTimestamp,
|
||||
StringPropertyTemplate, TagProperty, Template, TemplateFunction, TemplateProperty,
|
||||
WorkingCopiesProperty,
|
||||
ConditionalTemplate, ConflictProperty, DescriptionProperty, DivergentProperty,
|
||||
DynamicLabelTemplate, EmptyProperty, GitRefsProperty, IsGitHeadProperty, IsWorkingCopyProperty,
|
||||
LabelTemplate, ListTemplate, Literal, SignatureTimestamp, StringPropertyTemplate, TagProperty,
|
||||
Template, TemplateFunction, TemplateProperty, WorkingCopiesProperty,
|
||||
};
|
||||
use crate::time_util;
|
||||
|
||||
|
@ -315,7 +314,7 @@ fn parse_commit_term<'a>(
|
|||
) -> Box<dyn Template<Commit> + 'a> {
|
||||
assert_eq!(pair.as_rule(), Rule::term);
|
||||
if pair.as_str().is_empty() {
|
||||
Box::new(LiteralTemplate(String::new()))
|
||||
Box::new(Literal(String::new()))
|
||||
} else {
|
||||
let mut inner = pair.into_inner();
|
||||
let expr = inner.next().unwrap();
|
||||
|
@ -325,10 +324,9 @@ fn parse_commit_term<'a>(
|
|||
Rule::literal => {
|
||||
let text = parse_string_literal(expr);
|
||||
if maybe_method.as_str().is_empty() {
|
||||
Box::new(LiteralTemplate(text))
|
||||
Box::new(Literal(text))
|
||||
} else {
|
||||
let input_property =
|
||||
Property::String(Box::new(ConstantTemplateProperty { output: text }));
|
||||
let input_property = Property::String(Box::new(Literal(text)));
|
||||
let PropertyAndLabels(property, method_labels) =
|
||||
parse_method_chain(maybe_method, input_property);
|
||||
let string_property = coerce_to_string(property);
|
||||
|
@ -437,7 +435,7 @@ fn parse_commit_template_rule<'a>(
|
|||
}
|
||||
Box::new(ListTemplate(formatters))
|
||||
}
|
||||
_ => Box::new(LiteralTemplate(String::new())),
|
||||
_ => Box::new(Literal(String::new())),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -55,14 +55,6 @@ impl Template<()> for bool {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct LiteralTemplate(pub String);
|
||||
|
||||
impl<C> Template<C> for LiteralTemplate {
|
||||
fn format(&self, _context: &C, formatter: &mut dyn Formatter) -> io::Result<()> {
|
||||
formatter.write_str(&self.0)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: figure out why this lifetime is needed
|
||||
pub struct LabelTemplate<'a, C> {
|
||||
content: Box<dyn Template<C> + 'a>,
|
||||
|
@ -138,13 +130,18 @@ pub trait TemplateProperty<C, O> {
|
|||
fn extract(&self, context: &C) -> O;
|
||||
}
|
||||
|
||||
pub struct ConstantTemplateProperty<O> {
|
||||
pub output: O,
|
||||
/// Adapter to drop template context.
|
||||
pub struct Literal<O>(pub O);
|
||||
|
||||
impl<C, O: Template<()>> Template<C> for Literal<O> {
|
||||
fn format(&self, _context: &C, formatter: &mut dyn Formatter) -> io::Result<()> {
|
||||
self.0.format(&(), formatter)
|
||||
}
|
||||
}
|
||||
|
||||
impl<C, O: Clone> TemplateProperty<C, O> for ConstantTemplateProperty<O> {
|
||||
impl<C, O: Clone> TemplateProperty<C, O> for Literal<O> {
|
||||
fn extract(&self, _context: &C) -> O {
|
||||
self.output.clone()
|
||||
self.0.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue