Skip to content

Commit

Permalink
context: add provide_if methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Nouzan committed Oct 30, 2023
1 parent 6b6c6ce commit 05632bd
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
32 changes: 32 additions & 0 deletions crates/indicator/src/context/layer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,18 @@ where
self.with(AddData::with_data(data))
}

/// Optionally provide data to the context.
///
/// If the data is `None`, the layer will be skipped,
/// so will not panic if the data is not in the context.
fn provide_if<D>(self, data: Option<D>) -> Stack<Self, OptionalLayer<AddData<D>>>
where
D: Clone + Send + Sync + 'static,
Self: Sized,
{
self.with_if(data.map(AddData::with_data))
}

/// Provide data to the context with the given data provider.
fn provide_with<D>(
self,
Expand All @@ -218,6 +230,26 @@ where
self.with(AddData::new(provider))
}

/// Optionally provide data to the context with the given data provider.
///
/// If not enabled, the layer will be skipped,
/// so will not panic if the data is not in the context.
fn provide_with_if<D>(
self,
enable: bool,
provider: impl Fn() -> Option<D> + Send + Sync + 'static,
) -> Stack<Self, OptionalLayer<AddData<D>>>
where
D: Send + Sync + 'static,
Self: Sized,
{
self.with_if(if enable {
Some(AddData::new(provider))
} else {
None
})
}

/// Declare that the data of type `D` is in the context.
fn from_context<D>(self) -> Stack<Self, AddData<D>>

Check warning on line 254 in crates/indicator/src/context/layer/mod.rs

View workflow job for this annotation

GitHub Actions / Test on Rust nightly

methods called `from_*` usually take no `self`

Check warning on line 254 in crates/indicator/src/context/layer/mod.rs

View workflow job for this annotation

GitHub Actions / Test on Rust stable

methods called `from_*` usually take no `self`

Check warning on line 254 in crates/indicator/src/context/layer/mod.rs

View workflow job for this annotation

GitHub Actions / Test on Rust beta

methods called `from_*` usually take no `self`

Check warning on line 254 in crates/indicator/src/context/layer/mod.rs

View workflow job for this annotation

GitHub Actions / Test on Rust 1.67.0

methods called `from_*` usually take no `self`
where
Expand Down
32 changes: 32 additions & 0 deletions crates/indicator/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,18 @@ pub trait ContextOperatorExt<In>: ContextOperator<In> {
self.with(AddData::with_data(data))
}

/// Optionally provide data to the context.
///
/// If data is `None`, the layer will be skipped,
/// so it won't panic if the data is not in the context.
fn provide_if<D>(self, data: Option<D>) -> Either<AddDataOperator<D, Self>, Self>
where
D: Clone + Send + Sync + 'static,
Self: Sized,
{
self.with_if(data.map(AddData::with_data))
}

/// Provide data to the context with the given data provider.
fn provide_with<D>(
self,
Expand All @@ -249,6 +261,26 @@ pub trait ContextOperatorExt<In>: ContextOperator<In> {
self.with(AddData::new(provider))
}

/// Optionally provide data to the context with the given data provider.
///
/// If not enabled, the layer will be skipped,
/// so it will not panic if the data is not in the context.
fn provide_with_if<D>(
self,
enable: bool,
provider: impl Fn() -> Option<D> + Send + Sync + 'static,
) -> Either<AddDataOperator<D, Self>, Self>
where
D: Send + Sync + 'static,
Self: Sized,
{
self.with_if(if enable {
Some(AddData::new(provider))
} else {
None
})
}

/// Declare that the data of type `D` is in the context.
fn from_context<D>(self) -> AddDataOperator<D, Self>

Check warning on line 285 in crates/indicator/src/context/mod.rs

View workflow job for this annotation

GitHub Actions / Test on Rust nightly

methods called `from_*` usually take no `self`

Check warning on line 285 in crates/indicator/src/context/mod.rs

View workflow job for this annotation

GitHub Actions / Test on Rust stable

methods called `from_*` usually take no `self`

Check warning on line 285 in crates/indicator/src/context/mod.rs

View workflow job for this annotation

GitHub Actions / Test on Rust beta

methods called `from_*` usually take no `self`

Check warning on line 285 in crates/indicator/src/context/mod.rs

View workflow job for this annotation

GitHub Actions / Test on Rust 1.67.0

methods called `from_*` usually take no `self`
where
Expand Down

0 comments on commit 05632bd

Please sign in to comment.