toasty 0.4.0

An async ORM for Rust supporting SQL and NoSQL databases
Documentation
use crate::{
    Result,
    engine::{
        eval,
        exec::{Action, Exec, Output, VarId},
    },
};
use toasty_core::driver::{ExecResponse, Rows};

#[derive(Debug)]
pub(crate) struct Filter {
    /// Source of the input
    pub(crate) input: VarId,

    /// Where to store the output
    pub(crate) output: Output,

    /// How to project it before storing
    pub(crate) filter: eval::Func,
}

impl Exec<'_> {
    pub(super) async fn action_filter(&mut self, action: &Filter) -> Result<()> {
        // Load the input variable with metadata
        let input_response = self.vars.load(action.input).await?;
        let mut input_stream = input_response.values.into_value_stream();

        let mut filtered_rows = vec![];

        // Iterate through the input stream and apply the filter
        while let Some(res) = input_stream.next().await {
            let value = res?;

            if action.filter.eval_bool(std::slice::from_ref(&value))? {
                filtered_rows.push(value);
            }
        }

        // Store the filtered stream with preserved pagination metadata
        self.vars.store(
            action.output.var,
            action.output.num_uses,
            ExecResponse {
                values: Rows::value_stream(filtered_rows),
                next_cursor: input_response.next_cursor,
                prev_cursor: input_response.prev_cursor,
            },
        );

        Ok(())
    }
}

impl From<Filter> for Action {
    fn from(value: Filter) -> Self {
        Action::Filter(value)
    }
}