Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change EmptyLayerCause::{InvalidMetadataAction,RestoredLayerAction} to be tuple enum variant #849

Open
edmorley opened this issue Aug 2, 2024 · 0 comments
Labels

Comments

@edmorley
Copy link
Member

edmorley commented Aug 2, 2024

Currently EmptyLayerCause::InvalidMetadataAction and EmptyLayerCause::RestoredLayerAction are defined as struct enum variants with a single cause field:

pub enum EmptyLayerCause<MAC, RAC> {
/// The layer wasn't cached in a previous buildpack run and was newly created.
NewlyCreated,
/// The layer was cached in a previous buildpack run, but the metadata was invalid and couldn't
/// be converted into a valid form. Subsequently, the layer was deleted entirely.
///
/// See: `invalid_metadata_action` in [`CachedLayerDefinition`].
InvalidMetadataAction { cause: MAC },
/// The layer was cached in a previous buildpack run, but the `restored_layer_action` function
/// rejected the contents and/or metadata.
///
/// See: `restored_layer_action` in [`CachedLayerDefinition`].
RestoredLayerAction { cause: RAC },
}

This cause field is derived from the return value from the invalid_metadata_action and restored_layer_action functions.

However, I don't think it's ideal to name it cause, since:

  1. The embedded result/payload returned from invalid_metadata_action and restored_layer_action can be anything - it doesn't have to be related to the "cause" at all. eg: A layer could be implemented to always return RestoredLayerAction::DeleteLayer no matter what, but include some piece of metadata from the old layer (eg the runtime version from the last successful build) for use outside of restored_layer_action. In which cause calling it "cause" is not accurate.
  2. Even in cases where the returned value from restored_layer_action is technically a "cause", the named struct field of cause isn't accurate when there are multiple reasons for invalidation. For example, I have to rename the field to the plural reasons, which is a bit clunky: EmptyLayerCause::RestoredLayerAction { cause: reasons } (see here).

If these enum variants instead used a tuple type type, the naming of the variable is left up to the author:

EmptyLayerCause::RestoredLayerAction(reasons)

If we do this, there are a few other places where we'd want to replace "cause" references too:

pub enum LayerState<MAC, RAC> {
/// The layer contains validated cached contents from a previous buildpack run.
///
/// See: `restored_layer_action` in [`CachedLayerDefinition`].
Restored { cause: RAC },
/// The layer is empty. Inspect the contained [`EmptyLayerCause`] for the cause.
Empty { cause: EmptyLayerCause<MAC, RAC> },
}
/// The cause of a layer being empty.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum EmptyLayerCause<MAC, RAC> {
/// The layer wasn't cached in a previous buildpack run and was newly created.
NewlyCreated,
/// The layer was cached in a previous buildpack run, but the metadata was invalid and couldn't
/// be converted into a valid form. Subsequently, the layer was deleted entirely.
///
/// See: `invalid_metadata_action` in [`CachedLayerDefinition`].
InvalidMetadataAction { cause: MAC },
/// The layer was cached in a previous buildpack run, but the `restored_layer_action` function
/// rejected the contents and/or metadata.
///
/// See: `restored_layer_action` in [`CachedLayerDefinition`].
RestoredLayerAction { cause: RAC },
}
/// A value-to-value conversion for layer actions.
///
/// Similar to [`Into`], but specialized. Allowing it to also be implemented for
/// values in the standard library such as [`Result`].
///
/// Implement this trait if you want to use your own types as actions.
///
/// libcnb ships with generic implementations for the majority of the use-cases:
/// - Using [`RestoredLayerAction`] or [`InvalidMetadataAction`] directly.
/// - Using [`RestoredLayerAction`] or [`InvalidMetadataAction`] directly, wrapped in a Result.
/// - Using [`RestoredLayerAction`] or [`InvalidMetadataAction`] with a cause value in a tuple.
/// - Using [`RestoredLayerAction`] or [`InvalidMetadataAction`] with a cause value in a tuple, wrapped in a Result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant