Skip to main content

cargo_toml_builder/types/
mod.rs

1use std::collections::BTreeMap;
2
3pub use self::badges::*;
4pub use self::dependency::*;
5pub use self::feature::*;
6pub use self::license::*;
7pub use self::profile::*;
8pub use self::target::*;
9
10mod badges;
11mod dependency;
12mod feature;
13mod license;
14mod profile;
15mod target;
16
17#[derive(Debug, Clone, PartialEq, Default)]
18/// Represents the `[workspace]` section of a Cargo.toml file
19pub struct Workspace {
20    pub(crate) members: Option<Vec<String>>,
21    pub(crate) exclude: Option<Vec<String>>,
22}
23
24impl Workspace {
25    /// Constructs a new, empty `Workspace`
26    pub fn new() -> Workspace {
27        Default::default()
28    }
29
30    /// Adds a single `member` to this builder
31    ///
32    /// Will _not_ affect any existing members that have been added
33    pub fn member(&mut self, member: &str) -> &mut Self {
34        if let Some(ref mut v) = self.members {
35            v.push(member.into());
36        } else {
37            self.members = Some(vec![member.into()]);
38        }
39        self
40    }
41
42    /// Adds a set of `members` to this builder
43    ///
44    /// *WILL* replace any existing members that have been added
45    pub fn members(&mut self, members: &[String]) -> &mut Self {
46        self.members = Some(members.to_vec());
47        self
48    }
49
50    /// Adds an `exclude` to this builder
51    ///
52    /// Will _not_ affect any other excludes that have been added
53    pub fn exclude(&mut self, exclude: &str) -> &mut Self {
54        if let Some(ref mut v) = self.exclude {
55            v.push(exclude.into());
56        } else {
57            self.exclude = Some(vec![exclude.into()]);
58        }
59        self
60    }
61
62    /// Adds a set of `excludes` to this builder
63    ///
64    /// *WILL* replace any existing excludes that have been added
65    pub fn excludes(&mut self, excludes: &[String]) -> &mut Self {
66        self.exclude = Some(excludes.to_vec());
67        self
68    }
69
70    /// Takes ownership of this builder
71    pub fn build(&self) -> Self {
72        self.clone()
73    }
74}
75
76
77/// Represents a patchset for a Cargo.toml
78#[derive(Debug, Clone, PartialEq)]
79pub struct PatchSet {
80    pub(crate) label: String,
81    pub(crate) patches: Vec<Dependency>,
82}
83
84impl PatchSet {
85    /// Constructs a new, empty patchset
86    pub fn new(label: &str) -> PatchSet {
87        PatchSet {
88            label: label.into(),
89            patches: vec![],
90        }
91    }
92
93    /// Adds a single patch to this builder
94    ///
95    /// Will _not_ affect any other patches that have been added
96    pub fn patch<D: Into<Dependency>>(&mut self, patch: D) -> &mut Self {
97        self.patches.push(patch.into());
98        self
99    }
100
101    /// Adds a set of patches to this builder
102    ///
103    /// *WILL* replace any existing patches that have been added to this builder
104    pub fn patches(&mut self, patches: &[Dependency]) -> &mut Self {
105        self.patches = patches.to_vec();
106        self
107    }
108
109    /// Takes ownership of this builder
110    pub fn build(&self) -> Self {
111        self.clone()
112    }
113}
114
115/// Represents a dependency replacement for a Cargo.toml
116#[derive(Debug, Clone, PartialEq)]
117pub struct Replace(pub(crate) Dependency);
118
119impl Replace {
120    /// Creates a new entry in the [replace] section of a Cargo.toml
121    pub fn new<D: Into<Dependency>>(dep: D) -> Replace {
122        Replace(dep.into())
123    }
124}
125
126/// Represents the metadata table for a Cargo.toml
127#[derive(Debug, Clone, PartialEq)]
128pub struct MetadataTable {
129    pub(crate) label: String,
130    pub(crate) data: BTreeMap<String, String>,
131}