const WINGET_INSTALLER_FAILED: u32 = 0x8A15_0006;
const WINGET_ALREADY_INSTALLED: u32 = 0x8A15_002B;
const WINGET_RESTART_TO_FINISH: u32 = 0x8A15_0109;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Known {
Cancelled,
AnotherInstall,
NoDiskSpace,
NoInternet,
RestartFirst,
Policy,
AgreementToAccept,
InstallerError,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Done {
AlreadyInstalled,
RestartToFinish,
}
fn winget_summary(code: u32) -> Option<Known> {
Some(match code {
0x8A15_010C => Known::Cancelled,
0x8A15_0102 => Known::AnotherInstall,
0x8A15_0105 => Known::NoDiskSpace,
0x8A15_0107 => Known::NoInternet,
0x8A15_010A => Known::RestartFirst,
0x8A15_010F => Known::Policy,
0x8A15_0041 | 0x8A15_0046 => Known::AgreementToAccept,
WINGET_INSTALLER_FAILED => Known::InstallerError,
_ => return None,
})
}
pub(crate) fn winget_done(code: Option<i32>) -> Option<Done> {
match code?.cast_unsigned() {
WINGET_ALREADY_INSTALLED => Some(Done::AlreadyInstalled),
WINGET_RESTART_TO_FINISH => Some(Done::RestartToFinish),
_ => None,
}
}
fn installer_cancelled(code: u32) -> bool {
matches!(
code,
1223 | 1602 | 0x8007_04C7 | 0xFFFF_FFFB )
}
pub(crate) fn installer_exit_code(line: &str) -> Option<u32> {
let rest = line.trim().strip_prefix("Installer failed with exit code:")?.trim();
match rest.strip_prefix("0x").or_else(|| rest.strip_prefix("0X")) {
Some(hex) => u32::from_str_radix(hex, 16).ok(),
None => rest.parse::<u32>().ok().or_else(|| rest.parse::<i32>().ok().map(i32::cast_unsigned)),
}
}
pub(crate) fn winget_failure(code: Option<i32>, installer_code: Option<u32>) -> Option<Known> {
let code = code?.cast_unsigned();
match installer_code {
Some(c) if code == WINGET_INSTALLER_FAILED && installer_cancelled(c) => Some(Known::Cancelled),
_ => winget_summary(code),
}
}
pub(crate) fn installer_failure(code: Option<i32>) -> Option<Known> {
installer_cancelled(code?.cast_unsigned()).then_some(Known::Cancelled)
}
#[cfg(test)]
mod tests;