use std::{fmt, panic::Location};
use sea_orm::{DbBackend, Statement, Value};
use crate::{
classify::StmtKind,
expectation::describe,
matcher::{Arg, SqlMatcher},
};
#[derive(Clone)]
pub(crate) enum Mismatch {
Kind { expected: StmtKind, actual: StmtKind },
Table { expected: String, actual: Option<String>, backend: DbBackend },
Sql { matcher: Box<SqlMatcher>, hint: Option<&'static str> },
Args { expected: Vec<Arg>, actual: Vec<Value>, wrong: Vec<usize> },
}
impl fmt::Display for Mismatch {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Mismatch::Kind { expected, actual } => {
let article = if matches!(expected, StmtKind::Insert | StmtKind::Update) { "an" } else { "a" };
write!(f, "expected {article} {expected} statement, got {actual}")
}
Mismatch::Table { expected, actual: Some(actual), .. } => write!(f, "it targets `{actual}`, not `{expected}`"),
Mismatch::Table { expected, actual: None, .. } => write!(f, "it does not reference table `{expected}`"),
Mismatch::Sql { matcher, hint: None } => write!(f, "it does not match {matcher}"),
Mismatch::Sql { matcher, hint: Some(hint) } => write!(f, "it does not match {matcher} ({hint})"),
Mismatch::Args { expected, actual, .. } => write!(f, "arguments {actual:?} do not match expected {expected:?}"),
}
}
}
#[derive(Clone)]
pub(crate) struct Declared {
pub label: String,
pub location: &'static Location<'static>,
}
#[derive(Clone)]
pub(crate) struct Rejection {
pub expectation: Declared,
pub mismatch: Mismatch,
}
#[derive(Clone)]
pub(crate) enum Reason {
Next(Rejection),
NoExpectation,
Consumed,
NoneMatched { candidates: &'static str, rejections: Vec<Rejection> },
MatchedWithoutResult(Declared),
MissingRows { fix: &'static str },
}
impl Reason {
pub(crate) fn summary(&self) -> String {
match self {
Reason::Next(_) => "the next expectation does not match it".to_string(),
Reason::NoExpectation => "no expectation was set".to_string(),
Reason::Consumed => "every expectation was already consumed".to_string(),
Reason::NoneMatched { candidates, .. } => format!("none of the {candidates} expectations matches it"),
Reason::MatchedWithoutResult(_) => "it matches an expectation that has no result".to_string(),
Reason::MissingRows { .. } => "it reads the written rows back (`RETURNING` on this backend), but its expectation has no rows to return".to_string(),
}
}
}
impl fmt::Display for Reason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.summary())?;
match self {
Reason::Next(rejection) => write!(f, " ({}): {}", rejection.expectation.label, rejection.mismatch),
Reason::NoExpectation | Reason::Consumed => Ok(()),
Reason::NoneMatched { rejections, .. } => {
write!(f, ":")?;
for rejection in rejections {
write!(f, "\n - {}: {}", rejection.expectation.label, rejection.mismatch)?;
}
Ok(())
}
Reason::MatchedWithoutResult(expectation) => write!(f, ": {}", expectation.label),
Reason::MissingRows { fix } => write!(f, ": complete it with {fix}"),
}
}
}
#[derive(Clone)]
pub(crate) enum Problem {
Unexpected { kind: StmtKind, stmt: Statement, reason: Reason },
Unmet(Declared),
NoResult(Declared),
}
impl Problem {
pub(crate) fn headline(&self) -> String {
match self {
Problem::Unexpected { kind, reason, .. } => format!("unexpected {kind}: {}", reason.summary()),
_ => self.to_string(),
}
}
}
impl fmt::Display for Problem {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Problem::Unexpected { kind, stmt, reason } => write!(f, "unexpected {kind} {}: {reason}", describe(stmt)),
Problem::Unmet(expectation) => write!(f, "expectation not met: {}", expectation.label),
Problem::NoResult(expectation) => write!(f, "expectation has no result: {}", expectation.label),
}
}
}