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
#![deny(missing_docs)]
extern crate cargo;
#[macro_use]
extern crate log;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate toml as toml_crate;
#[macro_use]
extern crate failure;
use cargo::core::Package;
use cargo::core::Workspace;
use cargo::util::{paths, toml};
use cargo::util::Config;
use cargo::util::important_paths::find_root_manifest_for_wd;
use toml_crate::Value;
use serde::de::DeserializeOwned;
pub mod error {
pub type Result<T> = ::std::result::Result<T, ::failure::Error>;
}
use error::*;
#[derive(Deserialize, Debug)]
#[serde(rename_all = "kebab-case")]
pub struct PackConfig {
pub files: Option<Vec<String>>,
pub default_packers: Option<Vec<String>>,
}
pub struct CargoPack<'cfg> {
ws: Workspace<'cfg>,
package_name: Option<String>,
pack_config: PackConfig,
}
fn lookup(mut value: Value, path: &[&str]) -> Option<Value> {
for key in path {
match value {
Value::Table(mut hm) => {
match hm.remove(*key) {
Some(v) => value = v,
None => return None,
}
}
Value::Array(mut v) => {
match key.parse::<usize>().ok() {
Some(idx) if idx < v.len() => value = v.remove(idx),
_ => return None,
}
}
_ => return None,
}
}
Some(value)
}
impl<'cfg> CargoPack<'cfg> {
pub fn new<'a, P: Into<Option<String>>>(config: &'cfg Config, package_name: P) -> Result<Self> {
let package_name = package_name.into();
let root = find_root_manifest_for_wd(None, config.cwd())?;
let ws: Workspace<'cfg> = Workspace::new(&root, config)?;
let pack_config: PackConfig =
Self::decode_from_manifest_static(&ws, package_name.as_ref().map(|s| s.as_ref()))?;
debug!("config: {:?}", pack_config);
Ok(CargoPack {
ws: ws,
pack_config: pack_config,
package_name: package_name,
})
}
pub fn ws(&self) -> &Workspace<'cfg> {
&self.ws
}
pub fn config(&self) -> &PackConfig {
&self.pack_config
}
pub fn package(&self) -> Result<&Package> {
Self::find_package(self.ws(), self.package_name.as_ref().map(AsRef::as_ref))
}
fn find_package<'a, 'b>(
ws: &'a Workspace,
package_name: Option<&'b str>,
) -> Result<&'a Package> {
if let Some(ref name) = package_name {
let packages = ws
.members()
.filter(|p| p.package_id().name() == *name)
.collect::<Vec<_>>();
match packages.len() {
0 => return Err(format_err!("unknown package {}", name)),
1 => Ok(packages[0]),
_ => return Err(format_err!("ambiguous name {}", name)),
}
} else {
Ok(ws.current()?)
}
}
fn decode_from_manifest_static<T: DeserializeOwned>(
ws: &Workspace,
package_name: Option<&str>,
) -> Result<T> {
let manifest = Self::find_package(ws, package_name)?.manifest_path();
debug!("reading manifest: {:?}", manifest);
let contents = paths::read(manifest)?;
let root = toml::parse(&contents, &manifest, ws.config())?;
debug!("root: {:?}", root);
let data = lookup(root, &["package", "metadata", "pack"])
.expect("no package.metadata.pack found in Cargo.toml");
data.try_into().map_err(Into::into)
}
pub fn decode_from_manifest<'a, T: DeserializeOwned>(&self) -> Result<T> {
let package_name = self.package_name.as_ref().map(|s| s.as_ref());
Self::decode_from_manifest_static(self.ws(), package_name)
}
pub fn files(&self) -> &[String] {
self.pack_config
.files
.as_ref()
.map(AsRef::as_ref)
.unwrap_or(&[])
}
}