//! Error type for CortEX adapter operations.

use thiserror::Error;

use super::super::redex::RedexError;

/// Errors produced by [`super::CortexAdapter`] operations.
#[derive(Debug, Error)]
pub enum CortexAdapterError {
    /// Underlying RedEX storage error.
    #[error("redex: {0}")]
    Redex(#[from] RedexError),

    /// The adapter has been closed.
    #[error("adapter closed")]
    Closed,

    /// The fold task has stopped (first fold error under
    /// [`super::FoldErrorPolicy::Stop`]). Holds the RedEX sequence
    /// at which the fold stopped.
    #[error("fold stopped at seq {seq}")]
    FoldStopped {
        /// The RedEX sequence of the event whose fold returned an error.
        seq: u64,
    },

    /// `open` was called with a `StartPosition` that requires
    /// externally-rehydrated state — `FromSeq(n)` for `n > 0` or
    /// `LiveOnly`. Callers using these positions must construct
    /// the adapter via [`super::CortexAdapter::open_from_snapshot`]
    /// instead so the watermark and `state` are properly anchored
    /// to the prior events the adapter is going to skip.
    ///
    /// Accepting these positions in `open` would set
    /// `initial_watermark = start_seq - 1`, making
    /// `wait_for_seq(k)` for `k <= start_seq-1` return immediately
    /// — the adapter would claim those seqs were "applied" while
    /// `state` had never seen them.
    #[error("StartPosition::{0} requires open_from_snapshot, not open")]
    InvalidStartPosition(&'static str),
}
