Skip to content

Commit

Permalink
feat: impl ContextInitializer for Option<impl ContextInitializer>
Browse files Browse the repository at this point in the history
  • Loading branch information
CertainLach committed May 27, 2024
1 parent 476a44c commit e3f600e
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions crates/jrsonnet-evaluator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,29 @@ impl ContextInitializer for () {
}
}

impl<T> ContextInitializer for Option<T>
where
T: ContextInitializer,
{
fn initialize(&self, state: State, for_file: Source) -> Context {
if let Some(ctx) = self {
ctx.initialize(state, for_file)
} else {
().initialize(state, for_file)
}
}

fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {
if let Some(ctx) = self {
ctx.populate(for_file, builder);
}
}

fn as_any(&self) -> &dyn Any {
self
}
}

macro_rules! impl_context_initializer {
($($gen:ident)*) => {
#[allow(non_snake_case)]
Expand Down

0 comments on commit e3f600e

Please sign in to comment.