use std::fmt;
use thiserror::Error;
#[derive(Debug, Clone, Error)]
pub struct BuildError {
path: String,
message: String,
}
impl BuildError {
pub fn new(path: impl Into<String>, message: impl Into<String>) -> Self {
Self {
path: path.into(),
message: message.into(),
}
}
pub fn at(self, field: &str) -> Self {
let new_path = if self.path.is_empty() {
field.to_owned()
} else {
format!("{}.{}", field, self.path)
};
Self {
path: new_path,
message: self.message,
}
}
}
impl fmt::Display for BuildError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.path.is_empty() {
write!(f, "{}", self.message)
} else {
write!(f, "at `{}`: {}", self.path, self.message)
}
}
}
impl From<BuildError> for serde_json::Error {
fn from(e: BuildError) -> Self {
serde_json::Error::io(std::io::Error::new(
std::io::ErrorKind::InvalidData,
e.to_string(),
))
}
}
pub mod builders;
pub mod helpers;
pub use builders::{build_expression, build_predicate};
pub use helpers::{
opt_str, parse_binary_op, parse_comparison_op, parse_data_type, parse_join_type, req_arr,
req_obj, req_str, type_name,
};
#[cfg(test)]
mod tests;