1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use std::collections::BTreeMap;

pub use self::badges::*;
pub use self::dependency::*;
pub use self::feature::*;
pub use self::license::*;
pub use self::profile::*;
pub use self::target::*;

mod badges;
mod dependency;
mod feature;
mod license;
mod profile;
mod target;

#[derive(Debug, Clone, PartialEq, Default)]
/// Represents the `[workspace]` section of a Cargo.toml file
pub struct Workspace {
    pub(crate) members: Option<Vec<String>>,
    pub(crate) exclude: Option<Vec<String>>,
}

impl Workspace {
    /// Constructs a new, empty `Workspace`
    pub fn new() -> Workspace {
        Default::default()
    }

    /// Adds a single `member` to this builder
    ///
    /// Will _not_ affect any existing members that have been added
    pub fn member(&mut self, member: &str) -> &mut Self {
        if let Some(ref mut v) = self.members {
            v.push(member.into());
        } else {
            self.members = Some(vec![member.into()]);
        }
        self
    }

    /// Adds a set of `members` to this builder
    ///
    /// *WILL* replace any existing members that have been added
    pub fn members(&mut self, members: &[String]) -> &mut Self {
        self.members = Some(members.to_vec());
        self
    }

    /// Adds an `exclude` to this builder
    ///
    /// Will _not_ affect any other excludes that have been added
    pub fn exclude(&mut self, exclude: &str) -> &mut Self {
        if let Some(ref mut v) = self.exclude {
            v.push(exclude.into());
        } else {
            self.exclude = Some(vec![exclude.into()]);
        }
        self
    }

    /// Adds a set of `excludes` to this builder
    ///
    /// *WILL* replace any existing excludes that have been added
    pub fn excludes(&mut self, excludes: &[String]) -> &mut Self {
        self.exclude = Some(excludes.to_vec());
        self
    }

    /// Takes ownership of this builder
    pub fn build(&self) -> Self {
        self.clone()
    }
}


/// Represents a patchset for a Cargo.toml
#[derive(Debug, Clone, PartialEq)]
pub struct PatchSet {
    pub(crate) label: String,
    pub(crate) patches: Vec<Dependency>,
}

impl PatchSet {
    /// Constructs a new, empty patchset
    pub fn new(label: &str) -> PatchSet {
        PatchSet {
            label: label.into(),
            patches: vec![],
        }
    }

    /// Adds a single patch to this builder
    ///
    /// Will _not_ affect any other patches that have been added
    pub fn patch<D: Into<Dependency>>(&mut self, patch: D) -> &mut Self {
        self.patches.push(patch.into());
        self
    }

    /// Adds a set of patches to this builder
    ///
    /// *WILL* replace any existing patches that have been added to this builder
    pub fn patches(&mut self, patches: &[Dependency]) -> &mut Self {
        self.patches = patches.to_vec();
        self
    }

    /// Takes ownership of this builder
    pub fn build(&self) -> Self {
        self.clone()
    }
}

/// Represents a dependency replacement for a Cargo.toml
#[derive(Debug, Clone, PartialEq)]
pub struct Replace(pub(crate) Dependency);

impl Replace {
    /// Creates a new entry in the [replace] section of a Cargo.toml
    pub fn new<D: Into<Dependency>>(dep: D) -> Replace {
        Replace(dep.into())
    }
}

/// Represents the metadata table for a Cargo.toml
#[derive(Debug, Clone, PartialEq)]
pub struct MetadataTable {
    pub(crate) label: String,
    pub(crate) data: BTreeMap<String, String>,
}