Skip to content

Commit

Permalink
templater: remove Context type from TemplateLanguage/Property
Browse files Browse the repository at this point in the history
Now a compiled template doesn't have a static Context type internally. A
property is basically of "Fn() -> Result<O, _>" type, and a type-erased "self"
variable will be injected as needed.

Template<C> types will be refactored separately.
  • Loading branch information
yuja committed Mar 22, 2024
1 parent 20d6aa9 commit cf87d36
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 177 deletions.
4 changes: 2 additions & 2 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl CommandHelper {
/// This function also loads template aliases from the settings. Use
/// `WorkspaceCommandHelper::parse_template()` if you've already
/// instantiated the workspace helper.
pub fn parse_template<'a, C: Clone + 'a, L: TemplateLanguage<'a, Context = ()> + ?Sized>(
pub fn parse_template<'a, C: Clone + 'a, L: TemplateLanguage<'a> + ?Sized>(
&self,
ui: &Ui,
language: &L,
Expand Down Expand Up @@ -899,7 +899,7 @@ Set which revision the branch points to with `jj branch set {branch_name} -r <RE
///
/// `wrap_self` specifies the type of the top-level property, which should
/// be one of the `L::wrap_*()` functions.
pub fn parse_template<'a, C: Clone + 'a, L: TemplateLanguage<'a, Context = ()> + ?Sized>(
pub fn parse_template<'a, C: Clone + 'a, L: TemplateLanguage<'a> + ?Sized>(
&self,
language: &L,
template_text: &str,
Expand Down
35 changes: 17 additions & 18 deletions cli/src/commit_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ impl<'repo> CommitTemplateLanguage<'repo> {
}

impl<'repo> TemplateLanguage<'repo> for CommitTemplateLanguage<'repo> {
type Context = ();
type Property = CommitTemplatePropertyKind<'repo>;

template_builder::impl_core_wrap_property_fns!('repo, CommitTemplatePropertyKind::Core);
Expand Down Expand Up @@ -185,54 +184,54 @@ impl<'repo> CommitTemplateLanguage<'repo> {
}

pub fn wrap_commit(
property: impl TemplateProperty<(), Output = Commit> + 'repo,
property: impl TemplateProperty<Output = Commit> + 'repo,
) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::Commit(Box::new(property))
}

pub fn wrap_commit_list(
property: impl TemplateProperty<(), Output = Vec<Commit>> + 'repo,
property: impl TemplateProperty<Output = Vec<Commit>> + 'repo,
) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::CommitList(Box::new(property))
}

pub fn wrap_ref_name(
property: impl TemplateProperty<(), Output = RefName> + 'repo,
property: impl TemplateProperty<Output = RefName> + 'repo,
) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::RefName(Box::new(property))
}

pub fn wrap_ref_name_list(
property: impl TemplateProperty<(), Output = Vec<RefName>> + 'repo,
property: impl TemplateProperty<Output = Vec<RefName>> + 'repo,
) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::RefNameList(Box::new(property))
}

pub fn wrap_commit_or_change_id(
property: impl TemplateProperty<(), Output = CommitOrChangeId> + 'repo,
property: impl TemplateProperty<Output = CommitOrChangeId> + 'repo,
) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::CommitOrChangeId(Box::new(property))
}

pub fn wrap_shortest_id_prefix(
property: impl TemplateProperty<(), Output = ShortestIdPrefix> + 'repo,
property: impl TemplateProperty<Output = ShortestIdPrefix> + 'repo,
) -> CommitTemplatePropertyKind<'repo> {
CommitTemplatePropertyKind::ShortestIdPrefix(Box::new(property))
}
}

pub enum CommitTemplatePropertyKind<'repo> {
Core(CoreTemplatePropertyKind<'repo, ()>),
Commit(Box<dyn TemplateProperty<(), Output = Commit> + 'repo>),
CommitList(Box<dyn TemplateProperty<(), Output = Vec<Commit>> + 'repo>),
RefName(Box<dyn TemplateProperty<(), Output = RefName> + 'repo>),
RefNameList(Box<dyn TemplateProperty<(), Output = Vec<RefName>> + 'repo>),
CommitOrChangeId(Box<dyn TemplateProperty<(), Output = CommitOrChangeId> + 'repo>),
ShortestIdPrefix(Box<dyn TemplateProperty<(), Output = ShortestIdPrefix> + 'repo>),
Core(CoreTemplatePropertyKind<'repo>),
Commit(Box<dyn TemplateProperty<Output = Commit> + 'repo>),
CommitList(Box<dyn TemplateProperty<Output = Vec<Commit>> + 'repo>),
RefName(Box<dyn TemplateProperty<Output = RefName> + 'repo>),
RefNameList(Box<dyn TemplateProperty<Output = Vec<RefName>> + 'repo>),
CommitOrChangeId(Box<dyn TemplateProperty<Output = CommitOrChangeId> + 'repo>),
ShortestIdPrefix(Box<dyn TemplateProperty<Output = ShortestIdPrefix> + 'repo>),
}

impl<'repo> IntoTemplateProperty<'repo, ()> for CommitTemplatePropertyKind<'repo> {
fn try_into_boolean(self) -> Option<Box<dyn TemplateProperty<(), Output = bool> + 'repo>> {
impl<'repo> IntoTemplateProperty<'repo> for CommitTemplatePropertyKind<'repo> {
fn try_into_boolean(self) -> Option<Box<dyn TemplateProperty<Output = bool> + 'repo>> {
match self {
CommitTemplatePropertyKind::Core(property) => property.try_into_boolean(),
CommitTemplatePropertyKind::Commit(_) => None,
Expand All @@ -252,14 +251,14 @@ impl<'repo> IntoTemplateProperty<'repo, ()> for CommitTemplatePropertyKind<'repo
}
}

fn try_into_integer(self) -> Option<Box<dyn TemplateProperty<(), Output = i64> + 'repo>> {
fn try_into_integer(self) -> Option<Box<dyn TemplateProperty<Output = i64> + 'repo>> {
match self {
CommitTemplatePropertyKind::Core(property) => property.try_into_integer(),
_ => None,
}
}

fn try_into_plain_text(self) -> Option<Box<dyn TemplateProperty<(), Output = String> + 'repo>> {
fn try_into_plain_text(self) -> Option<Box<dyn TemplateProperty<Output = String> + 'repo>> {
match self {
CommitTemplatePropertyKind::Core(property) => property.try_into_plain_text(),
_ => {
Expand Down
19 changes: 9 additions & 10 deletions cli/src/generic_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl<'a, C> GenericTemplateLanguage<'a, C> {
pub fn add_keyword<F>(&mut self, name: &'static str, build: F)
where
F: Fn(
Box<dyn TemplateProperty<(), Output = C> + 'a>,
Box<dyn TemplateProperty<Output = C> + 'a>,
) -> TemplateParseResult<GenericTemplatePropertyKind<'a, C>>
+ 'a,
{
Expand All @@ -75,7 +75,6 @@ impl<'a, C> GenericTemplateLanguage<'a, C> {
}

impl<'a, C: 'a> TemplateLanguage<'a> for GenericTemplateLanguage<'a, C> {
type Context = ();
type Property = GenericTemplatePropertyKind<'a, C>;

template_builder::impl_core_wrap_property_fns!('a, GenericTemplatePropertyKind::Core);
Expand Down Expand Up @@ -113,33 +112,33 @@ impl<'a, C: 'a> TemplateLanguage<'a> for GenericTemplateLanguage<'a, C> {

impl<'a, C> GenericTemplateLanguage<'a, C> {
pub fn wrap_self(
property: impl TemplateProperty<(), Output = C> + 'a,
property: impl TemplateProperty<Output = C> + 'a,
) -> GenericTemplatePropertyKind<'a, C> {
GenericTemplatePropertyKind::Self_(Box::new(property))
}
}

pub enum GenericTemplatePropertyKind<'a, C> {
Core(CoreTemplatePropertyKind<'a, ()>),
Self_(Box<dyn TemplateProperty<(), Output = C> + 'a>),
Core(CoreTemplatePropertyKind<'a>),
Self_(Box<dyn TemplateProperty<Output = C> + 'a>),
}

impl<'a, C: 'a> IntoTemplateProperty<'a, ()> for GenericTemplatePropertyKind<'a, C> {
fn try_into_boolean(self) -> Option<Box<dyn TemplateProperty<(), Output = bool> + 'a>> {
impl<'a, C: 'a> IntoTemplateProperty<'a> for GenericTemplatePropertyKind<'a, C> {
fn try_into_boolean(self) -> Option<Box<dyn TemplateProperty<Output = bool> + 'a>> {
match self {
GenericTemplatePropertyKind::Core(property) => property.try_into_boolean(),
GenericTemplatePropertyKind::Self_(_) => None,
}
}

fn try_into_integer(self) -> Option<Box<dyn TemplateProperty<(), Output = i64> + 'a>> {
fn try_into_integer(self) -> Option<Box<dyn TemplateProperty<Output = i64> + 'a>> {
match self {
GenericTemplatePropertyKind::Core(property) => property.try_into_integer(),
GenericTemplatePropertyKind::Self_(_) => None,
}
}

fn try_into_plain_text(self) -> Option<Box<dyn TemplateProperty<(), Output = String> + 'a>> {
fn try_into_plain_text(self) -> Option<Box<dyn TemplateProperty<Output = String> + 'a>> {
match self {
GenericTemplatePropertyKind::Core(property) => property.try_into_plain_text(),
GenericTemplatePropertyKind::Self_(_) => None,
Expand All @@ -161,7 +160,7 @@ impl<'a, C: 'a> IntoTemplateProperty<'a, ()> for GenericTemplatePropertyKind<'a,
/// global resources, the keyword function is allowed to capture resources.
pub type GenericTemplateBuildKeywordFn<'a, C> = Box<
dyn Fn(
Box<dyn TemplateProperty<(), Output = C> + 'a>,
Box<dyn TemplateProperty<Output = C> + 'a>,
) -> TemplateParseResult<GenericTemplatePropertyKind<'a, C>>
+ 'a,
>;
Expand Down
19 changes: 9 additions & 10 deletions cli/src/operation_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ impl OperationTemplateLanguage {
}

impl TemplateLanguage<'static> for OperationTemplateLanguage {
type Context = ();
type Property = OperationTemplatePropertyKind;

template_builder::impl_core_wrap_property_fns!('static, OperationTemplatePropertyKind::Core);
Expand Down Expand Up @@ -117,41 +116,41 @@ impl OperationTemplateLanguage {
}

pub fn wrap_operation(
property: impl TemplateProperty<(), Output = Operation> + 'static,
property: impl TemplateProperty<Output = Operation> + 'static,
) -> OperationTemplatePropertyKind {
OperationTemplatePropertyKind::Operation(Box::new(property))
}

pub fn wrap_operation_id(
property: impl TemplateProperty<(), Output = OperationId> + 'static,
property: impl TemplateProperty<Output = OperationId> + 'static,
) -> OperationTemplatePropertyKind {
OperationTemplatePropertyKind::OperationId(Box::new(property))
}
}

pub enum OperationTemplatePropertyKind {
Core(CoreTemplatePropertyKind<'static, ()>),
Operation(Box<dyn TemplateProperty<(), Output = Operation>>),
OperationId(Box<dyn TemplateProperty<(), Output = OperationId>>),
Core(CoreTemplatePropertyKind<'static>),
Operation(Box<dyn TemplateProperty<Output = Operation>>),
OperationId(Box<dyn TemplateProperty<Output = OperationId>>),
}

impl IntoTemplateProperty<'static, ()> for OperationTemplatePropertyKind {
fn try_into_boolean(self) -> Option<Box<dyn TemplateProperty<(), Output = bool>>> {
impl IntoTemplateProperty<'static> for OperationTemplatePropertyKind {
fn try_into_boolean(self) -> Option<Box<dyn TemplateProperty<Output = bool>>> {
match self {
OperationTemplatePropertyKind::Core(property) => property.try_into_boolean(),
OperationTemplatePropertyKind::Operation(_) => None,
OperationTemplatePropertyKind::OperationId(_) => None,
}
}

fn try_into_integer(self) -> Option<Box<dyn TemplateProperty<(), Output = i64>>> {
fn try_into_integer(self) -> Option<Box<dyn TemplateProperty<Output = i64>>> {
match self {
OperationTemplatePropertyKind::Core(property) => property.try_into_integer(),
_ => None,
}
}

fn try_into_plain_text(self) -> Option<Box<dyn TemplateProperty<(), Output = String>>> {
fn try_into_plain_text(self) -> Option<Box<dyn TemplateProperty<Output = String>>> {
match self {
OperationTemplatePropertyKind::Core(property) => property.try_into_plain_text(),
_ => {
Expand Down
Loading

0 comments on commit cf87d36

Please sign in to comment.