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
#[repr(transparent)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CommandFailedOutput(pub Vec<u8>);
impl std::fmt::Display for CommandFailedOutput {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", String::from_utf8_lossy(&self.0))
}
}
impl std::fmt::Debug for CommandFailedOutput {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self, f)
}
}
impl From<CommandFailedOutput> for Vec<u8> {
#[inline(always)]
fn from(out: CommandFailedOutput) -> Self {
out.0
}
}
#[derive(Debug)]
pub enum Error {
SystemHTTPClientNotFound,
IoError(std::io::Error),
InvalidUrl(url::ParseError),
InvalidUrlScheme,
CommandFailed {
status: std::process::ExitStatus,
stdout: CommandFailedOutput,
stderr: CommandFailedOutput,
},
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::SystemHTTPClientNotFound => {
write!(f, "This system does not have an HTTP client installed")
}
Error::IoError(e) => write!(f, "I/O error: {}", e),
Error::InvalidUrl(e) => write!(f, "Invalid URL: {}", e),
Error::InvalidUrlScheme => write!(f, "URL must have http or https scheme"),
Error::CommandFailed { status, .. } => write!(f, "Process exited with code {status:?}"),
}
}
}
impl std::error::Error for Error {}
impl From<url::ParseError> for Error {
fn from(err: url::ParseError) -> Self {
Self::InvalidUrl(err)
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Self::IoError(err)
}
}