libcnb_data/
layer.rs

1use crate::newtypes::libcnb_newtype;
2
3libcnb_newtype!(
4    layer,
5    /// Construct a [`LayerName`] value at compile time.
6    ///
7    /// Passing a string that is not a valid `LayerName` value will yield a compilation error.
8    ///
9    /// # Examples:
10    /// ```
11    /// use libcnb_data::layer::LayerName;
12    /// use libcnb_data::layer_name;
13    ///
14    /// let layer_name: LayerName = layer_name!("foobar");
15    /// ```
16    layer_name,
17    /// The name of a layer.
18    ///
19    /// It can contain all characters supported by the filesystem, but MUST NOT be either `build`,
20    /// `launch` or `store`.
21    ///
22    /// Use the [`layer_name`](crate::layer_name) macro to construct a `LayerName` from a literal string. To
23    /// parse a dynamic string into a `LayerName`, use [`str::parse`](str::parse).
24    ///
25    /// # Examples
26    /// ```
27    /// use libcnb_data::layer::LayerName;
28    /// use libcnb_data::layer_name;
29    ///
30    /// let from_literal = layer_name!("foobar");
31    ///
32    /// let input = "foobar";
33    /// let from_dynamic: LayerName = input.parse().unwrap();
34    /// assert_eq!(from_dynamic, from_literal);
35    ///
36    /// let input = "build";
37    /// let invalid: Result<LayerName, _> = input.parse();
38    /// assert!(invalid.is_err());
39    /// ```
40    LayerName,
41    LayerNameError,
42    r"^(?!(build|launch|store)$).+$"
43);
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn layer_name_validation_valid() {
51        assert!("gems".parse::<LayerName>().is_ok());
52        assert!("Abc 123.-_!".parse::<LayerName>().is_ok());
53        assert!("build-foo".parse::<LayerName>().is_ok());
54        assert!("foo-build".parse::<LayerName>().is_ok());
55    }
56
57    #[test]
58    fn layer_name_validation_invalid() {
59        assert_eq!(
60            "build".parse::<LayerName>(),
61            Err(LayerNameError::InvalidValue(String::from("build")))
62        );
63        assert_eq!(
64            "launch".parse::<LayerName>(),
65            Err(LayerNameError::InvalidValue(String::from("launch")))
66        );
67        assert_eq!(
68            "store".parse::<LayerName>(),
69            Err(LayerNameError::InvalidValue(String::from("store")))
70        );
71        assert_eq!(
72            "".parse::<LayerName>(),
73            Err(LayerNameError::InvalidValue(String::new()))
74        );
75    }
76}