Skip to content

Commit

Permalink
More BON
Browse files Browse the repository at this point in the history
We could take the builder idea to an extreme and allow for BYO functions provided they have the same return type. These would basically be equivalent:

```rust
    // Fully spelled out
    let _ = cache_layer_builder()
        .on_invalid_metadata(&invalid_metadata_action)
        .on_restored_layer(&|old, _| restored_layer_action(old, metadata))
        .layer_name(layer_name!("ruby"))
        .metadata(metadata)
        .context(context)
        .launch(true)
        .build(true)
        .call()?;

    // With defaults
    let layer_ref = cache_layer_builder()
        .layer_name(layer_name!("ruby"))
        .metadata(metadata)
        .context(context)
        .call()?;
```

If we went this route, we could rename it to something more generic like simply `cache_layer` or maybe `cache_layer_builder`. The module name could be significant, maybe something like `diff_migrate_cache::layer_builder. Naming is hard.
  • Loading branch information
schneems committed Dec 19, 2024
1 parent d35dd15 commit ed8bde3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
18 changes: 15 additions & 3 deletions buildpacks/ruby/src/layers/ruby_install_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use bullet_stream::state::SubBullet;
use bullet_stream::Print;
use cache_diff::CacheDiff;
use commons::gemfile_lock::ResolvedRubyVersion;
use commons::layer::cache_buddy::{diff_migrate_cached_layer, CacheBuddy};
use commons::layer::cache_buddy::{
cache_layer_builder, invalid_metadata_action, restored_layer_action, CacheBuddy,
};
use flate2::read::GzDecoder;
use libcnb::data::layer_name;
use libcnb::layer::{EmptyLayerCause, LayerState};
Expand All @@ -39,9 +41,19 @@ pub(crate) fn handle(
mut bullet: Print<SubBullet<Stdout>>,
metadata: &Metadata,
) -> libcnb::Result<(Print<SubBullet<Stdout>>, LayerEnv), RubyBuildpackError> {
let layer_ref = diff_migrate_cached_layer()
.build(true)
// Fully spelled out
let _ = cache_layer_builder()
.on_invalid_metadata(&invalid_metadata_action)
.on_restored_layer(&|old, _| restored_layer_action(old, metadata))
.layer_name(layer_name!("ruby"))
.metadata(metadata)
.context(context)
.launch(true)
.build(true)
.call()?;

// With defaults
let layer_ref = cache_layer_builder()
.layer_name(layer_name!("ruby"))
.metadata(metadata)
.context(context)
Expand Down
11 changes: 8 additions & 3 deletions commons/src/layer/cache_buddy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,15 @@ pub struct CacheBuddy {
}

#[bon::builder]
pub fn diff_migrate_cached_layer<B, M>(
pub fn cache_layer_builder<'a, B, M>(
build: Option<bool>,
launch: Option<bool>,
layer_name: LayerName,
context: &BuildContext<B>,
on_invalid_metadata: Option<
&'a dyn Fn(&libcnb::generic::GenericMetadata) -> (InvalidMetadataAction<M>, Meta<M>),
>,
on_restored_layer: Option<&'a dyn Fn(&M, &std::path::Path) -> (RestoredLayerAction, Meta<M>)>,
metadata: &M,
) -> libcnb::Result<LayerRef<B, Meta<M>, Meta<M>>, B::Error>
where
Expand All @@ -90,8 +94,9 @@ where
CachedLayerDefinition {
build: build.unwrap_or(true),
launch: launch.unwrap_or(true),
invalid_metadata_action: &invalid_metadata_action,
restored_layer_action: &|old: &M, _| restored_layer_action(old, metadata),
invalid_metadata_action: on_invalid_metadata.unwrap_or(&invalid_metadata_action),
restored_layer_action: on_restored_layer
.unwrap_or(&|old: &M, _| restored_layer_action(old, metadata)),
},
)?;
layer_ref.write_metadata(metadata)?;
Expand Down

0 comments on commit ed8bde3

Please sign in to comment.