kea-lifecycle 0.1.0

buildpack lifecycle
Documentation
use std::{fmt::Display, str::FromStr};

use error::BuildpackError;

pub mod args;
pub mod error;
pub mod executable;

#[derive(
    Debug,
    Clone,
    Default,
    Copy,
    Eq,
    PartialEq,
    Ord,
    PartialOrd,
    serde::Serialize,
    serde::Deserialize,
)]
pub struct Version {
    pub major: u64,
    pub minor: u64,
}

impl Display for Version {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}.{}", self.major, self.minor)
    }
}

impl FromStr for Version {
    type Err = BuildpackError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut ver = s.splitn(2, '.');

        let major = {
            ver.next()
                .ok_or_else(|| BuildpackError::InvalidVersion(s.into()))?
                .parse()
                .map_err(|_| BuildpackError::InvalidVersion(s.into()))?
        };

        let minor = {
            if let Some(minor_str) = ver.next() {
                minor_str
                    .parse()
                    .map_err(|_| BuildpackError::InvalidVersion(s.into()))?
            } else {
                0
            }
        };

        if ver.next().is_some() {
            return Err(BuildpackError::InvalidVersion(s.into()));
        }

        Ok(Self { major, minor })
    }
}

impl Version {
    pub const fn new(major: u64, minor: u64) -> Self {
        Self { major, minor }
    }
}

impl Version {
    pub fn is_superset_of(&self, version: &Version) -> bool {
        if self.major == 0 {
            self == version
        } else {
            self.major == version.major && self.minor >= version.minor
        }
    }
}

#[derive(Debug)]
pub struct APIs {
    pub supported: &'static [Version],
    pub depreciated: &'static [Version],
}

impl APIs {
    pub const PLATFORM: APIs = {
        APIs {
            supported: &[
                Version::new(0, 7),
                Version::new(0, 8),
                Version::new(0, 9),
                Version::new(0, 10),
                Version::new(0, 11),
                Version::new(0, 12),
                Version::new(0, 13),
                Version::new(0, 14),
            ],
            depreciated: &[],
        }
    };

    pub const BUILDPACK: APIs = {
        APIs {
            supported: &[
                Version::new(0, 7),
                Version::new(0, 8),
                Version::new(0, 9),
                Version::new(0, 10),
                Version::new(0, 11),
            ],
            depreciated: &[],
        }
    };

    pub fn is_supported(&self, version: Version) -> bool {
        for api in self.supported {
            if api.is_superset_of(&version) {
                return true;
            }
        }
        false
    }

    pub fn is_depreciated(&self, version: Version) -> bool {
        for api in self.depreciated {
            if api.is_superset_of(&version) {
                return true;
            }
        }
        false
    }
}