use num_bigint::{BigInt, BigUint};
use ref_cast::RefCast;
use smol_str::SmolStr;
use std::{cmp::Ordering, num::NonZeroU32, ops::Deref};
#[expect(missing_docs, reason = "existing code")]
pub type EntityType = cedar_policy::EntityTypeName;
#[expect(missing_docs, reason = "existing code")]
pub type EntityID = cedar_policy::EntityId;
#[expect(missing_docs, reason = "existing code")]
pub type EntityUID = cedar_policy::EntityUid;
#[expect(missing_docs, reason = "existing code")]
pub type Attr = SmolStr;
#[expect(missing_docs, reason = "existing code")]
pub type Prim = cedar_policy_core::ast::Literal;
#[expect(missing_docs, reason = "existing code")]
pub type Nat = BigUint;
#[expect(missing_docs, reason = "existing code")]
pub type Int = BigInt;
pub type Width = NonZeroU32;
pub const ONE: Width = NonZeroU32::new(1).expect("1 is not 0"); pub const TWO: Width = NonZeroU32::new(2).expect("2 is not 0"); pub const FIVE: Width = NonZeroU32::new(5).expect("5 is not 0"); pub const SEVEN: Width = NonZeroU32::new(7).expect("7 is not 0"); pub const EIGHT: Width = NonZeroU32::new(8).expect("8 is not 0"); pub const SIXTEEN: Width = NonZeroU32::new(16).expect("16 is not 0"); pub const THIRTY_TWO: Width = NonZeroU32::new(32).expect("32 is not 0"); pub const SIXTY_FOUR: Width = NonZeroU32::new(64).expect("64 is not 0"); pub const HUNDRED_TWENTY: Width = NonZeroU32::new(120).expect("120 is not 0"); pub const HUNDRED_TWENTY_EIGHT: Width = NonZeroU32::new(128).expect("128 is not 0");
pub fn core_entity_type_into_entity_type(
entity_type: &cedar_policy_core::ast::EntityType,
) -> &EntityType {
EntityType::ref_cast(entity_type)
}
pub fn core_uid_into_uid(uid: &cedar_policy_core::ast::EntityUID) -> &EntityUID {
EntityUID::ref_cast(uid)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Ord, PartialOrd)]
#[expect(missing_docs, reason = "existing code")]
pub enum ExtType {
IpAddr,
Decimal,
DateTime,
Duration,
}
pub fn nat(v: u32) -> Nat {
BigUint::from(v)
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct Fin {
upper_bound: Nat,
v: Nat,
}
impl Fin {
pub fn try_new(upper_bound: Nat, v: Nat) -> Option<Self> {
if v < upper_bound {
Some(Self { upper_bound, v })
} else {
None
}
}
pub fn to_nat(&self) -> Nat {
self.v.clone()
}
}
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct OrdPattern(cedar_policy_core::ast::Pattern);
impl From<cedar_policy_core::ast::Pattern> for OrdPattern {
fn from(p: cedar_policy_core::ast::Pattern) -> Self {
Self(p)
}
}
impl Deref for OrdPattern {
type Target = cedar_policy_core::ast::Pattern;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
struct OrdPatternElem(cedar_policy_core::ast::PatternElem);
impl From<cedar_policy_core::ast::PatternElem> for OrdPatternElem {
fn from(e: cedar_policy_core::ast::PatternElem) -> Self {
Self(e)
}
}
impl PartialOrd for OrdPatternElem {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for OrdPatternElem {
fn cmp(&self, other: &Self) -> Ordering {
use cedar_policy_core::ast::PatternElem;
match (self.0, other.0) {
(PatternElem::Char(c1), PatternElem::Char(c2)) => c1.cmp(&c2),
(PatternElem::Wildcard, PatternElem::Wildcard) => Ordering::Equal,
(PatternElem::Char(_), PatternElem::Wildcard) => Ordering::Less,
(PatternElem::Wildcard, PatternElem::Char(_)) => Ordering::Greater,
}
}
}
impl PartialOrd for OrdPattern {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for OrdPattern {
fn cmp(&self, other: &Self) -> Ordering {
self.get_elems()
.iter()
.copied()
.map(OrdPatternElem::from)
.cmp(other.get_elems().iter().copied().map(OrdPatternElem::from))
}
}