use derive_builder::UninitializedFieldError;
use thiserror::Error;
use crate::http;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Credentials {
api_token: String,
}
impl Credentials {
pub fn from_api_token<T: Into<String>>(api_token: T) -> Self {
Self { api_token: api_token.into() }
}
pub fn api_token(&self) -> &str {
self.api_token.as_str()
}
}
pub type Result<T, E = Error> = core::result::Result<T, E>;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum Error {
#[cfg(feature = "cli")]
#[error(
"Exercism CLI config file not found - perhaps CLI application is not installed or configured?"
)]
ConfigNotFound,
#[cfg(feature = "cli")]
#[error("could not read Exercism CLI config file: {0:?}")]
ConfigReadError(#[from] std::io::Error),
#[cfg(feature = "cli")]
#[error("failed to parse Exercism CLI config file: {0:?}")]
ConfigParseError(#[from] serde_json::Error),
#[cfg(feature = "cli")]
#[error("Exercism CLI config file did not contain an API token")]
ApiTokenNotFoundInConfig,
#[error(transparent)]
BuildFailed(#[from] BuildError),
#[error("error while performing API request: {0:?}")]
ApiError(#[from] http::Error),
#[error("error while performing API request with retries: {0:?}")]
ApiRetryError(anyhow::Error),
}
impl From<UninitializedFieldError> for Error {
#[cfg_attr(coverage_nightly, coverage(off))]
fn from(_value: UninitializedFieldError) -> Self {
unreachable!("all fields should have had default values")
}
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum BuildError {
#[error("http client creation failed: {0:?}")]
HttpClientCreationFailed(#[from] http::Error),
}
impl From<http::middleware::Error> for Error {
fn from(value: http::middleware::Error) -> Self {
match value {
http::middleware::Error::Middleware(err) => Self::ApiRetryError(err),
http::middleware::Error::Reqwest(err) => err.into(),
}
}
}