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
//! Toolchain management for `WaterUI` CLI
use std::convert::Infallible;
pub mod cargo_helpers;
pub mod cmake;
pub mod doctor;
pub mod host;
pub mod linux;
pub mod meson;
pub mod rust;
pub mod sccache;
#[cfg(test)]
pub(crate) mod testing;
pub mod web;
pub mod windows_arm64_llvm;
pub(crate) mod winget;
pub use host::Host;
/// A toolchain that cannot be fixed automatically.
#[derive(Debug, Clone, thiserror::Error)]
#[error("Unfixable toolchain: {message}\nSuggestion: {suggestion}")]
pub struct UnfixableToolchain {
/// A message describing why the toolchain is unfixable.
message: String,
/// An suggestion for how to fix the toolchain manually.
suggestion: String,
}
impl UnfixableToolchain {
/// Create a new `UnfixableToolchain` with the given message and optional suggestion.
pub fn new(message: impl Into<String>, suggestion: impl Into<String>) -> Self {
Self {
message: message.into(),
suggestion: suggestion.into(),
}
}
/// Get the message describing why the toolchain is unfixable.
#[must_use]
pub fn message(&self) -> &str {
&self.message
}
/// Get the optional suggestion for how to fix the toolchain manually.
#[must_use]
pub fn suggestion(&self) -> &str {
&self.suggestion
}
}
/// Trait representing an installation plan for toolchain components.
pub trait Installation: Send + Sync {
/// The error type returned if installation fails.
type Error: Into<eyre::Report> + Send;
/// Execute the installation plan against `host`.
fn install(&self, host: &Host) -> impl Future<Output = Result<(), Self::Error>> + Send;
}
/// Optional installation step.
///
/// This is used by composite toolchains (e.g. tuples) to represent "install if missing".
impl<I: Installation> Installation for Option<I> {
type Error = eyre::Report;
async fn install(&self, host: &Host) -> Result<(), Self::Error> {
if let Some(install) = self {
install.install(host).await.map_err(Into::into)?;
}
Ok(())
}
}
/// An error indicating the state of the toolchain.
#[derive(Debug, Clone, thiserror::Error)]
pub enum ToolchainError<Install: Installation> {
/// The toolchain cannot be fixed automatically.
#[error("{0}")]
Unfixable(#[from] UnfixableToolchain),
/// The toolchain is missing components that can be installed.
#[error(
"Toolchain is missing components that can be fixed automatically. Run `water doctor --fix` for details."
)]
Fixable(Install),
}
impl<I: Installation> ToolchainError<I> {
/// Returns `true` if the toolchain can be fixed automatically.
#[must_use]
pub const fn is_fixable(&self) -> bool {
matches!(self, Self::Fixable(_))
}
/// Create a new `ToolchainError` indicating that the toolchain can be fixed automatically.
#[must_use]
pub const fn fixable(install: I) -> Self {
Self::Fixable(install)
}
/// Create a new `ToolchainError` indicating that the toolchain cannot be fixed automatically.
#[must_use]
pub fn unfixable(message: impl Into<String>, suggestion: impl Into<String>) -> Self {
Self::Unfixable(UnfixableToolchain::new(message, suggestion))
}
}
/// Trait for toolchain dependencies that can be checked and installed.
///
/// Implementors represent a specific toolchain configuration (e.g., Rust with
/// certain targets, Android SDK with specific components).
/// The associated `Installation` type preserves full type information through
/// the composition, enabling zero-cost abstractions for parallel/sequential
/// installation plans.
pub trait Toolchain: Send + Sync {
/// The installation type returned by `fix()`.
type Installation: Installation;
/// Check if the toolchain is properly installed on `host`.
///
/// Returns `Ok(())` if all components are available, or `Err` describing
/// what is missing.
fn check(
&self,
host: &Host,
) -> impl Future<Output = Result<(), ToolchainError<Self::Installation>>> + Send;
}
impl Installation for Infallible {
type Error = Self;
fn install(&self, _host: &Host) -> impl Future<Output = Result<(), Self::Error>> + Send {
std::future::poll_fn(|_| {
unreachable!("an Infallible installation plan cannot be constructed")
})
}
}
macro_rules! tuples {
($macro:ident) => {
$macro!(T0);
$macro!(T0, T1);
$macro!(T0, T1, T2);
$macro!(T0, T1, T2, T3);
$macro!(T0, T1, T2, T3, T4);
$macro!(T0, T1, T2, T3, T4, T5);
$macro!(T0, T1, T2, T3, T4, T5, T6);
$macro!(T0, T1, T2, T3, T4, T5, T6, T7);
$macro!(T0, T1, T2, T3, T4, T5, T6, T7, T8);
$macro!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9);
$macro!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10);
$macro!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11);
$macro!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12);
$macro!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13);
$macro!(
T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14
);
};
}
macro_rules! impl_installations {
($($ty:ident),*) => {
#[allow(unused_variables)]
#[allow(non_snake_case)]
impl<$($ty: Installation),*> Installation for ($($ty,)*) {
type Error = eyre::Report;
async fn install(&self, host: &Host) -> Result<(), Self::Error> {
let ($($ty,)*) = self;
$(
$ty.install(host).await.map_err(|e| e.into())?;
)*
Ok(())
}
}
};
}
impl Installation for () {
type Error = eyre::Report;
fn install(&self, _host: &Host) -> impl Future<Output = Result<(), Self::Error>> + Send {
std::future::ready(Ok(()))
}
}
tuples!(impl_installations);
macro_rules! impl_toolchains {
($(($idx:tt, $ty:ident)),*) => {
#[allow(unused_variables)]
#[allow(non_snake_case)]
impl<$($ty: Toolchain),*> Toolchain for ($($ty,)*) {
// Each slot is `Some(install)` if that component is missing-and-fixable.
// Components that are already OK produce `None` and are skipped during installation.
type Installation = ($(Option<$ty::Installation>,)*);
async fn check(&self, host: &Host) -> Result<(), ToolchainError<Self::Installation>> {
#[allow(unused_mut)]
let mut any_fixable = false;
#[allow(unused_mut)]
let mut installs: Self::Installation = ($(None::<$ty::Installation>,)*);
$(
match self.$idx.check(host).await {
Ok(()) => {}
Err(ToolchainError::Unfixable(u)) => {
return Err(ToolchainError::Unfixable(u));
}
Err(ToolchainError::Fixable(install)) => {
any_fixable = true;
installs.$idx = Some(install);
}
}
)*
if any_fixable {
Err(ToolchainError::Fixable(installs))
} else {
Ok(())
}
}
}
};
}
impl Toolchain for () {
type Installation = ();
fn check(
&self,
_host: &Host,
) -> impl Future<Output = Result<(), ToolchainError<Self::Installation>>> + Send {
std::future::ready(Ok(()))
}
}
macro_rules! tuples_idx {
($macro:ident) => {
$macro!((0, T0));
$macro!((0, T0), (1, T1));
$macro!((0, T0), (1, T1), (2, T2));
$macro!((0, T0), (1, T1), (2, T2), (3, T3));
$macro!((0, T0), (1, T1), (2, T2), (3, T3), (4, T4));
$macro!((0, T0), (1, T1), (2, T2), (3, T3), (4, T4), (5, T5));
$macro!(
(0, T0),
(1, T1),
(2, T2),
(3, T3),
(4, T4),
(5, T5),
(6, T6)
);
$macro!(
(0, T0),
(1, T1),
(2, T2),
(3, T3),
(4, T4),
(5, T5),
(6, T6),
(7, T7)
);
$macro!(
(0, T0),
(1, T1),
(2, T2),
(3, T3),
(4, T4),
(5, T5),
(6, T6),
(7, T7),
(8, T8)
);
$macro!(
(0, T0),
(1, T1),
(2, T2),
(3, T3),
(4, T4),
(5, T5),
(6, T6),
(7, T7),
(8, T8),
(9, T9)
);
$macro!(
(0, T0),
(1, T1),
(2, T2),
(3, T3),
(4, T4),
(5, T5),
(6, T6),
(7, T7),
(8, T8),
(9, T9),
(10, T10)
);
$macro!(
(0, T0),
(1, T1),
(2, T2),
(3, T3),
(4, T4),
(5, T5),
(6, T6),
(7, T7),
(8, T8),
(9, T9),
(10, T10),
(11, T11)
);
$macro!(
(0, T0),
(1, T1),
(2, T2),
(3, T3),
(4, T4),
(5, T5),
(6, T6),
(7, T7),
(8, T8),
(9, T9),
(10, T10),
(11, T11),
(12, T12)
);
$macro!(
(0, T0),
(1, T1),
(2, T2),
(3, T3),
(4, T4),
(5, T5),
(6, T6),
(7, T7),
(8, T8),
(9, T9),
(10, T10),
(11, T11),
(12, T12),
(13, T13)
);
$macro!(
(0, T0),
(1, T1),
(2, T2),
(3, T3),
(4, T4),
(5, T5),
(6, T6),
(7, T7),
(8, T8),
(9, T9),
(10, T10),
(11, T11),
(12, T12),
(13, T13),
(14, T14)
);
};
}
tuples_idx!(impl_toolchains);