#[derive(Debug, Clone)]
pub struct ErrorContext {
pub table: Option<String>,
pub column: Option<String>,
pub conditions: Vec<String>,
pub operator_chain: Option<String>,
pub query: Option<String>,
}
impl std::fmt::Display for ErrorContext {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut parts = Vec::new();
if let Some(ref table) = self.table {
parts.push(format!("table: {}", table));
}
if let Some(ref column) = self.column {
parts.push(format!("column: {}", column));
}
if !self.conditions.is_empty() {
parts.push(format!("conditions: {}", self.conditions.join(" | ")));
}
if let Some(ref operator_chain) = self.operator_chain {
parts.push(format!("operator_chain: {}", operator_chain));
}
if let Some(ref query) = self.query {
parts.push(format!("query: {}", query));
}
write!(f, "{}", parts.join(", "))
}
}
impl ErrorContext {
pub fn new() -> Self {
Self {
table: None,
column: None,
conditions: Vec::new(),
operator_chain: None,
query: None,
}
}
pub fn table(mut self, table: impl Into<String>) -> Self {
self.table = Some(table.into());
self
}
pub fn column(mut self, column: impl Into<String>) -> Self {
self.column = Some(column.into());
self
}
pub fn condition(mut self, condition: impl Into<String>) -> Self {
self.conditions.push(condition.into());
self
}
pub fn conditions(mut self, conditions: Vec<String>) -> Self {
self.conditions = conditions;
self
}
pub fn operator_chain(mut self, operator_chain: impl Into<String>) -> Self {
self.operator_chain = Some(operator_chain.into());
self
}
pub fn query(mut self, query: impl Into<String>) -> Self {
self.query = Some(query.into());
self
}
}
impl Default for ErrorContext {
fn default() -> Self {
Self::new()
}
}