docbert_plaid/error.rs
1//! Error type for the PLAID pipeline.
2//!
3//! A single `PlaidError` enum covers every failure mode the crate
4//! surfaces to callers. Variants use `#[from]` where a clean `?`
5//! conversion from a foreign error type makes sense, so the hot
6//! paths stay free of `map_err` boilerplate.
7//!
8//! Variants are added incrementally as each module migrates off
9//! `unwrap`/`expect`. This commit introduces the enum and the
10//! `Tensor` variant for `candle_core::Error` propagation; later
11//! commits add `InvalidCodec`, `InvalidIndex`, and `Io`.
12
13use thiserror::Error;
14
15/// Every failure the PLAID pipeline can surface.
16#[derive(Debug, Error)]
17pub enum PlaidError {
18 /// Underlying tensor operation failed (allocation, matmul,
19 /// transpose, argmin, etc.). Wraps `candle_core::Error` so
20 /// callers can downcast when they care about the specific
21 /// cause but still propagate uniformly via `?`.
22 #[error("tensor operation failed: {0}")]
23 Tensor(#[from] candle_core::Error),
24
25 /// A [`ResidualCodec`] failed its shape/invariant checks. Raised
26 /// from `encode_vector`, `decode_vector`, `batch_encode_tokens`,
27 /// and `build_index`'s validate step. The wrapped `String`
28 /// describes the specific constraint violated (dim == 0, nbits
29 /// not in `{1,2,4,8}`, non-monotonic cutoffs, etc.).
30 ///
31 /// [`ResidualCodec`]: crate::codec::ResidualCodec
32 #[error("invalid codec: {0}")]
33 InvalidCodec(String),
34
35 /// An on-disk index file was readable but its contents violated
36 /// the expected format (wrong magic bytes, unknown format
37 /// version, out-of-range header fields, or a centroid_id above
38 /// the advertised `k_centroids`). Distinct from `Io` — the file
39 /// was reached, it's just malformed.
40 #[error("invalid index file: {0}")]
41 InvalidIndex(String),
42
43 /// Low-level I/O failure (open/read/write/flush). Wraps
44 /// `std::io::Error` verbatim so callers retain the original
45 /// error kind when they need it.
46 #[error("i/o error: {0}")]
47 Io(#[from] std::io::Error),
48}
49
50/// Crate-level `Result` alias. Every public function that can fail
51/// returns this.
52pub type Result<T> = std::result::Result<T, PlaidError>;