sacp-cbor 0.7.1

SACP-CBOR/1: strict deterministic CBOR validation and canonical encoding (hot-path optimized, no_std-capable).
Documentation
#[cfg(feature = "simdutf8")]
use simdutf8::basic as simd_utf8;

/// Validates UTF-8 bytes and returns a borrowed `&str` on success.
#[inline]
pub fn validate(bytes: &[u8]) -> Result<&str, ()> {
    #[cfg(feature = "simdutf8")]
    {
        simd_utf8::from_utf8(bytes).map_err(|_| ())
    }

    #[cfg(not(feature = "simdutf8"))]
    {
        core::str::from_utf8(bytes).map_err(|_| ())
    }
}

/// Returns a `&str` from canonical-trusted bytes.
///
/// In `unsafe-utf8` mode this skips validation and relies on the canonical
/// input invariant.
#[cfg(feature = "unsafe-utf8")]
#[inline]
#[allow(clippy::unnecessary_wraps)]
#[allow(clippy::missing_const_for_fn)]
pub fn trusted(bytes: &[u8]) -> Result<&str, ()> {
    // Safety: callers only use this for canonical-validated bytes.
    Ok(unsafe { core::str::from_utf8_unchecked(bytes) })
}

/// Returns a `&str` from canonical-trusted bytes.
#[cfg(not(feature = "unsafe-utf8"))]
#[inline]
pub fn trusted(bytes: &[u8]) -> Result<&str, ()> {
    validate(bytes)
}