use std::{collections::HashSet, path::PathBuf};
macro_rules! impl_err_from {
($err:ident :: $type:ty > $variant:ident) => {
impl From<$type> for $err {
fn from(s: $type) -> Self {
$err::$variant(s)
}
}
};
}
pub type PaperClipResult<T> = Result<T, PaperClipError>;
#[derive(Debug, thiserror::Error)]
pub enum PaperClipError {
#[error("{}", _0)]
Validation(paperclip_core::ValidationError),
#[error("Cannot generate code in the given directory")]
InvalidCodegenDirectory,
#[error("This version of OpenAPI is unsupported.")]
#[allow(clippy::upper_case_acronyms)]
UnsupportedOpenAPIVersion,
#[error("Path similar to {:?} already exists.", _0)]
RelativePathNotUnique(String),
#[error("Parameter(s) {:?} aren't defined for templated path {:?}", _1, _0)]
MissingParametersInPath(String, HashSet<String>),
#[error("Cannot parse host {:?}: {}", _0, _1)]
InvalidHost(String, url_dep::ParseError),
#[error("Cannot set URL {:?}: {}", _0, _1)]
#[allow(clippy::upper_case_acronyms)]
InvalidBasePathURL(String, url_dep::ParseError),
#[error("Missing item schema for array: {:?}", _0)]
MissingArrayItem(Option<String>),
#[error("Invalid name for definition: '{0}'")]
InvalidDefinitionName(String),
#[error("Missing name for definition")]
MissingDefinitionName,
#[error("Invalid path for definition: {:?}", _0)]
InvalidDefinitionPath(PathBuf),
#[error("I/O error: {}", _0)]
Io(std::io::Error),
#[error("JSON error: {}", _0)]
Json(serde_json::Error),
#[error("YAML error: {}", _0)]
Yaml(serde_yaml::Error),
#[cfg(feature = "codegen-fmt")]
#[error("Rustfmt formatting error: {}", _0)]
RustFmt(rustfmt_nightly::ErrorKind),
#[cfg(feature = "codegen")]
#[error("Templating error: {}", _0)]
Templating(tinytemplate::error::Error),
}
impl_err_from!(PaperClipError::std::io::Error > Io);
impl_err_from!(PaperClipError::serde_json::Error > Json);
impl_err_from!(PaperClipError::serde_yaml::Error > Yaml);
impl_err_from!(PaperClipError::paperclip_core::ValidationError > Validation);
#[cfg(feature = "codegen-fmt")]
impl_err_from!(PaperClipError::rustfmt_nightly::ErrorKind > RustFmt);
#[cfg(feature = "codegen")]
impl_err_from!(PaperClipError::tinytemplate::error::Error > Templating);