use smol_str::SmolStr;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(
"curl is required to fetch GitHub SSH keys; install curl or switch the entry to type = \"manual\""
)]
CurlNotFound,
#[error("Failed to fetch SSH keys for GitHub user {username}: {source}")]
GithubKeyFetchFailed {
username: SmolStr,
#[source]
source: xshell::Error,
},
#[error("Failed to parse SSH signing keys response for GitHub user {username}: {source}")]
GithubKeyParseFailed {
username: SmolStr,
#[source]
source: serde_json::Error,
},
}
impl PartialEq for Error {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::CurlNotFound, Self::CurlNotFound) => true,
(
Self::GithubKeyFetchFailed {
username: l_user,
source: l_src,
},
Self::GithubKeyFetchFailed {
username: r_user,
source: r_src,
},
) => l_user == r_user && l_src.to_string() == r_src.to_string(),
(
Self::GithubKeyParseFailed {
username: l_user,
source: l_src,
},
Self::GithubKeyParseFailed {
username: r_user,
source: r_src,
},
) => l_user == r_user && l_src.to_string() == r_src.to_string(),
_ => false,
}
}
}
#[cfg(test)]
mod tests {
use similar_asserts::assert_eq;
use smol_str::SmolStr;
use xshell::{Shell, cmd};
use super::*;
fn xshell_err() -> xshell::Error {
let sh = Shell::new().unwrap();
cmd!(sh, "false").quiet().run().unwrap_err()
}
fn json_err() -> serde_json::Error {
serde_json::from_str::<serde_json::Value>("{").unwrap_err()
}
#[test]
fn curl_not_found_eq() {
assert_eq!(Error::CurlNotFound, Error::CurlNotFound);
}
#[test]
fn github_key_fetch_failed_eq_and_ne() {
let a = Error::GithubKeyFetchFailed {
username: SmolStr::new_static("u"),
source: xshell_err(),
};
let b = Error::GithubKeyFetchFailed {
username: SmolStr::new_static("u"),
source: xshell_err(),
};
let c = Error::GithubKeyFetchFailed {
username: SmolStr::new_static("v"),
source: xshell_err(),
};
assert_eq!(a, b);
assert_ne!(a, c);
}
#[test]
fn github_key_parse_failed_eq_and_ne() {
let a = Error::GithubKeyParseFailed {
username: SmolStr::new_static("u"),
source: json_err(),
};
let b = Error::GithubKeyParseFailed {
username: SmolStr::new_static("u"),
source: json_err(),
};
let c = Error::GithubKeyParseFailed {
username: SmolStr::new_static("v"),
source: json_err(),
};
assert_eq!(a, b);
assert_ne!(a, c);
}
}