rustilities/manifest/
types.rs

1// SPDX-License-Identifier: GPL-3.0
2
3#[cfg(test)]
4mod tests;
5
6use std::path::Path;
7
8/// A struct representing how a dependency should look like in a Rust manifest.
9#[derive(Debug, Clone, PartialEq)]
10pub struct ManifestDependencyConfig<'a> {
11	pub origin: ManifestDependencyOrigin<'a>,
12	pub default_features: bool,
13	pub features: Vec<&'a str>,
14	pub optional: bool,
15}
16
17impl<'a> ManifestDependencyConfig<'a> {
18	/// Creates a new instance of ManifestDependencyConfig specifying:
19	/// - The dependency origin.
20	/// - If the dependency should use its default features.
21	/// - The features the dependency should use.
22	/// - If the dependency is optional.
23	pub fn new(
24		origin: ManifestDependencyOrigin<'a>,
25		default_features: bool,
26		features: Vec<&'a str>,
27		optional: bool,
28	) -> Self {
29		Self { origin, default_features, features, optional }
30	}
31
32	/// Add some features to an existing ManifestDependencyConfig
33	pub fn add_features(&mut self, features: &[&'a str]) {
34		self.features.extend_from_slice(features);
35	}
36}
37
38/// Different origins available for a dependency in a Rust manifest.
39#[derive(Debug, Clone, PartialEq)]
40pub enum ManifestDependencyOrigin<'a> {
41	CratesIO { version: &'a str },
42	Git { url: &'a str, branch: &'a str },
43	Local { relative_path: &'a Path },
44	Workspace,
45}
46
47impl<'a> ManifestDependencyOrigin<'a> {
48	/// Creates a dependency origin from a specific version in [crates.io](https://crates.io).
49	pub fn crates_io(version: &'a str) -> Self {
50		Self::CratesIO { version }
51	}
52
53	/// Creates a dependency origin from a specific branch in a git repository.
54	pub fn git(url: &'a str, branch: &'a str) -> Self {
55		Self::Git { url, branch }
56	}
57
58	/// Creates a dependency origin from a local path.
59	pub fn local(relative_path: &'a Path) -> Self {
60		Self::Local { relative_path }
61	}
62
63	/// Creates a dependency origin coming from the workspace.
64	pub fn workspace() -> Self {
65		Self::Workspace
66	}
67}