Skip to main content

ploys/package/manifest/
mod.rs

1//! Manifest inspection and management utilities
2//!
3//! This module includes utilities for inspecting and managing package manifests
4//! across different package managers.
5
6pub mod cargo;
7mod dependency;
8mod error;
9mod members;
10
11use std::fmt::{self, Display};
12
13use strum::{EnumIs, EnumTryAs};
14
15pub use self::cargo::CargoManifest;
16pub use self::dependency::{Dependencies, DependenciesMut, DependencyMut, DependencyRef};
17pub use self::error::Error;
18pub use self::members::Members;
19
20use super::PackageKind;
21
22/// The package manifest.
23#[derive(Clone, Debug, PartialEq, Eq, EnumIs, EnumTryAs)]
24pub enum Manifest {
25    /// A cargo package manifest.
26    Cargo(CargoManifest),
27}
28
29impl Manifest {
30    /// Constructs a new cargo manifest.
31    pub fn new_cargo(name: impl Into<String>) -> Self {
32        Self::Cargo(CargoManifest::new_package(name))
33    }
34
35    /// Gets the package kind.
36    pub fn package_kind(&self) -> PackageKind {
37        match self {
38            Self::Cargo(_) => PackageKind::Cargo,
39        }
40    }
41
42    /// Gets the workspace members.
43    pub fn members(&self) -> Result<Members, Error> {
44        match self {
45            Self::Cargo(cargo) => Ok(cargo.members()?),
46        }
47    }
48
49    /// Creates a manifest from the given bytes.
50    pub fn from_bytes(kind: PackageKind, bytes: &[u8]) -> Result<Self, Error> {
51        match kind {
52            PackageKind::Cargo => Ok(Self::Cargo(CargoManifest::from_bytes(bytes)?)),
53        }
54    }
55}
56
57impl Manifest {
58    /// Gets the dependency with the given name.
59    pub fn get_dependency(&self, name: impl AsRef<str>) -> Option<DependencyRef<'_>> {
60        match self {
61            Self::Cargo(cargo) => cargo.get_dependency(name).map(DependencyRef::Cargo),
62        }
63    }
64
65    /// Gets the mutable dependency with the given name.
66    pub fn get_dependency_mut(&mut self, name: impl AsRef<str>) -> Option<DependencyMut<'_>> {
67        match self {
68            Self::Cargo(cargo) => cargo.get_dependency_mut(name).map(DependencyMut::Cargo),
69        }
70    }
71
72    /// Gets the dependencies.
73    pub fn dependencies(&self) -> Dependencies<'_> {
74        match self {
75            Self::Cargo(cargo) => Dependencies::Cargo(cargo.dependencies()),
76        }
77    }
78
79    /// Gets the mutable dependencies.
80    pub fn dependencies_mut(&mut self) -> DependenciesMut<'_> {
81        match self {
82            Self::Cargo(cargo) => DependenciesMut::Cargo(cargo.dependencies_mut()),
83        }
84    }
85}
86
87impl Manifest {
88    /// Gets the dev dependency with the given name.
89    pub fn get_dev_dependency(&self, name: impl AsRef<str>) -> Option<DependencyRef<'_>> {
90        match self {
91            Self::Cargo(cargo) => cargo.get_dev_dependency(name).map(DependencyRef::Cargo),
92        }
93    }
94
95    /// Gets the mutable dev dependency with the given name.
96    pub fn get_dev_dependency_mut(&mut self, name: impl AsRef<str>) -> Option<DependencyMut<'_>> {
97        match self {
98            Self::Cargo(cargo) => cargo.get_dev_dependency_mut(name).map(DependencyMut::Cargo),
99        }
100    }
101
102    /// Gets the dev dependencies.
103    pub fn dev_dependencies(&self) -> Dependencies<'_> {
104        match self {
105            Self::Cargo(cargo) => Dependencies::Cargo(cargo.dev_dependencies()),
106        }
107    }
108
109    /// Gets the mutable dev dependencies.
110    pub fn dev_dependencies_mut(&mut self) -> DependenciesMut<'_> {
111        match self {
112            Self::Cargo(cargo) => DependenciesMut::Cargo(cargo.dev_dependencies_mut()),
113        }
114    }
115}
116
117impl Manifest {
118    /// Gets the build dependency with the given name.
119    pub fn get_build_dependency(&self, name: impl AsRef<str>) -> Option<DependencyRef<'_>> {
120        match self {
121            Self::Cargo(cargo) => cargo.get_build_dependency(name).map(DependencyRef::Cargo),
122        }
123    }
124
125    /// Gets the mutable build dependency with the given name.
126    pub fn get_build_dependency_mut(&mut self, name: impl AsRef<str>) -> Option<DependencyMut<'_>> {
127        match self {
128            Self::Cargo(cargo) => cargo
129                .get_build_dependency_mut(name)
130                .map(DependencyMut::Cargo),
131        }
132    }
133
134    /// Gets the build dependencies.
135    pub fn build_dependencies(&self) -> Dependencies<'_> {
136        match self {
137            Self::Cargo(cargo) => Dependencies::Cargo(cargo.build_dependencies()),
138        }
139    }
140
141    /// Gets the mutable build dependencies.
142    pub fn build_dependencies_mut(&mut self) -> DependenciesMut<'_> {
143        match self {
144            Self::Cargo(cargo) => DependenciesMut::Cargo(cargo.build_dependencies_mut()),
145        }
146    }
147}
148
149impl Display for Manifest {
150    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
151        match self {
152            Self::Cargo(cargo) => Display::fmt(cargo, f),
153        }
154    }
155}