1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use crate::generic::GenericMetadata;
use serde::{Deserialize, Serialize};

/// Describes Layer Content Metadata
///
/// See [Cloud Native Buildpack specification](https://github.com/buildpacks/spec/blob/main/buildpack.md#layer-content-metadata-toml)
#[derive(Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct LayerContentMetadata<M = GenericMetadata> {
    pub types: Option<LayerTypes>,

    /// Metadata that describes the layer contents.
    pub metadata: M,
}

impl<M: PartialEq> PartialEq for LayerContentMetadata<M> {
    fn eq(&self, other: &Self) -> bool {
        self.types == other.types && self.metadata == other.metadata
    }
}

/// Used to specify layer availability based
/// on buildpack phase.
#[derive(Debug, Default, Deserialize, Serialize, Eq, PartialEq, Copy, Clone)]
#[serde(deny_unknown_fields)]
pub struct LayerTypes {
    /// Whether the layer is intended for launch.
    #[serde(default)]
    pub launch: bool,

    /// Whether the layer is intended for build.
    #[serde(default)]
    pub build: bool,

    /// Whether the layer is cached.
    #[serde(default)]
    pub cache: bool,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn deserialize_everything() {
        let toml_str = r#"
        [types]
        launch = true
        build = true
        cache = false

        [metadata]
        version = "1.2.3"
        "#;
        let layer = toml::from_str::<LayerContentMetadata>(toml_str).unwrap();
        assert_eq!(
            layer.types,
            Some(LayerTypes {
                launch: true,
                build: true,
                cache: false
            })
        );
        assert_eq!(
            layer.metadata.unwrap().get("version"),
            Some(&toml::value::Value::try_from("1.2.3").unwrap())
        );
    }

    #[test]
    fn deserialize_empty() {
        let layer = toml::from_str::<LayerContentMetadata>("").unwrap();
        assert_eq!(layer.types, None);
        assert_eq!(layer.metadata, None);
    }

    #[test]
    fn types_table_with_no_entries_has_defaults() {
        let toml_str = r#"
        [types]
        "#;
        let layer = toml::from_str::<LayerContentMetadata>(toml_str).unwrap();
        assert_eq!(
            layer.types,
            Some(LayerTypes {
                launch: false,
                build: false,
                cache: false
            })
        );
        assert_eq!(layer.metadata, None);
    }
}