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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
use std::path::Path;
use std::fs;
use std::io;
use toml;
#[macro_use]
extern crate serde_derive;
use serde::Deserialize;
use std::collections::BTreeMap;
pub use toml::Value;
pub type DepsSet = BTreeMap<String, Dependency>;
pub type TargetDepsSet = BTreeMap<String, Target>;
pub type FeatureSet = BTreeMap<String, Vec<String>>;
mod error;
mod afs;
pub use crate::error::Error;
pub use crate::afs::*;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Manifest<Metadata = Value> {
pub package: Option<Package<Metadata>>,
pub workspace: Option<Workspace>,
#[serde(default)]
pub dependencies: DepsSet,
#[serde(default)]
pub dev_dependencies: DepsSet,
#[serde(default)]
pub build_dependencies: DepsSet,
#[serde(default)]
pub target: TargetDepsSet,
#[serde(default)]
pub features: FeatureSet,
#[serde(default)]
pub bin: Vec<Product>,
#[serde(default)]
pub bench: Vec<Product>,
#[serde(default)]
pub test: Vec<Product>,
#[serde(default)]
pub example: Vec<Product>,
pub lib: Option<Product>,
#[serde(default)]
pub profile: Profiles,
#[serde(default)]
pub badges: Badges,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "kebab-case")]
pub struct Workspace {
#[serde(default)]
members: Vec<String>,
#[serde(default)]
default_members: Vec<String>,
#[serde(default)]
exclude: Vec<String>,
}
fn default_true() -> bool {
true
}
impl Manifest<Value> {
pub fn from_slice(cargo_toml_content: &[u8]) -> Result<Self, Error> {
Self::from_slice_with_metadata(cargo_toml_content)
}
pub fn from_path(cargo_toml_path: impl AsRef<Path>) -> Result<Self, Error> {
Self::from_path_with_metadata(cargo_toml_path)
}
pub fn from_str(cargo_toml_content: &str) -> Result<Self, Error> {
Self::from_slice_with_metadata(cargo_toml_content.as_bytes())
}
}
impl<Metadata: for<'a> Deserialize<'a>> Manifest<Metadata> {
pub fn from_slice_with_metadata(cargo_toml_content: &[u8]) -> Result<Self, Error> {
let mut manifest: Self = toml::from_slice(cargo_toml_content)?;
if manifest.package.is_none() && manifest.workspace.is_none() {
let val: Value = toml::from_slice(cargo_toml_content)?;
if let Some(project) = val.get("project") {
manifest.package = Some(project.clone().try_into()?);
} else {
manifest.package = Some(val.try_into()?);
}
}
return Ok(manifest);
}
pub fn from_path_with_metadata(cargo_toml_path: impl AsRef<Path>) -> Result<Self, Error> {
let cargo_toml_path = cargo_toml_path.as_ref();
let cargo_toml_content = fs::read(cargo_toml_path)?;
let mut manifest = Self::from_slice_with_metadata(&cargo_toml_content)?;
manifest.complete_from_path(cargo_toml_path)?;
Ok(manifest)
}
pub fn complete_from_path(&mut self, path: &Path) -> Result<(), Error> {
let manifest_dir = path.parent().ok_or_else(|| io::Error::new(io::ErrorKind::Other, "bad path"))?;
self.complete_from_abstract_filesystem(Filesystem::new(manifest_dir))
}
pub fn complete_from_abstract_filesystem(&mut self, fs: impl AbstractFilesystem) -> Result<(), Error> {
if let Some(ref package) = self.package {
let src = fs.file_names_in("src")?;
if let Some(ref mut lib) = self.lib {
lib.required_features.clear(); } else if src.contains("lib.rs") {
self.lib = Some(Product {
name: Some(package.name.replace("-", "_")),
path: Some("src/lib.rs".to_string()),
edition: Some(package.edition),
crate_type: vec!["rlib".to_string()],
..Product::default()
})
}
if package.autobins && self.bin.is_empty() {
self.bin = self.autoset("src/bin", &fs);
if src.contains("main.rs") {
self.bin.push(Product {
name: Some(package.name.clone()),
path: Some("src/main.rs".to_string()),
edition: Some(package.edition),
..Product::default()
})
}
}
if package.autoexamples && self.example.is_empty() {
self.example = self.autoset("examples", &fs);
}
if package.autotests && self.test.is_empty() {
self.test = self.autoset("tests", &fs);
}
if package.autobenches && self.bench.is_empty() {
self.bench = self.autoset("benches", &fs);
}
}
Ok(())
}
fn autoset(&self, dir: &str, fs: &dyn AbstractFilesystem) -> Vec<Product> {
let mut out = Vec::new();
if let Some(ref package) = self.package {
if let Ok(bins) = fs.file_names_in(dir) {
for name in bins {
let rel_path = format!("{}/{}", dir, name);
if name.ends_with(".rs") {
out.push(Product {
name: Some(name.trim_end_matches(".rs").into()),
path: Some(rel_path),
edition: Some(package.edition),
..Product::default()
})
} else if let Ok(sub) = fs.file_names_in(&rel_path) {
if sub.contains("main.rs") {
out.push(Product {
name: Some(name.into()),
path: Some(rel_path + "/main.rs"),
edition: Some(package.edition),
..Product::default()
})
}
}
}
}
}
out
}
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct Profiles {
pub release: Option<Profile>,
pub dev: Option<Profile>,
pub test: Option<Profile>,
pub bench: Option<Profile>,
pub doc: Option<Profile>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Profile {
pub opt_level: Option<Value>,
pub debug: Option<Value>,
pub rpath: Option<bool>,
pub lto: Option<Value>,
pub debug_assertions: Option<bool>,
pub codegen_units: Option<u16>,
pub panic: Option<String>,
pub incremental: Option<bool>,
pub overflow_checks: Option<bool>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Product {
pub path: Option<String>,
pub name: Option<String>,
#[serde(default = "default_true")]
pub test: bool,
#[serde(default = "default_true")]
pub doctest: bool,
#[serde(default = "default_true")]
pub bench: bool,
#[serde(default = "default_true")]
pub doc: bool,
#[serde(default)]
pub plugin: bool,
#[serde(default)]
pub proc_macro: bool,
#[serde(default = "default_true")]
pub harness: bool,
#[serde(default)]
pub edition: Option<Edition>,
#[serde(default)]
pub required_features: Vec<String>,
#[serde(default)]
pub crate_type: Vec<String>,
}
impl Default for Product {
fn default() -> Self {
Self {
path: None,
name: None,
test: true,
doctest: true,
bench: true,
doc: true,
harness: true,
plugin: false,
proc_macro: false,
required_features: Vec::new(),
crate_type: Vec::new(),
edition: Some(Edition::default()),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Target {
#[serde(default)]
pub dependencies: DepsSet,
#[serde(default)]
pub dev_dependencies: DepsSet,
#[serde(default)]
pub build_dependencies: DepsSet,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Dependency {
Simple(String),
Detailed(DependencyDetail),
}
impl Dependency {
pub fn req(&self) -> &str {
match *self {
Dependency::Simple(ref v) => v,
Dependency::Detailed(ref d) => d.version.as_ref().map(|s| s.as_str()).unwrap_or("*"),
}
}
pub fn req_features(&self) -> &[String] {
match *self {
Dependency::Simple(_) => &[],
Dependency::Detailed(ref d) => &d.features,
}
}
pub fn optional(&self) -> bool {
match *self {
Dependency::Simple(_) => false,
Dependency::Detailed(ref d) => d.optional,
}
}
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct DependencyDetail {
pub version: Option<String>,
pub registry: Option<String>,
pub registry_index: Option<String>,
pub path: Option<String>,
pub git: Option<String>,
pub branch: Option<String>,
pub tag: Option<String>,
pub rev: Option<String>,
#[serde(default)]
pub features: Vec<String>,
#[serde(default)]
pub optional: bool,
pub default_features: Option<bool>,
pub package: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Package<Metadata = Value> {
pub name: String,
#[serde(default)]
pub edition: Edition,
pub version: String,
pub build: Option<Value>,
pub workspace: Option<String>,
#[serde(default)]
pub authors: Vec<String>,
pub links: Option<String>,
pub description: Option<String>,
pub homepage: Option<String>,
pub documentation: Option<String>,
pub readme: Option<String>,
#[serde(default)]
pub keywords: Vec<String>,
#[serde(default)]
pub categories: Vec<String>,
pub license: Option<String>,
#[serde(rename = "license-file")]
pub license_file: Option<String>,
pub repository: Option<String>,
pub metadata: Option<Metadata>,
#[serde(default = "default_true")]
pub autobins: bool,
#[serde(default = "default_true")]
pub autoexamples: bool,
#[serde(default = "default_true")]
pub autotests: bool,
#[serde(default = "default_true")]
pub autobenches: bool,
#[serde(default = "default_true")]
pub publish: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Badge {
repository: String,
#[serde(default = "default_master")]
branch: String,
service: Option<String>,
id: Option<String>,
project_name: Option<String>,
}
fn default_master() -> String {
"master".to_string()
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Badges {
#[serde(default)]
appveyor: Option<Badge>,
#[serde(default)]
circle_ci: Option<Badge>,
#[serde(default)]
gitlab: Option<Badge>,
#[serde(default)]
travis_ci: Option<Badge>,
#[serde(default)]
codecov: Option<Badge>,
#[serde(default)]
coveralls: Option<Badge>,
#[serde(default)]
is_it_maintained_issue_resolution: Option<Badge>,
#[serde(default)]
is_it_maintained_open_issues: Option<Badge>,
#[serde(default)]
maintenance: Maintenance,
}
#[derive(Debug, Copy, Default, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Maintenance {
status: MaintenanceStatus,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum MaintenanceStatus {
None,
ActivelyDeveloped,
PassivelyMaintained,
AsIs,
Experimental,
LookingForMaintainer,
Deprecated,
}
impl Default for MaintenanceStatus {
fn default() -> Self {
MaintenanceStatus::None
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum Edition {
#[serde(rename = "2015")]
E2015,
#[serde(rename = "2018")]
E2018,
}
impl Default for Edition {
fn default() -> Self {
Edition::E2015
}
}