wolfcose 0.1.0

Safe Rust API for wolfSSL wolfCOSE.
use crate::{CborDecoder, CborItem, Result};

/// Non-async incremental CBOR item reader over one caller-provided slice.
///
/// The reader advances item-by-item without allocating. It is intended for
/// protocols that feed complete buffer chunks and need to inspect top-level
/// CBOR item boundaries.
pub struct CborItemReader<'a> {
    decoder: CborDecoder<'a>,
}

impl<'a> CborItemReader<'a> {
    /// Create a reader over `input`.
    pub fn new(input: &'a [u8]) -> Self {
        Self {
            decoder: CborDecoder::new(input),
        }
    }

    /// Current byte position.
    pub fn position(&self) -> usize {
        self.decoder.position()
    }

    /// Remaining bytes.
    pub fn remaining(&self) -> usize {
        self.decoder.remaining()
    }

    /// Whether all bytes have been consumed.
    pub fn is_finished(&self) -> bool {
        self.decoder.is_finished()
    }

    /// Borrow the underlying decoder.
    pub fn decoder_mut(&mut self) -> &mut CborDecoder<'a> {
        &mut self.decoder
    }

    /// Decode the next item head, leaving nested item contents to the caller.
    pub fn next_head(&mut self) -> Result<Option<CborItem<'a>>> {
        if self.decoder.is_finished() {
            Ok(None)
        } else {
            self.decoder.decode_head().map(Some)
        }
    }

    /// Skip the next complete CBOR item.
    pub fn skip_next(&mut self) -> Result<bool> {
        if self.decoder.is_finished() {
            Ok(false)
        } else {
            self.decoder.skip()?;
            Ok(true)
        }
    }
}