cargo_toml_builder/types/
target.rs1use std::fmt;
2use std::default::Default;
3use std::convert::TryFrom;
4
5use crate::error::Error;
6
7#[derive(Debug, Default, Clone, PartialEq)]
9pub struct LibTarget {
10 common: CommonTarget,
11 crate_type: Option<CrateType>,
12}
13
14#[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 pub fn new() -> LibTargetBuilder {
32 Default::default()
33 }
34
35 pub fn name(&self) -> Option<&String> {
37 self.common.name.as_ref()
38 }
39
40 pub fn path(&self) -> Option<&String> {
42 self.common.path.as_ref()
43 }
44
45 pub fn test(&self) -> Option<bool> {
47 self.common.test
48 }
49
50 pub fn doctest(&self) -> Option<bool> {
52 self.common.doctest
53 }
54
55 pub fn bench(&self) -> Option<bool> {
57 self.common.bench
58 }
59
60 pub fn doc(&self) -> Option<bool> {
62 self.common.doc
63 }
64
65 pub fn plugin(&self) -> Option<bool> {
67 self.common.plugin
68 }
69
70 pub fn proc_macro(&self) -> Option<bool> {
72 self.common.proc_macro
73 }
74
75 pub fn harness(&self) -> Option<bool> {
77 self.common.harness
78 }
79
80 pub fn crate_type(&self) -> Option<CrateType> {
82 self.crate_type
83 }
84
85}
86
87impl LibTargetBuilder {
88 pub fn name(&mut self, name: &str) -> &mut Self {
90 self.name = Some(name.into());
91 self
92 }
93
94 pub fn path(&mut self, path: &str) -> &mut Self {
96 self.path = Some(path.into());
97 self
98 }
99
100 pub fn test(&mut self, test: bool) -> &mut Self {
102 self.test = Some(test);
103 self
104 }
105
106 pub fn doctest(&mut self, doctest: bool) -> &mut Self {
108 self.doctest = Some(doctest);
109 self
110 }
111
112 pub fn bench(&mut self, bench: bool) -> &mut Self {
114 self.bench = Some(bench);
115 self
116 }
117
118 pub fn doc(&mut self, doc: bool) -> &mut Self {
120 self.doc = Some(doc);
121 self
122 }
123
124 pub fn plugin(&mut self, plugin: bool) -> &mut Self {
126 self.plugin = Some(plugin);
127 self
128 }
129
130 pub fn proc_macro(&mut self, proc_macro: bool) -> &mut Self {
132 self.proc_macro = Some(proc_macro);
133 self
134 }
135
136 pub fn harness(&mut self, harness: bool) -> &mut Self {
138 self.harness = Some(harness);
139 self
140 }
141
142 pub fn crate_type(&mut self, crate_type: CrateType) -> &mut Self {
144 self.crate_type = Some(crate_type);
145 self
146 }
147
148 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#[derive(Debug, Default, Clone, PartialEq)]
189pub struct NonLibTarget {
190 common: CommonTarget,
191 required_features: Option<Vec<String>>,
192}
193
194#[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 pub fn new() -> NonLibTargetBuilder {
212 Default::default()
213 }
214
215 pub fn name(&self) -> Option<&String> {
217 self.common.name.as_ref()
218 }
219
220 pub fn path(&self) -> Option<&String> {
222 self.common.path.as_ref()
223 }
224
225 pub fn test(&self) -> Option<bool> {
227 self.common.test
228 }
229
230 pub fn doctest(&self) -> Option<bool> {
232 self.common.doctest
233 }
234
235 pub fn bench(&self) -> Option<bool> {
237 self.common.bench
238 }
239
240 pub fn doc(&self) -> Option<bool> {
242 self.common.doc
243 }
244
245 pub fn plugin(&self) -> Option<bool> {
247 self.common.plugin
248 }
249
250 pub fn proc_macro(&self) -> Option<bool> {
252 self.common.proc_macro
253 }
254
255 pub fn harness(&self) -> Option<bool> {
257 self.common.harness
258 }
259
260 pub fn required_features(&self) -> Option<&Vec<String>> {
262 self.required_features.as_ref()
263 }
264}
265
266impl NonLibTargetBuilder {
267 pub fn name(&mut self, name: &str) -> &mut Self {
269 self.name = Some(name.into());
270 self
271 }
272
273 pub fn path(&mut self, path: &str) -> &mut Self {
275 self.path = Some(path.into());
276 self
277 }
278
279 pub fn test(&mut self, test: bool) -> &mut Self {
281 self.test = Some(test);
282 self
283 }
284
285 pub fn doctest(&mut self, doctest: bool) -> &mut Self {
287 self.doctest = Some(doctest);
288 self
289 }
290
291 pub fn bench(&mut self, bench: bool) -> &mut Self {
293 self.bench = Some(bench);
294 self
295 }
296
297 pub fn doc(&mut self, doc: bool) -> &mut Self {
299 self.doc = Some(doc);
300 self
301 }
302
303 pub fn plugin(&mut self, plugin: bool) -> &mut Self {
305 self.plugin = Some(plugin);
306 self
307 }
308
309 pub fn proc_macro(&mut self, proc_macro: bool) -> &mut Self {
311 self.proc_macro = Some(proc_macro);
312 self
313 }
314
315 pub fn harness(&mut self, harness: bool) -> &mut Self {
317 self.harness = Some(harness);
318 self
319 }
320
321 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 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#[derive(Debug, Clone, Copy, PartialEq)]
360pub enum CrateType {
361 Dylib,
363 Rlib,
365 Staticlib,
367 Cdylib,
369 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
408pub type BinTarget = NonLibTarget;
412
413pub type BenchTarget = NonLibTarget;
417
418pub type TestTarget = NonLibTarget;
422
423pub type ExampleTarget = NonLibTarget;
427