use {
    super::Package,
    crate::Version,
    serde::{Deserialize, Serialize},
};

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
/// Specifies a dependency requirement
pub struct Dependency {
    /// The name of the dependency. All packages must have a unique name.
    pub name: String,
    /// The version requirements for this dependency. If the low
    /// version is `Some`, then the version must be equal to or
    /// greater than this version. If the high version is `Some`,
    /// then the version must be less than this version.
    pub version: (Option<Version>, Option<Version>),
}

impl Dependency {
    #[allow(clippy::must_use_candidate)]
    /// Checks whether a package satisfies a given dependency
    pub fn satisfied(&self, package: &Package) -> bool {
        if self.name.as_str() == package.name.as_str() {
            match &self.version {
                (Some(low), Some(high)) => &package.version >= low && &package.version < high,
                (Some(low), None) => &package.version >= low,
                (None, Some(high)) => &package.version < high,
                // no version requirements
                _ => true,
            }
        } else {
            false
        }
    }
}
