mod bare;
mod convert;
mod decode;
mod endpoint;
mod json;
mod request;
mod shell;
mod snippet;
pub use convert::{convert, convert_stmts};
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConvertError {
InvalidJson(String),
UnsupportedEndpoint(String),
MissingCollection,
UndecodableBody {
detail: String,
},
InvalidField {
path: String,
detail: String,
},
InvalidLine {
line: usize,
source: Box<ConvertError>,
},
}
impl ConvertError {
pub(crate) fn invalid(path: impl Into<String>, detail: impl Into<String>) -> Self {
Self::InvalidField {
path: path.into(),
detail: detail.into(),
}
}
pub(crate) fn undecodable(detail: impl Into<String>) -> Self {
Self::UndecodableBody {
detail: detail.into(),
}
}
}
impl fmt::Display for ConvertError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidJson(e) => write!(f, "invalid JSON: {e}"),
Self::UnsupportedEndpoint(ep) => write!(f, "unsupported endpoint: {ep}"),
Self::MissingCollection => write!(
f,
"bare request body requires a collection (pass --collection)"
),
Self::UndecodableBody { detail } => write!(f, "cannot decode request body: {detail}"),
Self::InvalidField { path, detail } => write!(f, "invalid field '{path}': {detail}"),
Self::InvalidLine { line, source } => write!(f, "line {line}: {source}"),
}
}
}
impl std::error::Error for ConvertError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::InvalidLine { source, .. } => Some(source.as_ref()),
_ => None,
}
}
}