|
| 1 | +//! User consent acquiring logic. |
| 2 | +
|
| 3 | +use std::io; |
| 4 | + |
| 5 | +use cargo_gpu_build::spirv_cache::{ |
| 6 | + command::CommandExecError, |
| 7 | + toolchain::{HaltToolchainInstallation, REQUIRED_TOOLCHAIN_COMPONENTS}, |
| 8 | + user_output, |
| 9 | +}; |
| 10 | +use crossterm::tty::IsTty as _; |
| 11 | + |
| 12 | +/// Halts the installation process of toolchain or its required components |
| 13 | +/// if the user does not consent to install either of them. |
| 14 | +#[expect( |
| 15 | + clippy::type_complexity, |
| 16 | + reason = "it is impossible to create an alias for now" |
| 17 | +)] |
| 18 | +pub fn ask_for_user_consent( |
| 19 | + skip: bool, |
| 20 | +) -> HaltToolchainInstallation< |
| 21 | + impl FnOnce(&str) -> Result<(), UserConsentError>, |
| 22 | + impl FnOnce(&str) -> Result<(), UserConsentError>, |
| 23 | +> { |
| 24 | + let on_toolchain_install = move |channel: &str| { |
| 25 | + let message = format!("Rust {channel} with `rustup`"); |
| 26 | + get_consent_for_toolchain_install(format!("Install {message}").as_ref(), skip)?; |
| 27 | + log::debug!("installing toolchain {channel}"); |
| 28 | + user_output!(io::stdout(), "Installing {message}\n").map_err(UserConsentError::IoWrite)?; |
| 29 | + Ok(()) |
| 30 | + }; |
| 31 | + let on_components_install = move |channel: &str| { |
| 32 | + let message = format!( |
| 33 | + "components {REQUIRED_TOOLCHAIN_COMPONENTS:?} for toolchain {channel} with `rustup`" |
| 34 | + ); |
| 35 | + get_consent_for_toolchain_install(format!("Install {message}").as_ref(), skip)?; |
| 36 | + log::debug!("installing required components of toolchain {channel}"); |
| 37 | + user_output!(io::stdout(), "Installing {message}\n").map_err(UserConsentError::IoWrite)?; |
| 38 | + Ok(()) |
| 39 | + }; |
| 40 | + |
| 41 | + HaltToolchainInstallation { |
| 42 | + on_toolchain_install, |
| 43 | + on_components_install, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/// Prompt user if they want to install a new Rust toolchain. |
| 48 | +fn get_consent_for_toolchain_install(prompt: &str, skip: bool) -> Result<(), UserConsentError> { |
| 49 | + if skip { |
| 50 | + return Ok(()); |
| 51 | + } |
| 52 | + |
| 53 | + if !io::stdout().is_tty() { |
| 54 | + log::error!("attempted to ask for consent when there's no TTY"); |
| 55 | + return Err(UserConsentError::NoTTY); |
| 56 | + } |
| 57 | + |
| 58 | + log::debug!("asking for consent to install the required toolchain"); |
| 59 | + crossterm::terminal::enable_raw_mode().map_err(UserConsentError::IoRead)?; |
| 60 | + user_output!(io::stdout(), "{prompt} [y/n]: ").map_err(UserConsentError::IoWrite)?; |
| 61 | + let mut input = crossterm::event::read().map_err(UserConsentError::IoRead)?; |
| 62 | + |
| 63 | + if let crossterm::event::Event::Key(crossterm::event::KeyEvent { |
| 64 | + code: crossterm::event::KeyCode::Enter, |
| 65 | + kind: crossterm::event::KeyEventKind::Release, |
| 66 | + .. |
| 67 | + }) = input |
| 68 | + { |
| 69 | + // In Powershell, programs will potentially observe the Enter key release after they started |
| 70 | + // (see crossterm#124). If that happens, re-read the input. |
| 71 | + input = crossterm::event::read().map_err(UserConsentError::IoRead)?; |
| 72 | + } |
| 73 | + crossterm::terminal::disable_raw_mode().map_err(UserConsentError::IoRead)?; |
| 74 | + |
| 75 | + if let crossterm::event::Event::Key(crossterm::event::KeyEvent { |
| 76 | + code: crossterm::event::KeyCode::Char('y'), |
| 77 | + .. |
| 78 | + }) = input |
| 79 | + { |
| 80 | + Ok(()) |
| 81 | + } else { |
| 82 | + Err(UserConsentError::UserDenied) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/// An error indicating that user consent were not acquired. |
| 87 | +#[derive(Debug, thiserror::Error)] |
| 88 | +pub enum UserConsentError { |
| 89 | + /// An error occurred while executing a command. |
| 90 | + #[error(transparent)] |
| 91 | + CommandExec(#[from] CommandExecError), |
| 92 | + /// No TTY detected, so can't ask for consent to install Rust toolchain. |
| 93 | + #[error("no TTY detected, so can't ask for consent to install Rust toolchain")] |
| 94 | + NoTTY, |
| 95 | + /// An I/O error occurred while reading user input. |
| 96 | + #[error("failed to read user input: {0}")] |
| 97 | + IoRead(#[source] io::Error), |
| 98 | + /// An I/O error occurred while writing user output. |
| 99 | + #[error("failed to write user output: {0}")] |
| 100 | + IoWrite(#[source] io::Error), |
| 101 | + /// User denied to install the required toolchain. |
| 102 | + #[error("user denied to install the required toolchain")] |
| 103 | + UserDenied, |
| 104 | +} |
0 commit comments