templater: add CommitOrChangeId constructor to help inline keywords

This commit is contained in:
Yuya Nishihara 2023-01-31 17:56:59 +09:00
parent dc0e3c1855
commit 846be15132

View file

@ -486,7 +486,14 @@ pub struct CommitOrChangeId<'a> {
id_bytes: Vec<u8>, id_bytes: Vec<u8>,
} }
impl CommitOrChangeId<'_> { impl<'a> CommitOrChangeId<'a> {
pub fn new(repo: RepoRef<'a>, id: &impl ObjectId) -> Self {
CommitOrChangeId {
repo,
id_bytes: id.to_bytes(),
}
}
pub fn as_bytes(&self) -> &[u8] { pub fn as_bytes(&self) -> &[u8] {
&self.id_bytes &self.id_bytes
} }
@ -601,10 +608,7 @@ impl<'a> TemplateProperty<Commit> for CommitIdProperty<'a> {
type Output = CommitOrChangeId<'a>; type Output = CommitOrChangeId<'a>;
fn extract(&self, context: &Commit) -> Self::Output { fn extract(&self, context: &Commit) -> Self::Output {
CommitOrChangeId { CommitOrChangeId::new(self.repo, context.id())
repo: self.repo,
id_bytes: context.id().to_bytes(),
}
} }
} }
@ -616,10 +620,7 @@ impl<'a> TemplateProperty<Commit> for ChangeIdProperty<'a> {
type Output = CommitOrChangeId<'a>; type Output = CommitOrChangeId<'a>;
fn extract(&self, context: &Commit) -> Self::Output { fn extract(&self, context: &Commit) -> Self::Output {
CommitOrChangeId { CommitOrChangeId::new(self.repo, context.change_id())
repo: self.repo,
id_bytes: context.change_id().to_bytes(),
}
} }
} }