modelvault-core 0.15.1

Core engine for ModelVault — application-focused embedded storage with model schemas, validation, and migrations.
Documentation
use crate::error::DbError;
use crate::schema::CollectionId;

pub type RowKey = (CollectionId, Vec<u8>);

pub trait RowSource {
    fn next_key(&mut self) -> Option<Result<RowKey, DbError>>;
}

impl<T: RowSource + ?Sized> RowSource for Box<T> {
    fn next_key(&mut self) -> Option<Result<RowKey, DbError>> {
        (**self).next_key()
    }
}

pub struct LimitOp<S: RowSource> {
    inner: S,
    remaining: usize,
}

impl<S: RowSource> LimitOp<S> {
    pub fn new(inner: S, n: usize) -> Self {
        Self {
            inner,
            remaining: n,
        }
    }
}

impl<S: RowSource> RowSource for LimitOp<S> {
    #[rustfmt::skip]
    fn next_key(&mut self) -> Option<Result<RowKey, DbError>> {
        if self.remaining == 0 {
            return None;
        }
        let x = self.inner.next_key()?;
        Some(match x {
            Ok(rk) => { self.remaining = self.remaining.saturating_sub(1); Ok(rk) }
            Err(e) => Err(e), }) }
}

#[cfg(test)]
mod tests {
    include!(concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/tests/unit/src_query_operators_tests.rs"
    ));
}