Skip to content

Commit

Permalink
Merge pull request #106 from Malax/schneems/layer-content-metadata-ty…
Browse files Browse the repository at this point in the history
…pes-table

Support layer data under `types` key
  • Loading branch information
schneems authored Oct 20, 2021
2 parents 4a41330 + b11c9fc commit 2ca9fff
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [Unreleased]

- LayerContentMetadata values (build, cache, launch) are now under a "types" key
- Allow ProcessType to contain a dot (`.`) character

## [0.3.0] 2021/09/17
65 changes: 51 additions & 14 deletions src/data/layer_content_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ use serde::{Deserialize, Serialize};
use crate::data::defaults;
use crate::generic::GenericMetadata;

/// Describes Layer Content Metadata
///
/// See [Cloud Native Buildpack specification](https://github.com/buildpacks/spec/blob/main/buildpack.md#layer-content-metadata-toml)
/// Used to specify layer availability based
/// on buildpack phase.
#[derive(Debug, Deserialize, Serialize)]
pub struct LayerContentMetadata<M> {
pub struct LayerContentTypeTable {
/// Whether the layer is intended for launch.
#[serde(default = "defaults::r#false")]
pub launch: bool,
Expand All @@ -19,6 +18,34 @@ pub struct LayerContentMetadata<M> {
/// Whether the layer is cached.
#[serde(default = "defaults::r#false")]
pub cache: bool,
}

/// Describes Layer Content Metadata
///
/// See [Cloud Native Buildpack specification](https://github.com/buildpacks/spec/blob/main/buildpack.md#layer-content-metadata-toml)
///
/// ```
/// use libcnb::data::layer_content_metadata::LayerContentMetadata;
/// use toml::toml;
///
/// let layer = LayerContentMetadata::default()
/// .build(true)
/// .cache(true)
/// .launch(true)
/// .metadata(
/// toml! {
/// version = "2.5"
/// name = "ACME Corp."
/// });
///
/// assert!(layer.types.build);
///
/// let version = layer.metadata.get("version").unwrap().as_str().unwrap();
/// assert_eq!(version, "2.5");
/// ```
#[derive(Debug, Deserialize, Serialize)]
pub struct LayerContentMetadata<M> {
pub types: LayerContentTypeTable,

/// Metadata that describes the layer contents.
pub metadata: M,
Expand All @@ -27,35 +54,40 @@ pub struct LayerContentMetadata<M> {
impl Default for LayerContentMetadata<GenericMetadata> {
fn default() -> Self {
LayerContentMetadata {
launch: false,
build: false,
cache: false,
types: LayerContentTypeTable {
launch: false,
build: false,
cache: false,
},
metadata: GenericMetadata::default(),
}
}
}

impl<M> LayerContentMetadata<M> {
pub fn launch(mut self, launch: bool) -> Self {
self.launch = launch;
self.types.launch = launch;
self
}

pub fn build(mut self, build: bool) -> Self {
self.build = build;
self.types.build = build;
self
}

pub fn cache(mut self, cache: bool) -> Self {
self.cache = cache;
self.types.cache = cache;
self
}

pub fn metadata<NM>(&mut self, metadata: NM) -> LayerContentMetadata<NM> {
LayerContentMetadata {
cache: self.cache,
build: self.build,
launch: self.launch,
types: LayerContentTypeTable {
cache: self.types.cache,
build: self.types.build,
launch: self.types.launch,
},

metadata,
}
}
Expand All @@ -70,12 +102,17 @@ mod tests {
let layer: Result<LayerContentMetadata<Option<toml::value::Table>>, toml::de::Error> =
toml::from_str(
r#"
[types]
launch = true
build = true
cache = false
"#,
);

assert!(!layer.is_err());
let layer = layer.unwrap();
assert_eq!(layer.metadata, None);
assert!(layer.types.launch);
assert!(layer.types.build);
assert!(!layer.types.cache);
}
}

0 comments on commit 2ca9fff

Please sign in to comment.