Skip to main content

kea_lifecycle/buildpack/
mod.rs

1use std::{fmt::Display, str::FromStr};
2
3use error::BuildpackError;
4
5pub mod args;
6pub mod error;
7pub mod executable;
8
9#[derive(
10    Debug,
11    Clone,
12    Default,
13    Copy,
14    Eq,
15    PartialEq,
16    Ord,
17    PartialOrd,
18    serde::Serialize,
19    serde::Deserialize,
20)]
21pub struct Version {
22    pub major: u64,
23    pub minor: u64,
24}
25
26impl Display for Version {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        write!(f, "{}.{}", self.major, self.minor)
29    }
30}
31
32impl FromStr for Version {
33    type Err = BuildpackError;
34
35    fn from_str(s: &str) -> Result<Self, Self::Err> {
36        let mut ver = s.splitn(2, '.');
37
38        let major = {
39            ver.next()
40                .ok_or_else(|| BuildpackError::InvalidVersion(s.into()))?
41                .parse()
42                .map_err(|_| BuildpackError::InvalidVersion(s.into()))?
43        };
44
45        let minor = {
46            if let Some(minor_str) = ver.next() {
47                minor_str
48                    .parse()
49                    .map_err(|_| BuildpackError::InvalidVersion(s.into()))?
50            } else {
51                0
52            }
53        };
54
55        if ver.next().is_some() {
56            return Err(BuildpackError::InvalidVersion(s.into()));
57        }
58
59        Ok(Self { major, minor })
60    }
61}
62
63impl Version {
64    pub const fn new(major: u64, minor: u64) -> Self {
65        Self { major, minor }
66    }
67}
68
69impl Version {
70    pub fn is_superset_of(&self, version: &Version) -> bool {
71        if self.major == 0 {
72            self == version
73        } else {
74            self.major == version.major && self.minor >= version.minor
75        }
76    }
77}
78
79#[derive(Debug)]
80pub struct APIs {
81    pub supported: &'static [Version],
82    pub depreciated: &'static [Version],
83}
84
85impl APIs {
86    pub const PLATFORM: APIs = {
87        APIs {
88            supported: &[
89                Version::new(0, 7),
90                Version::new(0, 8),
91                Version::new(0, 9),
92                Version::new(0, 10),
93                Version::new(0, 11),
94                Version::new(0, 12),
95                Version::new(0, 13),
96                Version::new(0, 14),
97            ],
98            depreciated: &[],
99        }
100    };
101
102    pub const BUILDPACK: APIs = {
103        APIs {
104            supported: &[
105                Version::new(0, 7),
106                Version::new(0, 8),
107                Version::new(0, 9),
108                Version::new(0, 10),
109                Version::new(0, 11),
110            ],
111            depreciated: &[],
112        }
113    };
114
115    pub fn is_supported(&self, version: Version) -> bool {
116        for api in self.supported {
117            if api.is_superset_of(&version) {
118                return true;
119            }
120        }
121        false
122    }
123
124    pub fn is_depreciated(&self, version: Version) -> bool {
125        for api in self.depreciated {
126            if api.is_superset_of(&version) {
127                return true;
128            }
129        }
130        false
131    }
132}