use super::InnerCommitPolicy;
use crate::error::ShardError;

pub(super) enum PendingCallback<Data> {
    ExecuteMut(Box<dyn FnOnce(Result<&mut Data, ShardError>) -> InnerCommitPolicy + Send>),
    Execute(Box<dyn FnOnce(Result<&Data, ShardError>) + Send>),
}

impl<Data> PendingCallback<Data> {
    pub(super) fn reject(self, error: ShardError) {
        match self {
            PendingCallback::ExecuteMut(callback) => {
                (callback)(Err(error));
            }
            PendingCallback::Execute(callback) => {
                (callback)(Err(error));
            }
        }
    }

    pub(super) fn resolve(self, data: &mut Data) -> InnerCommitPolicy {
        match self {
            PendingCallback::Execute(callback) => {
                (callback)(Ok(data));
                InnerCommitPolicy::Noop
            }
            PendingCallback::ExecuteMut(callback) => (callback)(Ok(data)),
        }
    }
}
