Skip to content

Commit

Permalink
Rename LayerContents to LayerState, InspectExisting to InspectRestored
Browse files Browse the repository at this point in the history
  • Loading branch information
Malax committed Jun 14, 2024
1 parent 1c006c5 commit 424ce2c
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 96 deletions.
44 changes: 22 additions & 22 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, InspectExistingAction, IntoAction, InvalidMetadataAction, LayerRef,
CachedLayerDefinition, InspectRestoredAction, IntoAction, InvalidMetadataAction, LayerRef,
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, InspectExistingAction, InvalidMetadataAction, LayerContents,
/// # CachedLayerDefinition, InspectRestoredAction, InvalidMetadataAction, LayerState,
/// # };
/// # use libcnb::layer_env::{LayerEnv, ModificationBehavior, Scope};
/// # use libcnb::Buildpack;
Expand Down Expand Up @@ -175,7 +175,7 @@ impl<B: Buildpack + ?Sized> BuildContext<B> {
/// // 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_existing: &|_: &GenericMetadata, _| InspectExistingAction::KeepLayer,
/// inspect_restored: &|_: &GenericMetadata, _| InspectRestoredAction::KeepLayer,
/// },
/// )?;
///
Expand All @@ -187,8 +187,8 @@ impl<B: Buildpack + ?Sized> BuildContext<B> {
/// // In the majority of cases, we don't need more details beyond if it's empty or not and can
/// // ignore the details. This is what we do in this example. See the later example for a more
/// // complex situation.
/// match layer_ref.contents {
/// LayerContents::Empty { .. } => {
/// match layer_ref.state {
/// LayerState::Empty { .. } => {
/// println!("Creating new example layer!");
///
/// // Modify the layer contents with regular Rust functions:
Expand All @@ -206,7 +206,7 @@ impl<B: Buildpack + ?Sized> BuildContext<B> {
/// "LV-246",
/// ))?;
/// }
/// LayerContents::Cached { .. } => {
/// LayerState::Restored { .. } => {
/// println!("Reusing example layer from previous run!");
/// }
/// }
Expand All @@ -228,8 +228,8 @@ impl<B: Buildpack + ?Sized> BuildContext<B> {
/// # use libcnb::detect::{DetectContext, DetectResult};
/// # use libcnb::generic::GenericPlatform;
/// # use libcnb::layer::{
/// # CachedLayerDefinition, EmptyLayerCause, InspectExistingAction, InvalidMetadataAction,
/// # LayerContents,
/// # CachedLayerDefinition, EmptyLayerCause, InspectRestoredAction, InvalidMetadataAction,
/// # LayerState,
/// # };
/// # use libcnb::Buildpack;
/// # use libcnb_data::generic::GenericMetadata;
Expand Down Expand Up @@ -272,10 +272,10 @@ impl<B: Buildpack + ?Sized> BuildContext<B> {
/// build: false,
/// launch: false,
/// invalid_metadata: &|_| InvalidMetadataAction::DeleteLayer,
/// inspect_existing: &|metadata: &ExampleLayerMetadata, layer_dir| {
/// inspect_restored: &|metadata: &ExampleLayerMetadata, layer_dir| {
/// if metadata.lang_runtime_version.starts_with("0.") {
/// Ok((
/// InspectExistingAction::DeleteLayer,
/// InspectRestoredAction::DeleteLayer,
/// CustomCause::LegacyVersion,
/// ))
/// } else {
Expand All @@ -291,15 +291,15 @@ impl<B: Buildpack + ?Sized> BuildContext<B> {
///
/// if file_contents == "known-broken-0.1c" {
/// Ok((
/// InspectExistingAction::DeleteLayer,
/// InspectRestoredAction::DeleteLayer,
/// CustomCause::HasBrokenModule,
/// ))
/// } else {
/// Ok((InspectExistingAction::KeepLayer, CustomCause::Ok))
/// Ok((InspectRestoredAction::KeepLayer, CustomCause::Ok))
/// }
/// } else {
/// Ok((
/// InspectExistingAction::DeleteLayer,
/// InspectRestoredAction::DeleteLayer,
/// CustomCause::MissingModulesFile,
/// ))
/// }
Expand All @@ -308,8 +308,8 @@ impl<B: Buildpack + ?Sized> BuildContext<B> {
/// },
/// )?;
///
/// match layer_ref.contents {
/// LayerContents::Empty { ref cause } => {
/// match layer_ref.state {
/// LayerState::Empty { ref cause } => {
/// // Since the cause is just a regular Rust value, we can match it with regular
/// // Rust syntax and be as complex or simple as we need.
/// let message = match cause {
Expand All @@ -330,7 +330,7 @@ impl<B: Buildpack + ?Sized> BuildContext<B> {
/// lang_runtime_version: String::from("1.0.0"),
/// })?;
/// }
/// LayerContents::Cached { .. } => {
/// LayerState::Restored { .. } => {
/// println!("Re-using cached language runtime");
/// }
/// }
Expand All @@ -345,15 +345,15 @@ impl<B: Buildpack + ?Sized> BuildContext<B> {
/// # }
/// # }
/// ```
pub fn cached_layer<'a, M, MA, EA, MAC, EAC>(
pub fn cached_layer<'a, M, MA, IA, MAC, IAC>(
&self,
layer_name: impl Borrow<LayerName>,
layer_definition: impl Borrow<CachedLayerDefinition<'a, M, MA, EA>>,
) -> crate::Result<LayerRef<B, MAC, EAC>, B::Error>
layer_definition: impl Borrow<CachedLayerDefinition<'a, M, MA, IA>>,
) -> crate::Result<LayerRef<B, MAC, IAC>, B::Error>
where
M: 'a + Serialize + DeserializeOwned,
MA: 'a + IntoAction<InvalidMetadataAction<M>, MAC, B::Error>,
EA: 'a + IntoAction<InspectExistingAction, EAC, B::Error>,
IA: 'a + IntoAction<InspectRestoredAction, IAC, B::Error>,
{
let layer_definition = layer_definition.borrow();

Expand All @@ -364,7 +364,7 @@ impl<B: Buildpack + ?Sized> BuildContext<B> {
cache: true,
},
layer_definition.invalid_metadata,
layer_definition.inspect_existing,
layer_definition.inspect_restored,
layer_name.borrow(),
&self.layers_dir,
)
Expand All @@ -390,7 +390,7 @@ impl<B: Buildpack + ?Sized> BuildContext<B> {
cache: false,
},
&|_| InvalidMetadataAction::DeleteLayer,
&|_: &GenericMetadata, _| InspectExistingAction::DeleteLayer,
&|_: &GenericMetadata, _| InspectRestoredAction::DeleteLayer,
layer_name.borrow(),
&self.layers_dir,
)
Expand Down
2 changes: 1 addition & 1 deletion libcnb/src/layer/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub(in crate::layer) fn delete_layer<P: AsRef<Path>>(

#[derive(thiserror::Error, Debug)]
pub enum DeleteLayerError {
#[error("I/O error while deleting existing layer: {0}")]
#[error("I/O error while deleting layer: {0}")]
IoError(#[from] std::io::Error),
}

Expand Down
72 changes: 32 additions & 40 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, InspectExistingAction, IntoAction, InvalidMetadataAction, LayerContents,
LayerError, LayerRef,
EmptyLayerCause, InspectRestoredAction, IntoAction, InvalidMetadataAction, LayerError,
LayerRef, LayerState,
};
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, EA, MAC, EAC>(
pub(crate) fn handle_layer<B, M, MA, IA, MAC, IAC>(
layer_types: LayerTypes,
invalid_metadata: &dyn Fn(&GenericMetadata) -> MA,
inspect_existing: &dyn Fn(&M, &Path) -> EA,
inspect_restored: &dyn Fn(&M, &Path) -> IA,
layer_name: &LayerName,
layers_dir: &Path,
) -> crate::Result<LayerRef<B, MAC, EAC>, B::Error>
) -> crate::Result<LayerRef<B, MAC, IAC>, B::Error>
where
B: Buildpack + ?Sized,
M: Serialize + DeserializeOwned,
MA: IntoAction<InvalidMetadataAction<M>, MAC, B::Error>,
EA: IntoAction<InspectExistingAction, EAC, B::Error>,
IA: IntoAction<InspectRestoredAction, IAC, B::Error>,
{
match read_layer::<M, _>(layers_dir, layer_name) {
Ok(None) => create_layer(
Expand All @@ -37,12 +37,12 @@ where
EmptyLayerCause::Uncached,
),
Ok(Some(layer_data)) => {
let inspect_action = inspect_existing(&layer_data.metadata.metadata, &layer_data.path)
let inspect_action = inspect_restored(&layer_data.metadata.metadata, &layer_data.path)
.into_action()
.map_err(crate::Error::BuildpackError)?;

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

create_layer(
Expand All @@ -52,7 +52,7 @@ where
EmptyLayerCause::Inspect { cause },
)
}
(InspectExistingAction::KeepLayer, cause) => {
(InspectRestoredAction::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 @@ -64,7 +64,7 @@ where
name: layer_data.name,
layers_dir: PathBuf::from(layers_dir),
buildpack: PhantomData,
contents: LayerContents::Cached { cause },
state: LayerState::Restored { cause },
})
}
}
Expand Down Expand Up @@ -98,7 +98,7 @@ where
handle_layer(
layer_types,
invalid_metadata,
inspect_existing,
inspect_restored,
layer_name,
layers_dir,
)
Expand All @@ -109,12 +109,12 @@ where
}
}

fn create_layer<B, MAC, EAC>(
fn create_layer<B, MAC, IAC>(
layer_types: LayerTypes,
layer_name: &LayerName,
layers_dir: &Path,
empty_layer_cause: EmptyLayerCause<MAC, EAC>,
) -> Result<LayerRef<B, MAC, EAC>, crate::Error<B::Error>>
empty_layer_cause: EmptyLayerCause<MAC, IAC>,
) -> Result<LayerRef<B, MAC, IAC>, crate::Error<B::Error>>
where
B: Buildpack + ?Sized,
{
Expand All @@ -136,7 +136,7 @@ where
name: layer_data.name,
layers_dir: PathBuf::from(layers_dir),
buildpack: PhantomData,
contents: LayerContents::Empty {
state: LayerState::Empty {
cause: empty_layer_cause,
},
})
Expand All @@ -148,9 +148,7 @@ mod tests {
use crate::build::{BuildContext, BuildResult};
use crate::detect::{DetectContext, DetectResult};
use crate::generic::{GenericError, GenericPlatform};
use crate::layer::{
EmptyLayerCause, InspectExistingAction, InvalidMetadataAction, LayerContents,
};
use crate::layer::{EmptyLayerCause, InspectRestoredAction, InvalidMetadataAction, LayerState};
use crate::Buildpack;
use libcnb_common::toml_file::read_toml_file;
use libcnb_data::generic::GenericMetadata;
Expand Down Expand Up @@ -179,7 +177,7 @@ mod tests {
.unwrap();

assert_eq!(layer_ref.layers_dir, temp_dir.path());
assert_eq!(layer_ref.contents, LayerContents::Empty { cause });
assert_eq!(layer_ref.state, LayerState::Empty { cause });
assert!(temp_dir.path().join(&*layer_name).is_dir());
assert_eq!(
read_toml_file::<LayerContentMetadata<GenericMetadata>>(
Expand All @@ -206,7 +204,7 @@ mod tests {
TestBuildpack,
GenericMetadata,
InvalidMetadataAction<GenericMetadata>,
InspectExistingAction,
InspectRestoredAction,
(),
(),
>(
Expand All @@ -216,7 +214,7 @@ mod tests {
cache: true,
},
&|_| panic!("invalid_metadata callback should not be called!"),
&|_, _| panic!("inspect_existing callback should not be called!"),
&|_, _| panic!("inspect_restored callback should not be called!"),
&layer_name,
temp_dir.path(),
)
Expand All @@ -239,8 +237,8 @@ mod tests {
}
);
assert_eq!(
layer_ref.contents,
LayerContents::Empty {
layer_ref.state,
LayerState::Empty {
cause: EmptyLayerCause::Uncached
}
);
Expand Down Expand Up @@ -273,7 +271,7 @@ mod tests {
&|metadata, path| {
assert_eq!(metadata, &Some(toml! { answer = 42 }));
assert_eq!(path, temp_dir.path().join(&*layer_name.clone()));
(InspectExistingAction::KeepLayer, KEEP_CAUSE)
(InspectRestoredAction::KeepLayer, KEEP_CAUSE)
},
&layer_name,
temp_dir.path(),
Expand All @@ -296,10 +294,7 @@ mod tests {
metadata: Some(toml! { answer = 42 })
}
);
assert_eq!(
layer_ref.contents,
LayerContents::Cached { cause: KEEP_CAUSE }
);
assert_eq!(layer_ref.state, LayerState::Restored { cause: KEEP_CAUSE });
}

#[test]
Expand Down Expand Up @@ -329,7 +324,7 @@ mod tests {
&|metadata, path| {
assert_eq!(metadata, &Some(toml! { answer = 42 }));
assert_eq!(path, temp_dir.path().join(&*layer_name.clone()));
(InspectExistingAction::DeleteLayer, DELETE_CAUSE)
(InspectRestoredAction::DeleteLayer, DELETE_CAUSE)
},
&layer_name,
temp_dir.path(),
Expand All @@ -353,8 +348,8 @@ mod tests {
}
);
assert_eq!(
layer_ref.contents,
LayerContents::Empty {
layer_ref.state,
LayerState::Empty {
cause: EmptyLayerCause::Inspect {
cause: DELETE_CAUSE
}
Expand Down Expand Up @@ -387,7 +382,7 @@ mod tests {
TestBuildpack,
TestLayerMetadata,
_,
(InspectExistingAction, &str),
(InspectRestoredAction, &str),
&str,
_,
>(
Expand All @@ -400,7 +395,7 @@ mod tests {
assert_eq!(metadata, &Some(toml! { answer = 42 }));
(InvalidMetadataAction::DeleteLayer, DELETE_CAUSE)
},
&|_, _| panic!("inspect_existing callback should not be called!"),
&|_, _| panic!("inspect_restored callback should not be called!"),
&layer_name,
temp_dir.path(),
)
Expand All @@ -423,8 +418,8 @@ mod tests {
}
);
assert_eq!(
layer_ref.contents,
LayerContents::Empty {
layer_ref.state,
LayerState::Empty {
cause: EmptyLayerCause::MetadataInvalid {
cause: DELETE_CAUSE
}
Expand Down Expand Up @@ -479,7 +474,7 @@ mod tests {
}
);

(InspectExistingAction::KeepLayer, KEEP_CAUSE)
(InspectRestoredAction::KeepLayer, KEEP_CAUSE)
},
&layer_name,
temp_dir.path(),
Expand All @@ -505,10 +500,7 @@ mod tests {
}
);

assert_eq!(
layer_ref.contents,
LayerContents::Cached { cause: KEEP_CAUSE }
);
assert_eq!(layer_ref.state, LayerState::Restored { cause: KEEP_CAUSE });
}

struct TestBuildpack;
Expand Down
Loading

0 comments on commit 424ce2c

Please sign in to comment.