use semver::Version;
use super::cargo::{
Dependencies as CargoDependencies, DependenciesMut as CargoDependenciesMut,
DependencyMut as CargoDependencyMut, DependencyRef as CargoDependencyRef,
};
#[derive(Clone, Debug)]
pub enum DependencyRef<'a> {
Cargo(CargoDependencyRef<'a>),
}
impl<'a> DependencyRef<'a> {
pub fn name(&self) -> &'a str {
match self {
Self::Cargo(dependency) => dependency.name(),
}
}
pub fn version(&self) -> Option<&'a str> {
match self {
Self::Cargo(dependency) => dependency.version(),
}
}
pub fn path(&self) -> Option<&'a str> {
match self {
Self::Cargo(dependency) => dependency.path(),
}
}
}
#[derive(Clone, Debug)]
pub enum Dependencies<'a> {
Cargo(CargoDependencies<'a>),
}
impl<'a> Dependencies<'a> {
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))
}
}
}
}
#[derive(Debug)]
pub enum DependencyMut<'a> {
Cargo(CargoDependencyMut<'a>),
}
impl DependencyMut<'_> {
pub fn name(&self) -> &str {
match self {
Self::Cargo(dependency) => dependency.name(),
}
}
pub fn version(&self) -> Option<Version> {
match self {
Self::Cargo(dependency) => dependency.version(),
}
}
pub fn set_version(&mut self, version: impl Into<Version>) {
match self {
Self::Cargo(dependency) => dependency.set_version(version),
}
}
pub fn path(&self) -> Option<&str> {
match self {
Self::Cargo(dependency) => dependency.path(),
}
}
}
#[derive(Debug)]
pub enum DependenciesMut<'a> {
Cargo(CargoDependenciesMut<'a>),
}
impl DependenciesMut<'_> {
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))
}
}
}
}