use std::{fmt, path::PathBuf};
use credent_model::{Credentials, Profiles};
#[derive(Debug)]
pub enum Error<C = Credentials>
where
C: Clone + Eq,
{
UserConfigDirNotFound,
CredentialsParentDirCreate {
parent_path: PathBuf,
error: std::io::Error,
},
CredentialsFileNonExistent {
credentials_path: PathBuf,
},
CredentialsFileIsDir {
credentials_path: PathBuf,
},
CredentialsFileRead {
credentials_path: PathBuf,
error: std::io::Error,
},
CredentialsFileWrite {
credentials_path: PathBuf,
error: std::io::Error,
},
CredentialsFileDeserialize {
credentials_path: PathBuf,
error: toml::de::Error,
},
CredentialsFileSerialize {
profiles: Profiles<C>,
error: toml::ser::Error,
},
}
impl<C> fmt::Display for Error<C>
where
C: Clone + Eq + fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::UserConfigDirNotFound => {
write!(f, "Unable to determine user configuration directory.")
}
Self::CredentialsParentDirCreate { parent_path, .. } => write!(
f,
"Failed to create credentials file parent directory. Path: `{}`",
parent_path.display()
),
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::CredentialsFileRead {
credentials_path, ..
} => write!(
f,
"User credentials file failed to be read. Path: `{}`",
credentials_path.display()
),
Self::CredentialsFileWrite {
credentials_path, ..
} => write!(
f,
"User credentials file failed to be read. Path: `{}`",
credentials_path.display()
),
Self::CredentialsFileDeserialize {
credentials_path, ..
} => write!(
f,
"User credentials file failed to be deserialized. Path: `{}`",
credentials_path.display()
),
Self::CredentialsFileSerialize { profiles, .. } => write!(
f,
"User credentials failed to be serialized. Profiles: `{:?}`",
profiles
),
}
}
}
impl<C> std::error::Error for Error<C>
where
C: Clone + Eq + fmt::Debug,
{
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::UserConfigDirNotFound => None,
Self::CredentialsParentDirCreate { error, .. } => Some(error),
Self::CredentialsFileNonExistent { .. } => None,
Self::CredentialsFileIsDir { .. } => None,
Self::CredentialsFileRead { error, .. } => Some(error),
Self::CredentialsFileWrite { error, .. } => Some(error),
Self::CredentialsFileDeserialize { error, .. } => Some(error),
Self::CredentialsFileSerialize { error, .. } => Some(error),
}
}
}