ok/jj
1
0
Fork 0
forked from mirrors/jj

templater: inline TemplateFormatter into write_commit_summary()

It's not used extensively, and I don't think this abstraction is useful
for the moment.
This commit is contained in:
Yuya Nishihara 2023-01-14 18:30:14 +09:00
parent e716168368
commit d2b8240648
2 changed files with 1 additions and 24 deletions

View file

@ -58,7 +58,6 @@ use tracing_subscriber::prelude::*;
use crate::config::{FullCommandArgs, LayeredConfigs}; use crate::config::{FullCommandArgs, LayeredConfigs};
use crate::formatter::{Formatter, PlainTextFormatter}; use crate::formatter::{Formatter, PlainTextFormatter};
use crate::merge_tools::{ConflictResolveError, DiffEditError}; use crate::merge_tools::{ConflictResolveError, DiffEditError};
use crate::templater::TemplateFormatter;
use crate::ui::{ColorChoice, Ui}; use crate::ui::{ColorChoice, Ui};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -1389,9 +1388,7 @@ pub fn write_commit_summary(
.unwrap_or_else(|_| String::from(r#"commit_id.short() " " description.first_line()"#)); .unwrap_or_else(|_| String::from(r#"commit_id.short() " " description.first_line()"#));
let template = let template =
crate::template_parser::parse_commit_template(repo, workspace_id, &template_string); crate::template_parser::parse_commit_template(repo, workspace_id, &template_string);
let mut template_writer = TemplateFormatter::new(template, formatter); template.format(commit, formatter)
template_writer.format(commit)?;
Ok(())
} }
pub fn write_config_entry( pub fn write_config_entry(

View file

@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use std::borrow::BorrowMut;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::io; use std::io;
use std::ops::AddAssign; use std::ops::AddAssign;
@ -31,25 +30,6 @@ pub trait Template<C> {
fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()>; fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()>;
} }
// TODO: Extract a trait for this type?
pub struct TemplateFormatter<'f, 't: 'f, C> {
template: Box<dyn Template<C> + 't>,
formatter: &'f mut dyn Formatter,
}
impl<'f, 't: 'f, C> TemplateFormatter<'f, 't, C> {
pub fn new(template: Box<dyn Template<C> + 't>, formatter: &'f mut dyn Formatter) -> Self {
TemplateFormatter {
template,
formatter,
}
}
pub fn format<'c, 'a: 'c>(&'a mut self, context: &'c C) -> io::Result<()> {
self.template.format(context, self.formatter.borrow_mut())
}
}
pub struct LiteralTemplate(pub String); pub struct LiteralTemplate(pub String);
impl<C> Template<C> for LiteralTemplate { impl<C> Template<C> for LiteralTemplate {