style: description template extractor

This also prevents the creating of an extra empty String when
the description is empty.
This commit is contained in:
Samuel Tardieu 2023-01-14 11:32:01 +01:00
parent bbd49cdf29
commit 665f7c5917

View file

@ -15,7 +15,7 @@
use std::borrow::BorrowMut;
use std::collections::{HashMap, HashSet};
use std::io;
use std::ops::{Add, AddAssign};
use std::ops::AddAssign;
use itertools::Itertools;
use jujutsu_lib::backend::{ChangeId, CommitId, ObjectId, Signature, Timestamp};
@ -166,13 +166,10 @@ pub struct DescriptionProperty;
impl TemplateProperty<Commit, String> for DescriptionProperty {
fn extract(&self, context: &Commit) -> String {
let description = context.description().to_owned();
if description.ends_with('\n') {
description
} else if description.is_empty() {
"(no description set)\n".to_string()
} else {
description.add("\n")
match context.description() {
s if s.is_empty() => "(no description set)\n".to_owned(),
s if s.ends_with('\n') => s.to_owned(),
s => format!("{s}\n"),
}
}
}