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
use crate::{graph::PackageGraph, Error};
use cargo_metadata::CargoOpt;
use serde::{Deserialize, Serialize};
use std::{convert::TryFrom, io, path::PathBuf};
#[derive(Clone, Debug, Default)]
pub struct MetadataCommand {
inner: cargo_metadata::MetadataCommand,
}
impl MetadataCommand {
pub fn new() -> Self {
let mut inner = cargo_metadata::MetadataCommand::new();
inner.features(CargoOpt::AllFeatures);
Self { inner }
}
pub fn cargo_path(&mut self, path: impl Into<PathBuf>) -> &mut Self {
self.inner.cargo_path(path);
self
}
pub fn manifest_path(&mut self, path: impl Into<PathBuf>) -> &mut Self {
self.inner.manifest_path(path);
self
}
pub fn current_dir(&mut self, path: impl Into<PathBuf>) -> &mut Self {
self.inner.current_dir(path);
self
}
pub fn other_options(
&mut self,
options: impl IntoIterator<Item = impl Into<String>>,
) -> &mut Self {
self.inner
.other_options(options.into_iter().map(|s| s.into()).collect::<Vec<_>>());
self
}
pub fn exec(&self) -> Result<CargoMetadata, Error> {
let inner = self.inner.exec().map_err(Error::command_error)?;
Ok(CargoMetadata(inner))
}
pub fn build_graph(&self) -> Result<PackageGraph, Error> {
let metadata = self.exec()?;
metadata.build_graph()
}
}
impl TryFrom<MetadataCommand> for PackageGraph {
type Error = Error;
fn try_from(command: MetadataCommand) -> Result<Self, Self::Error> {
command.build_graph()
}
}
impl<'a> TryFrom<&'a MetadataCommand> for PackageGraph {
type Error = Error;
fn try_from(command: &'a MetadataCommand) -> Result<Self, Self::Error> {
command.build_graph()
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(transparent)]
pub struct CargoMetadata(pub(crate) cargo_metadata::Metadata);
impl CargoMetadata {
pub fn parse_json(json: impl AsRef<str>) -> Result<Self, Error> {
let inner = serde_json::from_str(json.as_ref()).map_err(Error::MetadataParseError)?;
Ok(Self(inner))
}
pub fn serialize(&self, writer: &mut impl io::Write) -> Result<(), Error> {
serde_json::to_writer(writer, &self.0).map_err(Error::MetadataSerializeError)
}
pub fn build_graph(self) -> Result<PackageGraph, Error> {
PackageGraph::from_metadata(self)
}
}
impl TryFrom<CargoMetadata> for PackageGraph {
type Error = Error;
fn try_from(metadata: CargoMetadata) -> Result<Self, Self::Error> {
metadata.build_graph()
}
}