#![allow(deprecated)]
use crate::Buildpack;
use crate::build::BuildContext;
use crate::data::layer::LayerName;
use crate::data::layer_content_metadata::{LayerContentMetadata, LayerTypes};
use crate::generic::GenericMetadata;
use crate::layer_env::LayerEnv;
use crate::sbom::Sbom;
use serde::Serialize;
use serde::de::DeserializeOwned;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
pub(crate) mod handling;
#[cfg(test)]
mod tests;
#[allow(unused_variables)]
#[deprecated = "The Layer trait API was replaced by a struct based API. Use CachedLayerDefinition or UncachedLayerDefinition."]
pub trait Layer {
type Buildpack: Buildpack;
type Metadata: DeserializeOwned + Serialize + Clone;
fn types(&self) -> LayerTypes;
fn create(
&mut self,
context: &BuildContext<Self::Buildpack>,
layer_path: &Path,
) -> Result<LayerResult<Self::Metadata>, <Self::Buildpack as Buildpack>::Error>;
fn existing_layer_strategy(
&mut self,
context: &BuildContext<Self::Buildpack>,
layer_data: &LayerData<Self::Metadata>,
) -> Result<ExistingLayerStrategy, <Self::Buildpack as Buildpack>::Error> {
Ok(ExistingLayerStrategy::Recreate)
}
fn update(
&mut self,
context: &BuildContext<Self::Buildpack>,
layer_data: &LayerData<Self::Metadata>,
) -> Result<LayerResult<Self::Metadata>, <Self::Buildpack as Buildpack>::Error> {
LayerResultBuilder::new(layer_data.content_metadata.metadata.clone())
.env(layer_data.env.clone())
.build()
}
fn migrate_incompatible_metadata(
&mut self,
context: &BuildContext<Self::Buildpack>,
metadata: &GenericMetadata,
) -> Result<MetadataMigration<Self::Metadata>, <Self::Buildpack as Buildpack>::Error> {
Ok(MetadataMigration::RecreateLayer)
}
}
#[derive(Eq, PartialEq, Clone, Copy, Debug)]
#[deprecated = "Part of the Layer trait API that was replaced by a struct based API."]
pub enum ExistingLayerStrategy {
Keep,
Recreate,
Update,
}
#[deprecated = "Part of the Layer trait API that was replaced by a struct based API."]
pub enum MetadataMigration<M> {
RecreateLayer,
ReplaceMetadata(M),
}
#[deprecated = "Part of the Layer trait API that was replaced by a struct based API."]
pub struct LayerData<M> {
pub name: LayerName,
pub path: PathBuf,
pub env: LayerEnv,
pub content_metadata: LayerContentMetadata<M>,
}
#[deprecated = "Part of the Layer trait API that was replaced by a struct based API."]
pub struct LayerResult<M> {
pub metadata: M,
pub env: Option<LayerEnv>,
pub exec_d_programs: HashMap<String, PathBuf>,
pub sboms: Vec<Sbom>,
}
#[deprecated = "Part of the Layer trait API that was replaced by a struct based API."]
pub struct LayerResultBuilder<M> {
metadata: M,
env: Option<LayerEnv>,
exec_d_programs: HashMap<String, PathBuf>,
sboms: Vec<Sbom>,
}
impl<M> LayerResultBuilder<M> {
#[must_use]
pub fn new(metadata: M) -> Self {
Self {
metadata,
env: None,
exec_d_programs: HashMap::new(),
sboms: Vec::new(),
}
}
#[must_use]
pub fn env(mut self, layer_env: LayerEnv) -> Self {
self.env = Some(layer_env);
self
}
#[must_use]
pub fn exec_d_program(
mut self,
name: impl Into<String>,
exec_d_program: impl Into<PathBuf>,
) -> Self {
self.exec_d_programs
.insert(name.into(), exec_d_program.into());
self
}
#[must_use]
pub fn sbom<S: Into<Sbom>>(mut self, s: S) -> Self {
self.sboms.push(s.into());
self
}
pub fn build<E>(self) -> Result<LayerResult<M>, E> {
Ok(self.build_unwrapped())
}
#[must_use]
pub fn build_unwrapped(self) -> LayerResult<M> {
LayerResult {
metadata: self.metadata,
env: self.env,
exec_d_programs: self.exec_d_programs,
sboms: self.sboms,
}
}
}