libcnb_data/buildpack/
stack.rs

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
use serde::Deserialize;

// Stacks are deprecated in Buildpack API 0.10, and libcnb.rs effectively
// ignores them. However, they are still supported by the Buildpack API, so
// libcnb should continue to allow them to exist in buildpack.toml.
#[derive(Deserialize, Debug, Eq, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct Stack {
    pub id: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub mixins: Vec<String>,
}

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

    #[test]
    fn deserialize_specific_stack_without_mixins() {
        let toml_str = r#"
id = "heroku-20"
"#;
        assert_eq!(
            toml::from_str::<Stack>(toml_str),
            Ok(Stack {
                id: String::from("heroku-20"),
                mixins: Vec::new()
            }),
        );
    }

    #[test]
    fn deserialize_specific_stack_with_mixins() {
        let toml_str = r#"
id = "io.buildpacks.stacks.focal"
mixins = ["build:jq", "wget"]
"#;
        assert_eq!(
            toml::from_str::<Stack>(toml_str),
            Ok(Stack {
                id: String::from("io.buildpacks.stacks.focal"),
                mixins: vec![String::from("build:jq"), String::from("wget")]
            }),
        );
    }

    #[test]
    fn deserialize_any_stack() {
        let toml_str = r#"
id = "*"
"#;
        assert_eq!(
            toml::from_str::<Stack>(toml_str),
            Ok(Stack {
                id: String::from("*"),
                mixins: Vec::new(),
            }),
        );
    }
}