use std::io;
#[cfg(feature = "full")]
use crate::assert::{self, guidance};
#[cfg(feature = "full")]
use once_cell::sync::Lazy;
#[cfg(feature = "full")]
use serde_json::Value;
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum AssertionKind {
Always,
AlwaysOrUnreachable,
Sometimes,
Reachable,
Unreachable,
}
impl AssertionKind {
pub const fn display_type(self) -> &'static str {
match self {
AssertionKind::Always => "Always",
AssertionKind::AlwaysOrUnreachable => "AlwaysOrUnreachable",
AssertionKind::Sometimes => "Sometimes",
AssertionKind::Reachable => "Reachable",
AssertionKind::Unreachable => "Unreachable",
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub struct SourceLocation {
pub file: &'static str,
pub begin_line: u32,
pub begin_column: u32,
pub class: &'static str,
pub function: &'static str,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Assertion {
pub id: &'static str,
pub message: &'static str,
pub kind: AssertionKind,
pub location: SourceLocation,
}
pub fn assertions() -> impl Iterator<Item = Assertion> {
#[cfg(feature = "full")]
{
assert::ANTITHESIS_CATALOG.iter().map(assertion_of)
}
#[cfg(not(feature = "full"))]
{
std::iter::empty()
}
}
pub fn write<W: io::Write>(out: W) -> io::Result<()> {
#[cfg(feature = "full")]
{
let mut out = out;
for event in assertion_events() {
serde_json::to_writer(&mut out, &event)?;
out.write_all(b"\n")?;
}
Ok(())
}
#[cfg(not(feature = "full"))]
{
let _ = out;
Ok(())
}
}
#[cfg(feature = "full")]
pub(crate) fn registrations() -> impl Iterator<Item = Value> {
let guidance = assert::ANTITHESIS_GUIDANCE_CATALOG
.iter()
.map(guidance::catalog_event);
assertion_events().chain(guidance)
}
#[cfg(feature = "full")]
fn assertion_events() -> impl Iterator<Item = Value> {
assert::ANTITHESIS_CATALOG.iter().map(assert::catalog_event)
}
#[cfg(feature = "full")]
#[allow(clippy::explicit_auto_deref)]
fn assertion_of(info: &assert::AssertionCatalogInfo) -> Assertion {
Assertion {
id: info.id,
message: info.message,
kind: kind_of(info.assert_type, info.must_hit),
location: SourceLocation {
file: info.file,
begin_line: info.begin_line,
begin_column: info.begin_column,
class: info.class,
function: *Lazy::force(info.function),
},
}
}
#[cfg(feature = "full")]
fn kind_of(assert_type: assert::AssertType, must_hit: bool) -> AssertionKind {
match (assert_type, must_hit) {
(assert::AssertType::Always, true) => AssertionKind::Always,
(assert::AssertType::Always, false) => AssertionKind::AlwaysOrUnreachable,
(assert::AssertType::Sometimes, _) => AssertionKind::Sometimes,
(assert::AssertType::Reachability, true) => AssertionKind::Reachable,
(assert::AssertType::Reachability, false) => AssertionKind::Unreachable,
}
}