use crate::{Error, Need, Plan, Pun, Root, pun_unchecked, validate::validate};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[expect(clippy::struct_excessive_bools, reason = "one for each function of the API")]
pub struct Verdict {
pub pun: bool,
pub head: bool,
pub try_pun: bool,
pub pun_ref: bool,
pub head_ref: bool,
pub try_pun_ref: bool,
pub pun_mut: bool,
pub try_pun_mut: bool,
why: ([u8; 512], usize),
}
impl Verdict {
#[must_use]
pub fn why(&self) -> &str {
core::str::from_utf8(self.why.0.split_at(self.why.1).0).unwrap_or("(not UTF-8)")
}
}
const fn ok<S: ?Sized + Pun, D: ?Sized + Pun>(need: Need) -> bool {
Plan::<S, D>::check(need).is_none()
}
#[must_use]
pub const fn verdict<S: ?Sized + Pun, D: ?Sized + Pun>() -> Verdict {
let mutable = Plan::<S, D>::MUTABLE;
let why = match Plan::<S, D>::check(Need::TRY_REF) {
None => ([0; 512], 0),
Some(message) => (message.bytes, message.len),
};
Verdict {
pun: ok::<S, D>(Need::VALUE),
head: ok::<S, D>(Need::PART),
try_pun: ok::<S, D>(Need::TRY),
pun_ref: ok::<S, D>(Need::REF),
head_ref: ok::<S, D>(Need::PART_REF),
try_pun_ref: ok::<S, D>(Need::TRY_REF),
pun_mut: ok::<S, D>(Need::REF) && mutable,
try_pun_mut: ok::<S, D>(Need::TRY_REF) && mutable,
why,
}
}
#[must_use]
pub const fn views<S: ?Sized + Pun, D: ?Sized + Pun>(fuel: usize) -> bool {
ok::<S, D>(Need::VIEW) && crate::deep::local::<S, D>(fuel).is_none()
}
#[must_use]
pub const fn tries_view<S: ?Sized + Pun, D: ?Sized + Pun>(fuel: usize) -> bool {
ok::<S, D>(Need::TRY_VIEW) && crate::deep::local::<S, D>(fuel).is_none()
}
#[must_use]
pub fn view_ref<S: ?Sized + Pun, D: ?Sized + Pun, const FUEL: usize>(value: &S) -> Option<&D> {
if const { !views::<S, D>(FUEL) } {
return None;
}
Some(unsafe { &*Plan::<S, D>::cast(value) })
}
#[must_use]
pub fn try_view_ref<S: ?Sized + Pun, D: ?Sized + Pun, const FUEL: usize>(
value: &S,
) -> Option<Result<&D, Error>> {
if const { !tries_view::<S, D>(FUEL) } {
return None;
}
Some(Plan::<S, D>::try_cast(value).map(|target| unsafe { &*target }))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[expect(clippy::struct_excessive_bools, reason = "one for each function of the API")]
pub struct Parts {
pub head: bool,
pub head_ref: bool,
pub head_mut: bool,
pub tail: bool,
pub tail_ref: bool,
pub tail_mut: bool,
}
#[must_use]
pub const fn parts<S: ?Sized + Pun, D: ?Sized + Pun>() -> Parts {
Parts {
head: ok::<S, D>(Need::PART),
head_ref: ok::<S, D>(Need::PART_REF),
head_mut: Plan::<S, D>::check_head_mut().is_none(),
tail: Plan::<S, D>::check_tail(Need::PART).is_none(),
tail_ref: Plan::<S, D>::check_tail(Need::PART_REF).is_none(),
tail_mut: Plan::<S, D>::check_tail_mut().is_none(),
}
}
#[must_use]
pub const fn try_pun<S: Pun + Copy, D: Pun + Copy>(value: S) -> Option<Result<D, Error>> {
if const { !verdict::<S, D>().try_pun } {
return None;
}
if !unsafe { validate::<Plan<S, D>, Root, D>((&raw const value).cast()) } {
return Some(Err(Error::Validity));
}
Some(Ok(unsafe { pun_unchecked(value) }))
}
#[must_use]
pub const fn pun<S: Pun + Copy, D: Pun + Copy>(value: S) -> Option<D> {
if const { !verdict::<S, D>().pun } {
return None;
}
Some(unsafe { pun_unchecked(value) })
}
#[must_use]
pub const fn head<S: Pun + Copy, D: Pun + Copy>(value: S) -> Option<D> {
if const { !verdict::<S, D>().head } {
return None;
}
Some(unsafe { pun_unchecked(value) })
}
#[must_use]
pub const fn tail<S: Pun + Copy, D: Pun + Copy>(value: S) -> Option<D> {
if const { !parts::<S, D>().tail } {
return None;
}
let shift = const { Plan::<S, D>::TAIL_ANALYSIS.shift };
Some(unsafe { (&raw const value).cast::<u8>().add(shift).cast::<D>().read_unaligned() })
}
#[must_use]
pub const fn tail_ref<S: ?Sized + Pun, D: ?Sized + Pun>(value: &S) -> Option<&D> {
if const { !parts::<S, D>().tail_ref } {
return None;
}
Some(unsafe { &*Plan::<S, D>::tail_cast(value) })
}
#[must_use]
pub fn try_pun_ref<S: ?Sized + Pun, D: ?Sized + Pun>(value: &S) -> Option<Result<&D, Error>> {
if const { !verdict::<S, D>().try_pun_ref } {
return None;
}
Some(Plan::<S, D>::try_cast(value).map(|target| unsafe { &*target }))
}
#[must_use]
pub fn try_pun_mut<S: ?Sized + Pun, D: ?Sized + Pun>(
value: &mut S,
) -> Option<Result<&mut D, Error>> {
if const { !verdict::<S, D>().try_pun_mut } {
return None;
}
let source: *mut S = value;
Some(Plan::<S, D>::try_cast(source).map(|target| unsafe { &mut *target.cast_mut() }))
}
#[must_use]
pub const fn pun_ref<S: ?Sized + Pun, D: ?Sized + Pun>(value: &S) -> Option<&D> {
if const { !verdict::<S, D>().pun_ref } {
return None;
}
Some(unsafe { &*Plan::<S, D>::cast(value) })
}
#[must_use]
pub const fn head_ref<S: ?Sized + Pun, D: ?Sized + Pun>(value: &S) -> Option<&D> {
if const { !verdict::<S, D>().head_ref } {
return None;
}
Some(unsafe { &*Plan::<S, D>::cast(value) })
}
#[must_use]
pub const fn head_mut<S: ?Sized + Pun, D: ?Sized + Pun>(value: &mut S) -> Option<&mut D> {
if const { !parts::<S, D>().head_mut } {
return None;
}
let source: *mut S = value;
Some(unsafe { &mut *Plan::<S, D>::cast(source).cast_mut() })
}
#[must_use]
pub const fn tail_mut<S: ?Sized + Pun, D: ?Sized + Pun>(value: &mut S) -> Option<&mut D> {
if const { !parts::<S, D>().tail_mut } {
return None;
}
let source: *mut S = value;
Some(unsafe { &mut *Plan::<S, D>::tail_cast(source).cast_mut() })
}
#[must_use]
pub const fn from_head<D: ?Sized + Pun>(bytes: &[u8]) -> Option<Result<(&D, &[u8]), Error>> {
if const { !verdict::<[u8], D>().try_pun_ref } {
return None;
}
Some(match unsafe { Plan::<[u8], D>::front(bytes.as_ptr(), bytes.len()) } {
Ok((target, size)) => Ok((unsafe { &*target }, bytes.split_at(size).1)),
Err(error) => Err(error),
})
}
#[must_use]
pub const fn from_tail<D: ?Sized + Pun>(bytes: &[u8]) -> Option<Result<(&[u8], &D), Error>> {
if const { Plan::<[u8], D>::check_from_tail().is_some() } {
return None;
}
Some(match unsafe { Plan::<[u8], D>::back(bytes.as_ptr(), bytes.len()) } {
Ok((start, target)) => Ok((bytes.split_at(start).0, unsafe { &*target })),
Err(error) => Err(error),
})
}