Skip to main content

cargo_toml_builder/types/
feature.rs

1use std::default::Default;
2
3use crate::types::Dependency;
4
5/// Represents a single feature
6#[derive(Default, Debug, Clone, PartialEq)]
7pub struct Feature {
8    pub(crate) label: String,
9    pub(crate) deps: Vec<Dependency>,
10    pub(crate) features: Vec<String>,
11}
12
13impl Feature {
14    /// Constructs a new, empty feature from it's label
15    pub fn new(label: &str) -> Feature {
16        let mut f = Feature::default();
17        f.label = label.to_string();
18        f
19    }
20
21    /// Adds a dependency to the list for this feature
22    pub fn dependency<D: Into<Dependency>>(&mut self, dep: D) -> &mut Self {
23        self.deps.push(dep.into());
24        self
25    }
26
27    /// Sets the list of dependencies for this feature
28    pub fn dependencies<D: Into<Dependency>>(&mut self, deps: Vec<D>) -> &mut Self {
29        self.deps = deps.into_iter().map(|d| d.into()).collect();
30        self
31    }
32
33    /// Adds a feature to the list
34    pub fn feature(&mut self, feature: &str) -> &mut Self {
35        self.features.push(feature.to_string());
36        self
37    }
38
39    /// Sets the list of features
40    ///
41    /// *WILL* replace any existing features
42    pub fn features(&mut self, features: &[String]) -> &mut Self {
43        self.features = features.to_vec();
44        self
45    }
46
47    /// Takes ownership of this builder
48    pub fn build(&self) -> Self {
49        self.clone()
50    }
51}
52
53impl<'a> From<&'a mut Feature> for Feature {
54    fn from(f: &'a mut Feature) -> Feature {
55        f.clone()
56    }
57}
58
59impl<'a> From<&'a str> for Feature {
60    fn from(s: &'a str) -> Feature {
61        Feature {
62            label: s.to_string(),
63            deps: vec![],
64            features: vec![],
65        }
66    }
67}
68