-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathcargo_metadata.rs
159 lines (140 loc) · 4.35 KB
/
cargo_metadata.rs
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
use crate::error::Result;
use rustwide::{cmd::Command, Toolchain, Workspace};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::path::Path;
pub(crate) struct CargoMetadata {
packages: HashMap<String, Package>,
deps_graph: HashMap<String, HashSet<String>>,
root_id: String,
}
impl CargoMetadata {
pub(crate) fn load(
workspace: &Workspace,
toolchain: &Toolchain,
source_dir: &Path,
) -> Result<Self> {
let res = Command::new(workspace, toolchain.cargo())
.args(&["metadata", "--format-version", "1"])
.cd(source_dir)
.log_output(false)
.run_capture()?;
let mut iter = res.stdout_lines().iter();
let metadata = if let (Some(serialized), None) = (iter.next(), iter.next()) {
serde_json::from_str::<DeserializedMetadata>(serialized)?
} else {
return Err(::failure::err_msg(
"invalid output returned by `cargo metadata`",
));
};
// Convert from Vecs to HashMaps and HashSets to get more efficient lookups
Ok(CargoMetadata {
packages: metadata
.packages
.into_iter()
.map(|pkg| (pkg.id.clone(), pkg))
.collect(),
deps_graph: metadata
.resolve
.nodes
.into_iter()
.map(|node| (node.id, node.deps.into_iter().map(|d| d.pkg).collect()))
.collect(),
root_id: metadata.resolve.root,
})
}
pub(crate) fn root_dependencies(&self) -> Vec<&Package> {
let ids = &self.deps_graph[&self.root_id];
self.packages
.iter()
.filter(|(id, _pkg)| ids.contains(id.as_str()))
.map(|(_id, pkg)| pkg)
.collect()
}
pub(crate) fn root(&self) -> &Package {
&self.packages[&self.root_id]
}
}
#[derive(Deserialize, Serialize)]
pub(crate) struct Package {
pub(crate) id: String,
pub(crate) name: String,
pub(crate) version: String,
pub(crate) license: Option<String>,
pub(crate) repository: Option<String>,
pub(crate) homepage: Option<String>,
pub(crate) description: Option<String>,
pub(crate) documentation: Option<String>,
pub(crate) dependencies: Vec<Dependency>,
pub(crate) targets: Vec<Target>,
pub(crate) readme: Option<String>,
pub(crate) keywords: Vec<String>,
pub(crate) authors: Vec<String>,
pub(crate) features: HashMap<String, Vec<String>>,
}
impl Package {
fn library_target(&self) -> Option<&Target> {
self.targets
.iter()
.find(|target| target.crate_types.iter().any(|kind| kind != "bin"))
}
pub(crate) fn is_library(&self) -> bool {
self.library_target().is_some()
}
fn normalize_package_name(&self, name: &str) -> String {
name.replace('-', "_")
}
pub(crate) fn package_name(&self) -> String {
self.library_name()
.unwrap_or_else(|| self.normalize_package_name(&self.targets[0].name))
}
pub(crate) fn library_name(&self) -> Option<String> {
self.library_target()
.map(|target| self.normalize_package_name(&target.name))
}
}
#[derive(Deserialize, Serialize)]
pub(crate) struct Target {
pub(crate) name: String,
#[cfg(not(test))]
crate_types: Vec<String>,
#[cfg(test)]
pub(crate) crate_types: Vec<String>,
pub(crate) src_path: Option<String>,
}
impl Target {
#[cfg(test)]
pub(crate) fn dummy_lib(name: String, src_path: Option<String>) -> Self {
Target {
name,
crate_types: vec!["lib".into()],
src_path,
}
}
}
#[derive(Deserialize, Serialize)]
pub(crate) struct Dependency {
pub(crate) name: String,
pub(crate) req: String,
pub(crate) kind: Option<String>,
pub(crate) optional: bool,
}
#[derive(Deserialize, Serialize)]
struct DeserializedMetadata {
packages: Vec<Package>,
resolve: DeserializedResolve,
}
#[derive(Deserialize, Serialize)]
struct DeserializedResolve {
root: String,
nodes: Vec<DeserializedResolveNode>,
}
#[derive(Deserialize, Serialize)]
struct DeserializedResolveNode {
id: String,
deps: Vec<DeserializedResolveDep>,
}
#[derive(Deserialize, Serialize)]
struct DeserializedResolveDep {
pkg: String,
}