Skip to content

Commit

Permalink
templater: add helper method that unwraps Option<T> property
Browse files Browse the repository at this point in the history
I'll add a few more optional property types, and I don't want to duplicate the
error message. Type names are capitalized for consistency.
  • Loading branch information
yuja committed May 8, 2024
1 parent 11bef2a commit 8ae79f0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
10 changes: 3 additions & 7 deletions cli/src/commit_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use crate::template_builder::{
use crate::template_parser::{self, FunctionCallNode, TemplateParseError, TemplateParseResult};
use crate::templater::{
self, PlainTextFormattedProperty, Template, TemplateFormatter, TemplateProperty,
TemplatePropertyError, TemplatePropertyExt as _,
TemplatePropertyExt as _,
};
use crate::{revset_util, text_util};

Expand Down Expand Up @@ -130,9 +130,7 @@ impl<'repo> TemplateLanguage<'repo> for CommitTemplateLanguage<'repo> {
let type_name = "Commit";
let table = &self.build_fn_table.commit_methods;
let build = template_parser::lookup_method(type_name, table, function)?;
let inner_property = property.and_then(|opt| {
opt.ok_or_else(|| TemplatePropertyError("No commit available".into()))
});
let inner_property = property.try_unwrap(type_name);
build(self, build_ctx, Box::new(inner_property), function)
}
CommitTemplatePropertyKind::CommitList(property) => {
Expand All @@ -154,9 +152,7 @@ impl<'repo> TemplateLanguage<'repo> for CommitTemplateLanguage<'repo> {
let type_name = "RefName";
let table = &self.build_fn_table.ref_name_methods;
let build = template_parser::lookup_method(type_name, table, function)?;
let inner_property = property.and_then(|opt| {
opt.ok_or_else(|| TemplatePropertyError("No RefName available".into()))
});
let inner_property = property.try_unwrap(type_name);
build(self, build_ctx, Box::new(inner_property), function)
}
CommitTemplatePropertyKind::RefNameList(property) => {
Expand Down
11 changes: 11 additions & 0 deletions cli/src/templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,17 @@ pub trait TemplatePropertyExt: TemplateProperty {
TemplateFunction::new(self, move |value| Ok(function(value)))
}

/// Translates to a property that will unwrap an extracted `Option` value
/// of the specified `type_name`, mapping `None` to `Err`.
fn try_unwrap<O>(self, type_name: &str) -> impl TemplateProperty<Output = O>
where
Self: TemplateProperty<Output = Option<O>> + Sized,
{
self.and_then(move |opt| {
opt.ok_or_else(|| TemplatePropertyError(format!("No {type_name} available").into()))
})
}

/// Converts this property into `Template`.
fn into_template<'a>(self) -> Box<dyn Template + 'a>
where
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/test_global_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ fn test_color_ui_messages() {
);
insta::assert_snapshot!(stdout, @r###"
167f90e7600a50f85c4f909b53eaf546faa82879
[1m[39m<[38;5;1mError: [39mNo commit available>[0m [38;5;8m(elided revisions)[39m
[1m[39m<[38;5;1mError: [39mNo Commit available>[0m [38;5;8m(elided revisions)[39m
0000000000000000000000000000000000000000
"###);

Expand Down
2 changes: 1 addition & 1 deletion cli/tests/test_tag_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fn test_tag_list() {
[conflicted_tag]
present: true
conflict: true
normal_target: <Error: No commit available>
normal_target: <Error: No Commit available>
removed_targets: commit1
added_targets: commit2 commit3
[test_tag]
Expand Down

0 comments on commit 8ae79f0

Please sign in to comment.