forked from mirrors/jj
templater: implement context-less Template::format() on value types
This is a slightly better version of the one I described in: https://github.com/martinvonz/jj/pull/1098#issuecomment-1399476487 These impls will replace coerce_to_string() to support labeled outputs. We could allow arbitrary context type 'C', but I feel uncomfortable with that. So let's start with () until we find it doesn't work out.
This commit is contained in:
parent
c1f0ca9b5a
commit
23ab89d200
1 changed files with 31 additions and 0 deletions
|
@ -25,11 +25,36 @@ use jujutsu_lib::revset::RevsetExpression;
|
|||
use jujutsu_lib::rewrite::merge_commit_trees;
|
||||
|
||||
use crate::formatter::Formatter;
|
||||
use crate::time_util;
|
||||
|
||||
pub trait Template<C> {
|
||||
fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()>;
|
||||
}
|
||||
|
||||
impl Template<()> for Signature {
|
||||
fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> {
|
||||
formatter.write_str(&self.name)
|
||||
}
|
||||
}
|
||||
|
||||
impl Template<()> for String {
|
||||
fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> {
|
||||
formatter.write_str(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Template<()> for Timestamp {
|
||||
fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> {
|
||||
formatter.write_str(&time_util::format_absolute_timestamp(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl Template<()> for bool {
|
||||
fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> {
|
||||
formatter.write_str(if *self { "true" } else { "false" })
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LiteralTemplate(pub String);
|
||||
|
||||
impl<C> Template<C> for LiteralTemplate {
|
||||
|
@ -411,6 +436,12 @@ impl CommitOrChangeId {
|
|||
}
|
||||
}
|
||||
|
||||
impl Template<()> for CommitOrChangeId {
|
||||
fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> {
|
||||
formatter.write_str(&self.hex())
|
||||
}
|
||||
}
|
||||
|
||||
fn highlight_shortest_prefix(id: &CommitOrChangeId, total_len: usize, repo: RepoRef) -> String {
|
||||
let prefix_len = repo
|
||||
.base_repo()
|
||||
|
|
Loading…
Reference in a new issue