cargo_quickinstall/
install_error.rs1use crate::{CommandFailed, CrateDetails, JsonExtError};
2use std::fmt::{Debug, Display};
3
4use tinyjson::JsonParseError;
5
6pub enum InstallError {
7 MissingCrateNameArgument(&'static str),
8 CommandFailed(CommandFailed),
9 IoError(std::io::Error),
10 CargoInstallFailed,
11 CrateDoesNotExist { crate_name: String },
12 NoFallback(CrateDetails),
13 InvalidJson { url: String, err: JsonParseError },
14 JsonErr(JsonExtError),
15 FailToParseRustcOutput { reason: &'static str },
16}
17
18impl InstallError {
19 pub fn is_curl_404(&self) -> bool {
20 matches!(
21 self,
22 Self::CommandFailed(CommandFailed { stderr, .. })
23 if stderr.contains("The requested URL returned error: 404")
24 )
25 }
26}
27
28impl std::error::Error for InstallError {
29 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
30 match self {
31 Self::IoError(io_err) => Some(io_err),
32 Self::InvalidJson { err, .. } => Some(err),
33 Self::JsonErr(err) => Some(err),
34 _ => None,
35 }
36 }
37}
38
39impl Debug for InstallError {
42 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
43 Display::fmt(self, f)
44 }
45}
46
47impl Display for InstallError {
48 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
49 match self {
50 &InstallError::MissingCrateNameArgument(usage_text) => {
51 write!(f, "No crate name specified.\n\n{usage_text}")
52 }
53 InstallError::CommandFailed(CommandFailed {
54 command,
55 stdout,
56 stderr,
57 }) => {
58 write!(f, "Command failed:\n {command}\n")?;
59 if !stdout.is_empty() {
60 write!(f, "Stdout:\n{stdout}\n")?;
61 }
62 if !stderr.is_empty() {
63 write!(f, "Stderr:\n{stderr}")?;
64 }
65
66 Ok(())
67 }
68 InstallError::IoError(e) => Display::fmt(e, f),
69 InstallError::CargoInstallFailed => {
70 f.write_str("`cargo install` didn't work either. Looks like you're on your own.")
71 }
72
73 InstallError::CrateDoesNotExist { crate_name } => {
74 write!(f, "`{crate_name}` does not exist on crates.io.")
75 }
76 InstallError::NoFallback(crate_details) => {
77 write!(
78 f,
79 "Could not find a pre-built package for {} {} on {}.",
80 crate_details.crate_name, crate_details.version, crate_details.target
81 )
82 }
83 InstallError::InvalidJson { url, err } => {
84 write!(f, "Failed to parse json downloaded from '{url}': {err}",)
85 }
86 InstallError::JsonErr(err) => write!(f, "{err}"),
87 InstallError::FailToParseRustcOutput { reason } => {
88 write!(f, "Failed to parse `rustc -vV` output: {reason}")
89 }
90 }
91 }
92}
93
94impl From<std::io::Error> for InstallError {
95 fn from(err: std::io::Error) -> InstallError {
96 InstallError::IoError(err)
97 }
98}
99impl From<CommandFailed> for InstallError {
100 fn from(err: CommandFailed) -> InstallError {
101 InstallError::CommandFailed(err)
102 }
103}
104
105impl From<JsonExtError> for InstallError {
106 fn from(err: JsonExtError) -> InstallError {
107 InstallError::JsonErr(err)
108 }
109}
110
111#[derive(Debug)]
112pub enum InstallSuccess {
113 InstalledFromTarball,
114 BuiltFromSource,
115}
116
117pub fn install_result_to_status_str(result: &Result<InstallSuccess, InstallError>) -> &'static str {
130 match result {
131 Ok(InstallSuccess::InstalledFromTarball) => "installed-from-tarball",
132 Ok(InstallSuccess::BuiltFromSource) => "built-from-source",
133 Err(InstallError::CargoInstallFailed) => "cargo-install-failed",
134 Err(InstallError::NoFallback(_)) => "no-fallback",
135 Err(InstallError::MissingCrateNameArgument(_))
136 | Err(InstallError::CommandFailed(_))
137 | Err(InstallError::IoError(_))
138 | Err(InstallError::CrateDoesNotExist { .. })
139 | Err(InstallError::InvalidJson { .. })
140 | Err(InstallError::JsonErr(_))
141 | Err(InstallError::FailToParseRustcOutput { .. }) => "other-error",
142 }
143}