Skip to main content

cargo_toml_builder/types/
target.rs

1use std::fmt;
2use std::default::Default;
3use std::convert::TryFrom;
4
5use crate::error::Error;
6
7/// Represents a `[lib]` table
8#[derive(Debug, Default, Clone, PartialEq)]
9pub struct LibTarget {
10    common: CommonTarget,
11    crate_type: Option<CrateType>,
12}
13
14/// Builder for [LibTarget](../types/struct.LibTarget.html) types
15#[derive(Debug, Default, Clone, PartialEq)]
16pub struct LibTargetBuilder {
17    name: Option<String>,
18    path: Option<String>,
19    test: Option<bool>,
20    doctest: Option<bool>,
21    bench: Option<bool>,
22    doc: Option<bool>,
23    plugin: Option<bool>,
24    proc_macro: Option<bool>,
25    harness: Option<bool>,
26    crate_type: Option<CrateType>,
27}
28
29impl LibTarget {
30    /// Constructs a new builder for a lib target
31    pub fn new() -> LibTargetBuilder {
32        Default::default()
33    }
34
35    /// Returns the value of the name of this lib target
36    pub fn name(&self) ->  Option<&String> {
37        self.common.name.as_ref()
38    }
39
40    /// Returns the value of the path to the root for this lib target
41    pub fn path(&self) ->  Option<&String> {
42        self.common.path.as_ref()
43    }
44
45    /// Returns the value of the test flag for this lib target
46    pub fn test(&self) ->  Option<bool> {
47        self.common.test
48    }
49
50    /// Returns the value of the doctest flag for this lib target
51    pub fn doctest(&self) ->  Option<bool> {
52        self.common.doctest
53    }
54
55    /// Returns the value of the bench flag for this lib target
56    pub fn bench(&self) ->  Option<bool> {
57        self.common.bench
58    }
59
60    /// Returns the value of the doc flag for this lib target
61    pub fn doc(&self) ->  Option<bool> {
62        self.common.doc
63    }
64
65    /// Returns the value of the plugin flag for this lib target
66    pub fn plugin(&self) ->  Option<bool> {
67        self.common.plugin
68    }
69
70    /// Returns the value of the proc-macro flag for this lib target
71    pub fn proc_macro(&self) ->  Option<bool> {
72        self.common.proc_macro
73    }
74
75    /// Returns the value of the harness flag for this lib target
76    pub fn harness(&self) ->  Option<bool> {
77        self.common.harness
78    }
79
80    /// Returns the value of the crate-type for this lib target
81    pub fn crate_type(&self) ->  Option<CrateType> {
82        self.crate_type
83    }
84
85}
86
87impl LibTargetBuilder {
88    /// Sets the name of this lib target
89    pub fn name(&mut self, name: &str) -> &mut Self {
90        self.name = Some(name.into());
91        self
92    }
93
94    /// Sets the path to the root for this lib target
95    pub fn path(&mut self, path: &str) -> &mut Self {
96        self.path = Some(path.into());
97        self
98    }
99
100    /// Sets the test flag for this lib target
101    pub fn test(&mut self, test: bool) -> &mut Self {
102        self.test = Some(test);
103        self
104    }
105
106    /// Sets the doctest flag for this lib target
107    pub fn doctest(&mut self, doctest: bool) -> &mut Self {
108        self.doctest = Some(doctest);
109        self
110    }
111
112    /// Sets the bench flag for this lib target
113    pub fn bench(&mut self, bench: bool) -> &mut Self {
114        self.bench = Some(bench);
115        self
116    }
117
118    /// Sets the doc flag for this lib target
119    pub fn doc(&mut self, doc: bool) -> &mut Self {
120        self.doc = Some(doc);
121        self
122    }
123
124    /// Sets the plugin flag for this lib target
125    pub fn plugin(&mut self, plugin: bool) -> &mut Self {
126        self.plugin = Some(plugin);
127        self
128    }
129
130    /// Sets the proc-macro flag for this lib target
131    pub fn proc_macro(&mut self, proc_macro: bool) -> &mut Self {
132        self.proc_macro = Some(proc_macro);
133        self
134    }
135
136    /// Sets the harness flag for this lib target
137    pub fn harness(&mut self, harness: bool) -> &mut Self {
138        self.harness = Some(harness);
139        self
140    }
141
142    /// Sets the crate-type for this lib target
143    pub fn crate_type(&mut self, crate_type: CrateType) -> &mut Self {
144        self.crate_type = Some(crate_type);
145        self
146    }
147
148    /// Constructs an instance of `LibTarget` from this builder
149    pub fn build(&self) -> LibTarget {
150        let common = CommonTarget {
151            name: self.name.clone(),
152            path: self.path.clone(),
153            test: self.test.clone(),
154            doctest: self.doctest.clone(),
155            bench: self.bench.clone(),
156            doc: self.doc.clone(),
157            plugin: self.plugin.clone(),
158            proc_macro: self.proc_macro.clone(),
159            harness: self.harness.clone(),
160        };
161        LibTarget {
162            common: common,
163            crate_type: self.crate_type.clone(),
164        }
165    }
166}
167
168impl<'a> From<&'a mut LibTargetBuilder> for LibTarget {
169    fn from(target: &'a mut LibTargetBuilder) -> LibTarget {
170        target.build()
171    }
172}
173
174#[derive(Debug, Default, Clone, PartialEq)]
175struct CommonTarget {
176    name: Option<String>,
177    path: Option<String>,
178    test: Option<bool>,
179    doctest: Option<bool>,
180    bench: Option<bool>,
181    doc: Option<bool>,
182    plugin: Option<bool>,
183    proc_macro: Option<bool>,
184    harness: Option<bool>,
185}
186
187/// Represents `[[bench]]`, `[[bin]]`, `[[example]]`, and `[[test]]` tables
188#[derive(Debug, Default, Clone, PartialEq)]
189pub struct NonLibTarget {
190    common: CommonTarget,
191    required_features: Option<Vec<String>>,
192}
193
194/// Builder for [NonLibTarget](../types/struct.NonLibTarget.html) types
195#[derive(Debug, Default, Clone, PartialEq)]
196pub struct NonLibTargetBuilder {
197    name: Option<String>,
198    path: Option<String>,
199    test: Option<bool>,
200    doctest: Option<bool>,
201    bench: Option<bool>,
202    doc: Option<bool>,
203    plugin: Option<bool>,
204    proc_macro: Option<bool>,
205    harness: Option<bool>,
206    required_features: Option<Vec<String>>,
207}
208
209impl NonLibTarget {
210    /// Constructs a builder for a non-lib target (bin, example, bench, test)
211    pub fn new() -> NonLibTargetBuilder {
212        Default::default()
213    }
214
215    /// Returns the value of the name of this target
216    pub fn name(&self) -> Option<&String> {
217        self.common.name.as_ref()
218    }
219
220    /// Returns the value of the path to the root for this target
221    pub fn path(&self) -> Option<&String> {
222        self.common.path.as_ref()
223    }
224
225    /// Returns the value of the test flag for this target
226    pub fn test(&self) -> Option<bool> {
227        self.common.test
228    }
229
230    /// Returns the value of the doctest flag for this target
231    pub fn doctest(&self) -> Option<bool> {
232        self.common.doctest
233    }
234
235    /// Returns the value of the bench flag for this target
236    pub fn bench(&self) -> Option<bool> {
237        self.common.bench
238    }
239
240    /// Returns the value of the doc flag for this target
241    pub fn doc(&self) -> Option<bool> {
242        self.common.doc
243    }
244
245    /// Returns the value of the plugin flag for this target
246    pub fn plugin(&self) ->  Option<bool> {
247        self.common.plugin
248    }
249
250    /// Returns the value of the proc-macro flag for this target
251    pub fn proc_macro(&self) -> Option<bool> {
252        self.common.proc_macro
253    }
254
255    /// Returns the value of the harness flag for this target
256    pub fn harness(&self) -> Option<bool> {
257        self.common.harness
258    }
259
260    /// Returns the value of the required-features value for this target
261    pub fn required_features(&self) -> Option<&Vec<String>> {
262        self.required_features.as_ref()
263    }
264}
265
266impl NonLibTargetBuilder {
267    /// Sets the name of this target
268    pub fn name(&mut self, name: &str) -> &mut Self {
269        self.name = Some(name.into());
270        self
271    }
272
273    /// Sets the path to the root for this target
274    pub fn path(&mut self, path: &str) -> &mut Self {
275        self.path = Some(path.into());
276        self
277    }
278
279    /// Sets the test flag for this target
280    pub fn test(&mut self, test: bool) -> &mut Self {
281        self.test = Some(test);
282        self
283    }
284
285    /// sets the doctest flag for this target
286    pub fn doctest(&mut self, doctest: bool) -> &mut Self {
287        self.doctest = Some(doctest);
288        self
289    }
290
291    /// sets the bench flag for this target
292    pub fn bench(&mut self, bench: bool) -> &mut Self {
293        self.bench = Some(bench);
294        self
295    }
296
297    /// sets the doc flag for this target
298    pub fn doc(&mut self, doc: bool) -> &mut Self {
299        self.doc = Some(doc);
300        self
301    }
302
303    /// Sets the plugin flag for this target
304    pub fn plugin(&mut self, plugin: bool) -> &mut Self {
305        self.plugin = Some(plugin);
306        self
307    }
308
309    /// Sets the proc-macro flag for this target
310    pub fn proc_macro(&mut self, proc_macro: bool) -> &mut Self {
311        self.proc_macro = Some(proc_macro);
312        self
313    }
314
315    /// sets the harness flag for this target
316    pub fn harness(&mut self, harness: bool) -> &mut Self {
317        self.harness = Some(harness);
318        self
319    }
320
321    /// Sets a required feature for this target
322    pub fn required_feature(&mut self, feature: &str) -> &mut Self {
323        if let Some(ref mut v) = self.required_features {
324            v.push(feature.into());
325        } else {
326            self.required_features = Some(vec![feature.into()]);
327        }
328        self
329    }
330
331    /// Constructs a `NonLibTarget` from this builder
332    pub fn build(&self) -> NonLibTarget {
333        let common = CommonTarget {
334            name: self.name.clone(),
335            path: self.path.clone(),
336            test: self.test.clone(),
337            doctest: self.doctest.clone(),
338            bench: self.bench.clone(),
339            doc: self.doc.clone(),
340            plugin: self.plugin.clone(),
341            proc_macro: self.proc_macro.clone(),
342            harness: self.harness.clone(),
343        };
344
345        NonLibTarget {
346            common: common,
347            required_features: self.required_features.clone(),
348        }
349    }
350}
351
352impl<'a> From<&'a mut NonLibTargetBuilder> for NonLibTarget {
353    fn from(t: &'a mut NonLibTargetBuilder) -> NonLibTarget {
354        t.build()
355    }
356}
357
358/// Represents the choices for the `crate-type` attribute
359#[derive(Debug, Clone, Copy, PartialEq)]
360pub enum CrateType {
361    /// `type = "dylib"`
362    Dylib,
363    /// `type = "rlib"`
364    Rlib,
365    /// `type = "staticlib"`
366    Staticlib,
367    /// `type = "cdylib"`
368    Cdylib,
369    /// `type = "proc-macro"`
370    ProcMacro,
371}
372
373impl<'a> TryFrom<&'a str> for CrateType {
374    type Error = Error;
375
376    fn try_from(s: &'a str) -> Result<Self, Self::Error> {
377        Ok(match s.to_lowercase().as_str() {
378            "dylib" => CrateType::Dylib,
379            "rlib" => CrateType::Rlib,
380            "staticlib" => CrateType::Staticlib,
381            "cdylib" => CrateType::Cdylib,
382            "proc-macro" => CrateType::ProcMacro,
383            _ => return Err("not a valid crate type".into()),
384        })
385    }
386}
387
388impl TryFrom<String> for CrateType {
389    type Error = Error;
390
391    fn try_from(s: String) -> Result<Self, Self::Error> {
392        TryFrom::try_from(s.as_str())
393    }
394}
395
396impl fmt::Display for CrateType {
397    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
398        write!(f, "{}", match *self {
399            CrateType::Dylib => "dylib",
400            CrateType::Rlib => "rlib",
401            CrateType::Staticlib => "staticlib",
402            CrateType::Cdylib => "cdylib",
403            CrateType::ProcMacro => "proc-macro",
404        })
405    }
406}
407
408/// Represents a `[[bin]]` table
409/// 
410/// See [NonLibTarget](../types/struct.NonLibTarget.html) docs for more
411pub type BinTarget = NonLibTarget;
412
413/// Represents a `[[bench]]` table
414/// 
415/// See [NonLibTarget](../types/struct.NonLibTarget.html) docs for more
416pub type BenchTarget = NonLibTarget;
417
418/// Represents a `[[test]]` table
419/// 
420/// See [NonLibTarget](../types/struct.NonLibTarget.html) docs for more
421pub type TestTarget = NonLibTarget;
422
423/// Represents an `[[example]]` table
424/// 
425/// See [NonLibTarget](../types/struct.NonLibTarget.html) docs for more
426pub type ExampleTarget = NonLibTarget;
427