Skip to content

Commit

Permalink
Rename CachedLayerDefinition callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Malax committed Jun 18, 2024
1 parent 06d4668 commit bfcb0ce
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 69 deletions.
36 changes: 18 additions & 18 deletions libcnb/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::data::{
};
use crate::layer::trait_api::handling::LayerErrorOrBuildpackError;
use crate::layer::{
CachedLayerDefinition, InspectRestoredAction, IntoAction, InvalidMetadataAction, LayerRef,
CachedLayerDefinition, IntoAction, InvalidMetadataAction, LayerRef, RestoredLayerAction,
UncachedLayerDefinition,
};
use crate::sbom::Sbom;
Expand Down Expand Up @@ -137,7 +137,7 @@ impl<B: Buildpack + ?Sized> BuildContext<B> {
/// # use libcnb::detect::{DetectContext, DetectResult};
/// # use libcnb::generic::GenericPlatform;
/// # use libcnb::layer::{
/// # CachedLayerDefinition, InspectRestoredAction, InvalidMetadataAction, LayerState,
/// # CachedLayerDefinition, RestoredLayerAction, InvalidMetadataAction, LayerState,
/// # };
/// # use libcnb::layer_env::{LayerEnv, ModificationBehavior, Scope};
/// # use libcnb::Buildpack;
Expand Down Expand Up @@ -170,12 +170,12 @@ impl<B: Buildpack + ?Sized> BuildContext<B> {
/// // Will be called if a cached version of the layer was found, but the metadata
/// // could not be parsed. In this example, we instruct libcnb to always delete the
/// // existing layer in such a case. But we can implement any logic here if we want.
/// invalid_metadata: &|_| InvalidMetadataAction::DeleteLayer,
/// invalid_metadata_action: &|_| InvalidMetadataAction::DeleteLayer,
/// // Will be called if a cached version of the layer was found. This allows us to
/// // inspect the contents and metadata to decide if we want to keep the existing
/// // layer or let libcnb delete the existing layer and create a new one for us.
/// // This is libcnb's method to implement cache invalidations for layers.
/// inspect_restored: &|_: &GenericMetadata, _| InspectRestoredAction::KeepLayer,
/// restored_layer_action: &|_: &GenericMetadata, _| RestoredLayerAction::KeepLayer,
/// },
/// )?;
///
Expand Down Expand Up @@ -228,7 +228,7 @@ impl<B: Buildpack + ?Sized> BuildContext<B> {
/// # use libcnb::detect::{DetectContext, DetectResult};
/// # use libcnb::generic::GenericPlatform;
/// # use libcnb::layer::{
/// # CachedLayerDefinition, EmptyLayerCause, InspectRestoredAction, InvalidMetadataAction,
/// # CachedLayerDefinition, EmptyLayerCause, RestoredLayerAction, InvalidMetadataAction,
/// # LayerState,
/// # };
/// # use libcnb::Buildpack;
Expand Down Expand Up @@ -271,11 +271,11 @@ impl<B: Buildpack + ?Sized> BuildContext<B> {
/// CachedLayerDefinition {
/// build: false,
/// launch: false,
/// invalid_metadata: &|_| InvalidMetadataAction::DeleteLayer,
/// inspect_restored: &|metadata: &ExampleLayerMetadata, layer_dir| {
/// invalid_metadata_action: &|_| InvalidMetadataAction::DeleteLayer,
/// restored_layer_action: &|metadata: &ExampleLayerMetadata, layer_dir| {
/// if metadata.lang_runtime_version.starts_with("0.") {
/// Ok((
/// InspectRestoredAction::DeleteLayer,
/// RestoredLayerAction::DeleteLayer,
/// CustomCause::LegacyVersion,
/// ))
/// } else {
Expand All @@ -291,15 +291,15 @@ impl<B: Buildpack + ?Sized> BuildContext<B> {
///
/// if file_contents == "known-broken-0.1c" {
/// Ok((
/// InspectRestoredAction::DeleteLayer,
/// RestoredLayerAction::DeleteLayer,
/// CustomCause::HasBrokenModule,
/// ))
/// } else {
/// Ok((InspectRestoredAction::KeepLayer, CustomCause::Ok))
/// Ok((RestoredLayerAction::KeepLayer, CustomCause::Ok))
/// }
/// } else {
/// Ok((
/// InspectRestoredAction::DeleteLayer,
/// RestoredLayerAction::DeleteLayer,
/// CustomCause::MissingModulesFile,
/// ))
/// }
Expand Down Expand Up @@ -345,15 +345,15 @@ impl<B: Buildpack + ?Sized> BuildContext<B> {
/// # }
/// # }
/// ```
pub fn cached_layer<'a, M, MA, IA, MAC, IAC>(
pub fn cached_layer<'a, M, MA, RA, MAC, RAC>(
&self,
layer_name: impl Borrow<LayerName>,
layer_definition: impl Borrow<CachedLayerDefinition<'a, M, MA, IA>>,
) -> crate::Result<LayerRef<B, MAC, IAC>, B::Error>
layer_definition: impl Borrow<CachedLayerDefinition<'a, M, MA, RA>>,
) -> crate::Result<LayerRef<B, MAC, RAC>, B::Error>
where
M: 'a + Serialize + DeserializeOwned,
MA: 'a + IntoAction<InvalidMetadataAction<M>, MAC, B::Error>,
IA: 'a + IntoAction<InspectRestoredAction, IAC, B::Error>,
RA: 'a + IntoAction<RestoredLayerAction, RAC, B::Error>,
{
let layer_definition = layer_definition.borrow();

Expand All @@ -363,8 +363,8 @@ impl<B: Buildpack + ?Sized> BuildContext<B> {
build: layer_definition.build,
cache: true,
},
layer_definition.invalid_metadata,
layer_definition.inspect_restored,
layer_definition.invalid_metadata_action,
layer_definition.restored_layer_action,
layer_name.borrow(),
&self.layers_dir,
)
Expand All @@ -390,7 +390,7 @@ impl<B: Buildpack + ?Sized> BuildContext<B> {
cache: false,
},
&|_| InvalidMetadataAction::DeleteLayer,
&|_: &GenericMetadata, _| InspectRestoredAction::DeleteLayer,
&|_: &GenericMetadata, _| RestoredLayerAction::DeleteLayer,
layer_name.borrow(),
&self.layers_dir,
)
Expand Down
64 changes: 33 additions & 31 deletions libcnb/src/layer/struct_api/handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::layer::shared::{
WriteLayerError,
};
use crate::layer::{
EmptyLayerCause, InspectRestoredAction, IntoAction, InvalidMetadataAction, LayerError,
LayerRef, LayerState,
EmptyLayerCause, IntoAction, InvalidMetadataAction, LayerError, LayerRef, LayerState,
RestoredLayerAction,
};
use crate::Buildpack;
use libcnb_common::toml_file::read_toml_file;
Expand All @@ -16,18 +16,18 @@ use serde::Serialize;
use std::marker::PhantomData;
use std::path::{Path, PathBuf};

pub(crate) fn handle_layer<B, M, MA, IA, MAC, IAC>(
pub(crate) fn handle_layer<B, M, MA, RA, MAC, RAC>(
layer_types: LayerTypes,
invalid_metadata: &dyn Fn(&GenericMetadata) -> MA,
inspect_restored: &dyn Fn(&M, &Path) -> IA,
invalid_metadata_action_fn: &dyn Fn(&GenericMetadata) -> MA,
restored_layer_action_fn: &dyn Fn(&M, &Path) -> RA,
layer_name: &LayerName,
layers_dir: &Path,
) -> crate::Result<LayerRef<B, MAC, IAC>, B::Error>
) -> crate::Result<LayerRef<B, MAC, RAC>, B::Error>
where
B: Buildpack + ?Sized,
M: Serialize + DeserializeOwned,
MA: IntoAction<InvalidMetadataAction<M>, MAC, B::Error>,
IA: IntoAction<InspectRestoredAction, IAC, B::Error>,
RA: IntoAction<RestoredLayerAction, RAC, B::Error>,
{
match read_layer::<M, _>(layers_dir, layer_name) {
Ok(None) => create_layer(
Expand All @@ -37,12 +37,13 @@ where
EmptyLayerCause::Uncached,
),
Ok(Some(layer_data)) => {
let inspect_action = inspect_restored(&layer_data.metadata.metadata, &layer_data.path)
.into_action()
.map_err(crate::Error::BuildpackError)?;
let inspect_action =
restored_layer_action_fn(&layer_data.metadata.metadata, &layer_data.path)
.into_action()
.map_err(crate::Error::BuildpackError)?;

match inspect_action {
(InspectRestoredAction::DeleteLayer, cause) => {
(RestoredLayerAction::DeleteLayer, cause) => {
delete_layer(layers_dir, layer_name).map_err(LayerError::DeleteLayerError)?;

create_layer(
Expand All @@ -52,7 +53,7 @@ where
EmptyLayerCause::Inspect { cause },
)
}
(InspectRestoredAction::KeepLayer, cause) => {
(RestoredLayerAction::KeepLayer, cause) => {
// Always write the layer types as:
// a) they might be different from what is currently on disk
// b) the cache field will be removed by CNB lifecycle on cache restore
Expand All @@ -75,9 +76,10 @@ where
)
.map_err(LayerError::CouldNotReadGenericLayerMetadata)?;

let invalid_metadata_action = invalid_metadata(&layer_content_metadata.metadata)
.into_action()
.map_err(crate::Error::BuildpackError)?;
let invalid_metadata_action =
invalid_metadata_action_fn(&layer_content_metadata.metadata)
.into_action()
.map_err(crate::Error::BuildpackError)?;

match invalid_metadata_action {
(InvalidMetadataAction::DeleteLayer, cause) => {
Expand All @@ -97,8 +99,8 @@ where

handle_layer(
layer_types,
invalid_metadata,
inspect_restored,
invalid_metadata_action_fn,
restored_layer_action_fn,
layer_name,
layers_dir,
)
Expand All @@ -109,12 +111,12 @@ where
}
}

fn create_layer<B, MAC, IAC>(
fn create_layer<B, MAC, RAC>(
layer_types: LayerTypes,
layer_name: &LayerName,
layers_dir: &Path,
empty_layer_cause: EmptyLayerCause<MAC, IAC>,
) -> Result<LayerRef<B, MAC, IAC>, crate::Error<B::Error>>
empty_layer_cause: EmptyLayerCause<MAC, RAC>,
) -> Result<LayerRef<B, MAC, RAC>, crate::Error<B::Error>>
where
B: Buildpack + ?Sized,
{
Expand Down Expand Up @@ -148,7 +150,7 @@ mod tests {
use crate::build::{BuildContext, BuildResult};
use crate::detect::{DetectContext, DetectResult};
use crate::generic::{GenericError, GenericPlatform};
use crate::layer::{EmptyLayerCause, InspectRestoredAction, InvalidMetadataAction, LayerState};
use crate::layer::{EmptyLayerCause, InvalidMetadataAction, LayerState, RestoredLayerAction};
use crate::Buildpack;
use libcnb_common::toml_file::read_toml_file;
use libcnb_data::generic::GenericMetadata;
Expand Down Expand Up @@ -204,7 +206,7 @@ mod tests {
TestBuildpack,
GenericMetadata,
InvalidMetadataAction<GenericMetadata>,
InspectRestoredAction,
RestoredLayerAction,
(),
(),
>(
Expand All @@ -213,8 +215,8 @@ mod tests {
launch: true,
cache: true,
},
&|_| panic!("invalid_metadata callback should not be called!"),
&|_, _| panic!("inspect_restored callback should not be called!"),
&|_| panic!("invalid_metadata_action callback should not be called!"),
&|_, _| panic!("restored_layer_action callback should not be called!"),
&layer_name,
temp_dir.path(),
)
Expand Down Expand Up @@ -267,11 +269,11 @@ mod tests {
launch: true,
cache: true,
},
&|_| panic!("invalid_metadata callback should not be called!"),
&|_| panic!("invalid_metadata_action callback should not be called!"),
&|metadata, path| {
assert_eq!(metadata, &Some(toml! { answer = 42 }));
assert_eq!(path, temp_dir.path().join(&*layer_name.clone()));
(InspectRestoredAction::KeepLayer, KEEP_CAUSE)
(RestoredLayerAction::KeepLayer, KEEP_CAUSE)
},
&layer_name,
temp_dir.path(),
Expand Down Expand Up @@ -320,11 +322,11 @@ mod tests {
launch: true,
cache: true,
},
&|_| panic!("invalid_metadata callback should not be called!"),
&|_| panic!("invalid_metadata_action callback should not be called!"),
&|metadata, path| {
assert_eq!(metadata, &Some(toml! { answer = 42 }));
assert_eq!(path, temp_dir.path().join(&*layer_name.clone()));
(InspectRestoredAction::DeleteLayer, DELETE_CAUSE)
(RestoredLayerAction::DeleteLayer, DELETE_CAUSE)
},
&layer_name,
temp_dir.path(),
Expand Down Expand Up @@ -382,7 +384,7 @@ mod tests {
TestBuildpack,
TestLayerMetadata,
_,
(InspectRestoredAction, &str),
(RestoredLayerAction, &str),
&str,
_,
>(
Expand All @@ -395,7 +397,7 @@ mod tests {
assert_eq!(metadata, &Some(toml! { answer = 42 }));
(InvalidMetadataAction::DeleteLayer, DELETE_CAUSE)
},
&|_, _| panic!("inspect_restored callback should not be called!"),
&|_, _| panic!("restored_layer_action callback should not be called!"),
&layer_name,
temp_dir.path(),
)
Expand Down Expand Up @@ -474,7 +476,7 @@ mod tests {
}
);

(InspectRestoredAction::KeepLayer, KEEP_CAUSE)
(RestoredLayerAction::KeepLayer, KEEP_CAUSE)
},
&layer_name,
temp_dir.path(),
Expand Down
Loading

0 comments on commit bfcb0ce

Please sign in to comment.