use std::{
fmt::{self, Display},
path::PathBuf,
};
use credent_model::Profiles;
#[derive(Debug)]
pub enum Error {
UserConfigDirNotFound,
CredentialsParentDirFailedToCreate {
parent_path: PathBuf,
io_error: std::io::Error,
},
CredentialsFileNonExistent {
credentials_path: PathBuf,
},
CredentialsFileIsDir {
credentials_path: PathBuf,
},
CredentialsFileFailedToRead {
credentials_path: PathBuf,
io_error: std::io::Error,
},
CredentialsFileFailedToWrite {
credentials_path: PathBuf,
io_error: std::io::Error,
},
CredentialsFileFailedToDeserialize {
credentials_path: PathBuf,
toml_de_error: toml::de::Error,
},
CredentialsFileFailedToSerialize {
profiles: Profiles,
toml_ser_error: toml::ser::Error,
},
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::UserConfigDirNotFound => {
write!(f, "Unable to determine user configuration directory.")
}
Self::CredentialsParentDirFailedToCreate {
parent_path,
io_error,
} => write!(
f,
"Failed to create credentials file parent directory.\n\
Path: `{}`\n\
Error: `{}`",
parent_path.display(),
io_error
),
Self::CredentialsFileNonExistent { credentials_path } => write!(
f,
"User credentials does not exist or cannot be accessed. Path: `{}`",
credentials_path.display()
),
Self::CredentialsFileIsDir { credentials_path } => write!(
f,
"User credentials file should be a file, but it is a directory. Path: `{}`",
credentials_path.display()
),
Self::CredentialsFileFailedToRead {
credentials_path,
io_error,
} => write!(
f,
"User credentials file failed to be read.\n\
Path: `{}`\n\
Error: `{}`",
credentials_path.display(),
io_error
),
Self::CredentialsFileFailedToWrite {
credentials_path,
io_error,
} => write!(
f,
"User credentials file failed to be read.\n\
Path: `{}`\n\
Error: `{}`",
credentials_path.display(),
io_error
),
Self::CredentialsFileFailedToDeserialize {
credentials_path,
toml_de_error,
} => write!(
f,
"User credentials file failed to be deserialized.\n\
Path: `{}`\n\
Error: `{}`",
credentials_path.display(),
toml_de_error
),
Self::CredentialsFileFailedToSerialize {
profiles,
toml_ser_error,
} => write!(
f,
"User credentials failed to be serialized.\n\
Profiles: `{:?}`\n\
Error: `{}`",
profiles, toml_ser_error
),
}
}
}
impl std::error::Error for Error {}