ploys 0.6.0

A utility to manage projects, packages, releases and deployments.
Documentation
use semver::Version;

use super::cargo::{
    Dependencies as CargoDependencies, DependenciesMut as CargoDependenciesMut,
    DependencyMut as CargoDependencyMut, DependencyRef as CargoDependencyRef,
};

/// The package dependency.
#[derive(Clone, Debug)]
pub enum DependencyRef<'a> {
    /// A cargo package dependency.
    Cargo(CargoDependencyRef<'a>),
}

impl<'a> DependencyRef<'a> {
    /// Gets the dependency name.
    pub fn name(&self) -> &'a str {
        match self {
            Self::Cargo(dependency) => dependency.name(),
        }
    }

    /// Gets the dependency version if it has been set.
    pub fn version(&self) -> Option<&'a str> {
        match self {
            Self::Cargo(dependency) => dependency.version(),
        }
    }

    /// Gets the dependency path if it has been set.
    pub fn path(&self) -> Option<&'a str> {
        match self {
            Self::Cargo(dependency) => dependency.path(),
        }
    }
}

/// The package dependencies.
#[derive(Clone, Debug)]
pub enum Dependencies<'a> {
    Cargo(CargoDependencies<'a>),
}

impl<'a> Dependencies<'a> {
    /// Gets the dependency with the given name.
    pub fn get(&self, name: impl AsRef<str>) -> Option<DependencyRef<'a>> {
        match self {
            Self::Cargo(dependencies) => dependencies.get(name).map(DependencyRef::Cargo),
        }
    }
}

impl<'a> IntoIterator for Dependencies<'a> {
    type Item = DependencyRef<'a>;
    type IntoIter = Box<dyn Iterator<Item = DependencyRef<'a>> + 'a>;

    fn into_iter(self) -> Self::IntoIter {
        match self {
            Self::Cargo(dependencies) => {
                Box::new(dependencies.into_iter().map(DependencyRef::Cargo))
            }
        }
    }
}

/// The mutable package dependency.
#[derive(Debug)]
pub enum DependencyMut<'a> {
    /// A cargo package dependency.
    Cargo(CargoDependencyMut<'a>),
}

impl DependencyMut<'_> {
    /// Gets the dependency name.
    pub fn name(&self) -> &str {
        match self {
            Self::Cargo(dependency) => dependency.name(),
        }
    }

    /// Gets the dependency version if it has been set.
    pub fn version(&self) -> Option<Version> {
        match self {
            Self::Cargo(dependency) => dependency.version(),
        }
    }

    /// Sets the dependency version.
    pub fn set_version(&mut self, version: impl Into<Version>) {
        match self {
            Self::Cargo(dependency) => dependency.set_version(version),
        }
    }

    /// Gets the dependency path if it has been set.
    pub fn path(&self) -> Option<&str> {
        match self {
            Self::Cargo(dependency) => dependency.path(),
        }
    }
}

/// The mutable package dependencies.
#[derive(Debug)]
pub enum DependenciesMut<'a> {
    Cargo(CargoDependenciesMut<'a>),
}

impl DependenciesMut<'_> {
    /// Gets the mutable dependency with the given name.
    pub fn get_mut(&mut self, name: impl AsRef<str>) -> Option<DependencyMut<'_>> {
        match self {
            Self::Cargo(dependencies) => dependencies.get_mut(name).map(DependencyMut::Cargo),
        }
    }
}

impl<'a> IntoIterator for DependenciesMut<'a> {
    type Item = DependencyMut<'a>;
    type IntoIter = Box<dyn Iterator<Item = DependencyMut<'a>> + 'a>;

    fn into_iter(self) -> Self::IntoIter {
        match self {
            Self::Cargo(dependencies) => {
                Box::new(dependencies.into_iter().map(DependencyMut::Cargo))
            }
        }
    }
}