Skip to content

Commit

Permalink
templater: leverage TemplateProperty::map/and_then() helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
yuja committed Mar 23, 2024
1 parent ec6a307 commit 4ad92b8
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 215 deletions.
26 changes: 12 additions & 14 deletions cli/examples/custom-commit-templater/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use jj_cli::commit_templater::{
};
use jj_cli::template_builder::TemplateLanguage;
use jj_cli::template_parser::{self, TemplateParseError};
use jj_cli::templater::{TemplateFunction, TemplatePropertyError};
use jj_cli::templater::TemplatePropertyExt as _;
use jj_lib::backend::CommitId;
use jj_lib::commit::Commit;
use jj_lib::extensions_map::ExtensionsMap;
Expand All @@ -39,14 +39,14 @@ fn num_digits_in_id(id: &CommitId) -> i64 {
count
}

fn num_char_in_id(commit: Commit, ch_match: char) -> Result<i64, TemplatePropertyError> {
fn num_char_in_id(commit: Commit, ch_match: char) -> i64 {
let mut count = 0;
for ch in commit.id().hex().chars() {
if ch == ch_match {
count += 1;
}
}
Ok(count)
count
}

struct MostDigitsInId {
Expand Down Expand Up @@ -85,19 +85,18 @@ impl CommitTemplateLanguageExtension for HexCounter {
.cache_extension::<MostDigitsInId>()
.unwrap()
.count(language.repo());
Ok(L::wrap_boolean(TemplateFunction::new(
property,
move |commit| Ok(num_digits_in_id(commit.id()) == most_digits),
)))
Ok(L::wrap_boolean(property.map(move |commit| {
num_digits_in_id(commit.id()) == most_digits
})))
},
);
table.commit_methods.insert(
"num_digits_in_id",
|_language, _build_context, property, call| {
template_parser::expect_no_arguments(call)?;
Ok(L::wrap_integer(TemplateFunction::new(property, |commit| {
Ok(num_digits_in_id(commit.id()))
})))
Ok(L::wrap_integer(
property.map(|commit| num_digits_in_id(commit.id())),
))
},
);
table.commit_methods.insert(
Expand All @@ -116,10 +115,9 @@ impl CommitTemplateLanguageExtension for HexCounter {
}
})?;

Ok(L::wrap_integer(TemplateFunction::new(
property,
move |commit| num_char_in_id(commit, char_arg),
)))
Ok(L::wrap_integer(
property.map(move |commit| num_char_in_id(commit, char_arg)),
))
},
);

Expand Down
20 changes: 9 additions & 11 deletions cli/examples/custom-operation-templater/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use jj_cli::operation_templater::{
};
use jj_cli::template_builder::TemplateLanguage;
use jj_cli::template_parser::{self, TemplateParseError};
use jj_cli::templater::{TemplateFunction, TemplatePropertyError};
use jj_cli::templater::TemplatePropertyExt as _;
use jj_lib::extensions_map::ExtensionsMap;
use jj_lib::object_id::ObjectId;
use jj_lib::op_store::OperationId;
Expand All @@ -36,14 +36,14 @@ fn num_digits_in_id(id: &OperationId) -> i64 {
count
}

fn num_char_in_id(operation: Operation, ch_match: char) -> Result<i64, TemplatePropertyError> {
fn num_char_in_id(operation: Operation, ch_match: char) -> i64 {
let mut count = 0;
for ch in operation.id().hex().chars() {
if ch == ch_match {
count += 1;
}
}
Ok(count)
count
}

impl OperationTemplateLanguageExtension for HexCounter {
Expand All @@ -54,10 +54,9 @@ impl OperationTemplateLanguageExtension for HexCounter {
"num_digits_in_id",
|_language, _build_context, property, call| {
template_parser::expect_no_arguments(call)?;
Ok(L::wrap_integer(TemplateFunction::new(
property,
|operation| Ok(num_digits_in_id(operation.id())),
)))
Ok(L::wrap_integer(
property.map(|operation| num_digits_in_id(operation.id())),
))
},
);
table.operation_methods.insert(
Expand All @@ -76,10 +75,9 @@ impl OperationTemplateLanguageExtension for HexCounter {
}
})?;

Ok(L::wrap_integer(TemplateFunction::new(
property,
move |operation| num_char_in_id(operation, char_arg),
)))
Ok(L::wrap_integer(
property.map(move |operation| num_char_in_id(operation, char_arg)),
))
},
);

Expand Down
12 changes: 4 additions & 8 deletions cli/src/commands/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::command_error::{user_error, CommandError};
use crate::config::{AnnotatedValue, ConfigSource};
use crate::generic_templater::GenericTemplateLanguage;
use crate::template_builder::TemplateLanguage as _;
use crate::templater::{Template as _, TemplateFunction};
use crate::templater::{Template as _, TemplatePropertyExt as _};
use crate::ui::Ui;

#[derive(clap::Args, Clone, Debug)]
Expand Down Expand Up @@ -191,20 +191,16 @@ fn config_template_language() -> GenericTemplateLanguage<'static, AnnotatedValue
let mut language = L::new();
// "name" instead of "path" to avoid confusion with the source file path
language.add_keyword("name", |self_property| {
let out_property =
TemplateFunction::new(self_property, |annotated| Ok(annotated.path.join(".")));
let out_property = self_property.map(|annotated| annotated.path.join("."));
Ok(L::wrap_string(out_property))
});
language.add_keyword("value", |self_property| {
// TODO: would be nice if we can provide raw dynamically-typed value
let out_property = TemplateFunction::new(self_property, |annotated| {
Ok(serialize_config_value(&annotated.value))
});
let out_property = self_property.map(|annotated| serialize_config_value(&annotated.value));
Ok(L::wrap_string(out_property))
});
language.add_keyword("overridden", |self_property| {
let out_property =
TemplateFunction::new(self_property, |annotated| Ok(annotated.is_overridden));
let out_property = self_property.map(|annotated| annotated.is_overridden);
Ok(L::wrap_boolean(out_property))
});
language
Expand Down
Loading

0 comments on commit 4ad92b8

Please sign in to comment.