#[cfg(not(feature = "std"))]
extern crate alloc;
#[cfg(not(feature = "std"))]
use alloc::{borrow::Cow, string::String, vec::Vec};
#[cfg(feature = "std")]
use std::borrow::Cow;
use crate::asn1::{MessagePriority, Version};
use crate::testing::macros::Cardinality;
#[cfg(feature = "policy")]
use crate::policy::TransitStatus;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum AssertionLabel {
Custom(Cow<'static, str>),
}
impl AssertionLabel {
pub fn matches(&self, other: &AssertionLabel) -> bool {
if self == other {
return true;
}
let (Self::Custom(recorded), Self::Custom(expected)) = (self, other);
let pattern = ["/", expected.as_ref()].concat();
recorded.ends_with(&pattern)
}
}
#[derive(Debug, Clone)]
pub enum AssertionValue {
String(String),
Bool(bool),
U8(u8),
U32(u32),
U64(u64),
I32(i32),
I64(i64),
F64(f64), MessagePriority(MessagePriority),
Version(Version),
Some(Box<AssertionValue>),
IsNone,
IsSome,
RatioActual(u64, u64),
RatioLimit(u64, u64),
#[cfg(feature = "policy")]
TransitStatus(TransitStatus),
}
impl PartialEq for AssertionValue {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::IsSome, Self::Some(_)) => true,
(Self::Some(_), Self::IsSome) => true,
(Self::IsSome, Self::IsSome) => true,
(Self::IsSome, Self::IsNone) => false,
(Self::IsNone, Self::IsSome) => false,
(Self::String(a), Self::String(b)) => a == b,
(Self::Bool(a), Self::Bool(b)) => a == b,
(Self::U8(a), Self::U8(b)) => a == b,
(Self::U32(a), Self::U32(b)) => a == b,
(Self::U64(a), Self::U64(b)) => a == b,
(Self::I32(a), Self::I32(b)) => a == b,
(Self::I64(a), Self::I64(b)) => a == b,
(Self::F64(a), Self::F64(b)) => a == b,
(Self::MessagePriority(a), Self::MessagePriority(b)) => a == b,
(Self::Version(a), Self::Version(b)) => a == b,
(Self::Some(a), Self::Some(b)) => a == b,
(Self::IsNone, Self::IsNone) => true,
(Self::RatioActual(an, ad), Self::RatioActual(bn, bd)) => ratio_equal(*an, *ad, *bn, *bd),
(Self::RatioLimit(an, ad), Self::RatioLimit(bn, bd)) => ratio_equal(*an, *ad, *bn, *bd),
(Self::RatioActual(an, ad), Self::RatioLimit(bn, bd)) => ratio_less_equal(*an, *ad, *bn, *bd),
(Self::RatioLimit(an, ad), Self::RatioActual(bn, bd)) => ratio_less_equal(*bn, *bd, *an, *ad),
#[cfg(feature = "policy")]
(Self::TransitStatus(a), Self::TransitStatus(b)) => a == b,
_ => false,
}
}
}
fn ratio_equal(an: u64, ad: u64, bn: u64, bd: u64) -> bool {
if ad == 0 || bd == 0 {
return false;
}
an.saturating_mul(bd) == bn.saturating_mul(ad)
}
fn ratio_less_equal(an: u64, ad: u64, bn: u64, bd: u64) -> bool {
if ad == 0 || bd == 0 {
return false;
}
an.saturating_mul(bd) <= bn.saturating_mul(ad)
}
impl From<String> for AssertionValue {
fn from(s: String) -> Self {
Self::String(s)
}
}
impl From<&str> for AssertionValue {
fn from(s: &str) -> Self {
Self::String(s.to_string())
}
}
impl From<bool> for AssertionValue {
fn from(b: bool) -> Self {
Self::Bool(b)
}
}
impl From<u8> for AssertionValue {
fn from(n: u8) -> Self {
Self::U8(n)
}
}
impl From<u32> for AssertionValue {
fn from(n: u32) -> Self {
Self::U32(n)
}
}
impl From<u64> for AssertionValue {
fn from(n: u64) -> Self {
Self::U64(n)
}
}
impl From<i32> for AssertionValue {
fn from(n: i32) -> Self {
Self::I32(n)
}
}
impl From<i64> for AssertionValue {
fn from(n: i64) -> Self {
Self::I64(n)
}
}
impl From<MessagePriority> for AssertionValue {
fn from(p: MessagePriority) -> Self {
Self::MessagePriority(p)
}
}
impl From<Version> for AssertionValue {
fn from(v: Version) -> Self {
Self::Version(v)
}
}
#[cfg(feature = "policy")]
impl From<TransitStatus> for AssertionValue {
fn from(status: TransitStatus) -> Self {
Self::TransitStatus(status)
}
}
impl<T> From<Option<T>> for AssertionValue
where
T: Into<AssertionValue>,
{
fn from(opt: Option<T>) -> Self {
match opt {
Some(val) => Self::Some(Box::new(val.into())),
None => Self::IsNone,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct IsSome;
impl From<IsSome> for AssertionValue {
fn from(_: IsSome) -> Self {
Self::IsSome
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct IsNone;
impl From<IsNone> for AssertionValue {
fn from(_: IsNone) -> Self {
Self::IsNone
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Presence {
Present,
Absent,
}
impl Presence {
pub fn of_option<T>(opt: &Option<T>) -> Self {
if opt.is_some() {
Self::Present
} else {
Self::Absent
}
}
}
impl From<Presence> for AssertionValue {
fn from(presence: Presence) -> Self {
match presence {
Presence::Present => AssertionValue::IsSome,
Presence::Absent => AssertionValue::IsNone,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RatioLimit(pub u64, pub u64);
impl From<(u64, u64)> for AssertionValue {
fn from(pair: (u64, u64)) -> Self {
Self::RatioActual(pair.0, pair.1)
}
}
impl From<RatioLimit> for AssertionValue {
fn from(limit: RatioLimit) -> Self {
Self::RatioLimit(limit.0, limit.1)
}
}
#[derive(Clone, Debug)]
pub struct Assertion {
pub seq: usize,
pub label: AssertionLabel,
pub tags: Vec<&'static str>,
pub payload_hash: Option<[u8; 32]>,
pub value: Option<AssertionValue>,
}
impl Assertion {
pub fn new(seq: usize, label: AssertionLabel, tags: Vec<&'static str>, payload_hash: Option<[u8; 32]>) -> Self {
Self { seq, label, tags, payload_hash, value: None }
}
pub fn with_value(
seq: usize,
label: AssertionLabel,
tags: Vec<&'static str>,
payload_hash: Option<[u8; 32]>,
value: AssertionValue,
) -> Self {
Self { seq, label, tags, payload_hash, value: Some(value) }
}
}
#[derive(Clone, Debug)]
pub struct AssertionContract {
pub label: AssertionLabel,
pub tag_filter: Option<Vec<&'static str>>,
pub cardinality: Cardinality,
pub expected_value: Option<AssertionValue>,
}
impl AssertionContract {
pub fn new(label: AssertionLabel, cardinality: Cardinality) -> Self {
Self { label, tag_filter: None, cardinality, expected_value: None }
}
pub fn with_tag_filter(mut self, tags: Vec<&'static str>) -> Self {
self.tag_filter = Some(tags);
self
}
pub fn with_value(mut self, expected_value: AssertionValue) -> Self {
self.expected_value = Some(expected_value);
self
}
pub fn is_satisfied_by(&self, assertions: &[Assertion]) -> bool {
let matching: Vec<_> = assertions
.iter()
.filter(|a| {
if !a.label.matches(&self.label) {
return false;
}
if let Some(ref filter_tags) = self.tag_filter {
for filter_tag in filter_tags {
if !a.tags.contains(filter_tag) {
return false;
}
}
}
true
})
.collect();
if !self.cardinality.is_satisfied_by(matching.len()) {
return false;
}
if let Some(ref expected) = self.expected_value {
matching.iter().all(|a| a.value.as_ref() == Some(expected))
} else {
true
}
}
pub fn describe(&self) -> String {
let cardinality_desc = self.cardinality.describe();
let tag_desc = if let Some(ref tags) = self.tag_filter {
format!(" with tags {tags:?}")
} else {
String::new()
};
if let Some(ref expected) = self.expected_value {
format!("{cardinality_desc} with value {expected:?}{tag_desc}")
} else {
format!("{cardinality_desc}{tag_desc}")
}
}
}
pub mod cardinality {
pub use crate::testing::macros::{absent, at_least, at_most, between, exactly, present};
}