#![deny(missing_docs)]
#[derive(thiserror::Error, Debug)]
#[repr(u8)]
pub enum NysmError {
#[error("Unable to parse data as json")]
SerdeJson(#[from] serde_json::Error),
#[error("Unable to parse data as yaml")]
SerdeYaml(#[from] serde_yml::Error),
#[error("Unable to pretty print data")]
BatPrint(#[from] bat::error::Error),
#[error("Unable to read/write file caused by: {}", .0)]
IO(#[from] std::io::Error),
#[error("Failed to list secrets: {0}")]
ListSecretsFailed(String),
#[error("Failed to retrieve secret value: {0}")]
GetSecretValueFailed(String),
#[error("Secret does not support read operations")]
SecretNotReadable,
#[error("Failed to update secret: {0}")]
UpdateSecretFailed(String),
#[error("Failed to create secret: {0}")]
CreateSecretFailed(String),
#[error("Failed to delete secret: {0}")]
DeleteSecretFailed(String),
#[error("Authentication failed: {0}")]
AuthenticationFailed(String),
#[error("Invalid configuration: {0}")]
InvalidConfiguration(String),
}
impl PartialEq for NysmError {
fn eq(&self, other: &Self) -> bool {
std::mem::discriminant(self) == std::mem::discriminant(other)
}
}
#[cfg(not(tarpaulin_include))]
#[cfg(test)]
mod tests {
use super::*;
use serde::de::Error;
#[test]
fn test_serde_json_error() {
let error = NysmError::SerdeJson(serde_json::Error::custom("custom error"));
assert_eq!(error.to_string(), "Unable to parse data as json");
}
#[test]
fn test_serde_yaml_error() {
let error = NysmError::SerdeYaml(serde_yml::Error::custom("custom error"));
assert_eq!(error.to_string(), "Unable to parse data as yaml");
}
#[test]
fn test_bat_print_error() {
let error = NysmError::BatPrint(Into::<bat::error::Error>::into("custom error"));
assert_eq!(error.to_string(), "Unable to pretty print data");
}
#[test]
fn test_io_error() {
let error = NysmError::IO(std::io::Error::new(
std::io::ErrorKind::Other,
"custom error",
));
assert_eq!(
error.to_string(),
"Unable to read/write file caused by: custom error"
);
}
#[test]
fn test_list_secrets_failed_error() {
let error = NysmError::ListSecretsFailed("connection timeout".to_string());
assert_eq!(
error.to_string(),
"Failed to list secrets: connection timeout"
);
}
#[test]
fn test_get_secret_value_failed_error() {
let error = NysmError::GetSecretValueFailed("secret not found".to_string());
assert_eq!(
error.to_string(),
"Failed to retrieve secret value: secret not found"
);
}
#[test]
fn test_secret_not_readable_error() {
let error = NysmError::SecretNotReadable;
assert_eq!(error.to_string(), "Secret does not support read operations");
}
#[test]
fn test_update_secret_failed_error() {
let error = NysmError::UpdateSecretFailed("permission denied".to_string());
assert_eq!(
error.to_string(),
"Failed to update secret: permission denied"
);
}
#[test]
fn test_create_secret_failed_error() {
let error = NysmError::CreateSecretFailed("secret already exists".to_string());
assert_eq!(
error.to_string(),
"Failed to create secret: secret already exists"
);
}
#[test]
fn test_delete_secret_failed_error() {
let error = NysmError::DeleteSecretFailed("secret in use".to_string());
assert_eq!(error.to_string(), "Failed to delete secret: secret in use");
}
#[test]
fn test_authentication_failed_error() {
let error = NysmError::AuthenticationFailed("invalid token".to_string());
assert_eq!(error.to_string(), "Authentication failed: invalid token");
}
#[test]
fn test_invalid_configuration_error() {
let error = NysmError::InvalidConfiguration("missing repository name".to_string());
assert_eq!(
error.to_string(),
"Invalid configuration: missing repository name"
);
}
#[test]
fn test_partial_eq() {
let error1 = NysmError::SerdeJson(serde_json::Error::custom("custom error"));
let error2 = NysmError::SerdeJson(serde_json::Error::custom("custom error"));
let error3 = NysmError::SerdeYaml(serde_yml::Error::custom("different error"));
assert_eq!(error1, error2);
assert_ne!(error1, error3);
}
}