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
use crate::{Command, CommandExt, ResultExt};
use std::borrow::Borrow;
use std::cmp::Ordering;
use std::collections::BTreeSet;
use std::ffi::*;
use std::fmt::{self, Display, Formatter};
use std::hash::{Hash, Hasher};
use std::io;
use std::process::Stdio;
use std::sync::Arc;
pub struct Rustup { rustup: Arc<OsString> }
impl Rustup {
pub fn default() -> io::Result<Self> { Self::new("rustup") }
pub fn new(rustup: impl AsRef<OsStr> + Into<OsString>) -> io::Result<Self> {
let rustup = rustup.into();
Command::new(&rustup).arg("--version").stdout(|| Stdio::null()).stderr(|| Stdio::null()).status0()?;
Ok(Self { rustup: Arc::new(rustup.into()) })
}
pub fn new_unchecked(rustup: impl AsRef<OsStr> + Into<OsString>) -> Self {
let rustup = rustup.into();
Self { rustup: Arc::new(rustup.into()) }
}
#[cfg(feature = "version")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "version")))]
pub fn version(&self) -> crate::Version {
Command::new(self.rustup.as_os_str()).arg("--version").stderr(|| Stdio::null()).stdout0().unwrap().parse().unwrap()
}
pub fn is_available(&self) -> bool {
Command::new(self.rustup.as_os_str()).arg("--version").stdout(|| Stdio::null()).stderr(|| Stdio::null()).status().map_or(false, |c| c.code() == Some(0))
}
pub fn toolchains(&self) -> RustupToolchains {
RustupToolchains { rustup: &self.rustup }
}
}
pub struct RustupToolchains<'r> { rustup: &'r Arc<OsString> }
impl<'r> RustupToolchains<'r> {
pub fn active(&self) -> Option<Toolchain> {
let o = self.rustup(&["show", "active-toolchain"]).stdout0_no_stderr().ok()?;
let o = o.trim().split(' ').next()?;
Some(Toolchain { rustup: self.rustup.clone(), toolchain: Arc::new(o.into()) })
}
pub fn default(&self) -> Option<Toolchain> {
let o = self.rustup(&["default"]).stdout0_no_stderr().or_die();
let o = o.trim().split(' ').next()?;
Some(Toolchain { rustup: self.rustup.clone(), toolchain: Arc::new(o.into()) })
}
pub fn installed(&self) -> BTreeSet<Toolchain> {
let o = self.rustup(&["toolchain", "list"]).stdout0().or_die();
let mut r = BTreeSet::new();
for line in o.lines() {
let line = line.trim().split(' ').next().unwrap_or("");
if line.is_empty() { continue }
r.extend(Some(Toolchain { rustup: self.rustup.clone(), toolchain: Arc::new(line.into()) }));
}
r
}
pub fn get(&self, toolchain: impl AsRef<str>) -> Option<Toolchain> {
let toolchain = toolchain.as_ref();
let o = self.rustup(&[&format!("+{}", toolchain), "show", "active-toolchain"]).env_remove("RUSTUP_TOOLCHAIN").stdout0_no_stderr().ok()?;
let o = o.trim().split(' ').next()?;
Some(Toolchain { rustup: self.rustup.clone(), toolchain: Arc::new(o.into()) })
}
pub fn install(&self, toolchain: impl AsRef<str>) -> io::Result<Toolchain> {
self.rustup(&["toolchain", "install", toolchain.as_ref()]).stdout0()?;
self.get(toolchain.as_ref()).ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, format!("unable to find toolchain {}", toolchain.as_ref())))
}
pub fn uninstall(&self, toolchain: impl AsRef<str>) -> io::Result<()> {
self.rustup(&["toolchain", "install", toolchain.as_ref()]).status0()
}
fn rustup<I: IntoIterator<Item = S>, S: AsRef<OsStr>>(&self, args: I) -> Command {
let mut c = Command::new(self.rustup.as_os_str());
c.args(args);
c
}
}
#[derive(Clone, Debug)] pub struct Toolchain { rustup: Arc<OsString>, toolchain: Arc<String> }
impl AsRef<str> for Toolchain { fn as_ref(&self) -> &str { &self.toolchain } }
impl Borrow<str> for Toolchain { fn borrow(&self) -> &str { &self.toolchain } }
impl Display for Toolchain { fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { Display::fmt(&self.toolchain, fmt) } }
impl Eq for Toolchain {}
impl Hash for Toolchain { fn hash<H: Hasher>(&self, state: &mut H) { self.toolchain.hash(state) } }
impl Ord for Toolchain { fn cmp(&self, other: &Self) -> Ordering { self.toolchain.cmp(&other.toolchain) } }
impl PartialEq for Toolchain { fn eq(&self, other: &Self) -> bool { self.toolchain == other.toolchain } }
impl PartialOrd for Toolchain { fn partial_cmp(&self, other: &Self) -> Option<Ordering> { self.toolchain.partial_cmp(&other.toolchain) } }
impl Toolchain {
pub fn as_str(&self) -> &str { &self.toolchain }
pub fn targets(&self) -> ToolchainTargets { ToolchainTargets { toolchain: self } }
pub fn cargo(&self) -> Command { self.run("cargo") }
pub fn rustc(&self) -> Command { self.run("rustc") }
fn run(&self, command: &str) -> Command {
let mut c = Command::new(self.rustup.as_os_str());
c.arg("run");
c.arg(self.toolchain.as_str());
c.arg(command);
c
}
}
pub struct ToolchainTargets<'t> { toolchain: &'t Toolchain }
impl<'t> ToolchainTargets<'t> {
pub fn all(&self) -> BTreeSet<Target> {
let o = self.rustup(&["target", "list"]).stdout0().or_die();
let mut r = BTreeSet::new();
for line in o.lines() {
let line = line.trim().split(' ').next().unwrap_or("");
if line.is_empty() { continue }
r.extend(Some(Target(line.into())));
}
r
}
pub fn installed(&self) -> BTreeSet<Target> {
let o = self.rustup(&["target", "list", "--installed"]).stdout0().or_die();
let mut r = BTreeSet::new();
for line in o.lines() {
let line = line.trim().split(' ').next().unwrap_or("");
if line.is_empty() { continue }
r.extend(Some(Target(line.into())));
}
r
}
pub fn get(&self, target: impl AsRef<str>) -> Option<Target> {
self.installed().take(target.as_ref())
}
pub fn add(&self, target: impl AsRef<str>) -> io::Result<()> {
if self.get(target.as_ref()).is_some() { return Ok(()) }
self.rustup(&["target", "add", target.as_ref()]).status0()
}
pub fn remove(&self, target: impl AsRef<str>) -> io::Result<()> {
self.rustup(&["target", "remove", target.as_ref()]).status0()
}
fn rustup<I: IntoIterator<Item = S>, S: AsRef<OsStr>>(&self, args: I) -> Command {
let mut c = Command::new(self.toolchain.rustup.as_os_str());
c.args(args);
c.arg("--toolchain");
c.arg(self.toolchain.toolchain.as_str());
c
}
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Target(String);
impl AsRef<str> for Target { fn as_ref(&self) -> &str { &self.0 } }
impl Borrow<str> for Target { fn borrow(&self) -> &str { &self.0 } }
impl Display for Target { fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { Display::fmt(&self.0, fmt) } }
impl Target {
pub fn new(src: impl Into<Target>) -> Self { src.into() }
pub fn as_str(&self) -> &str { &self.0 }
}