Skip to main content

popsam_core/
error.rs

1use thiserror::Error;
2
3/// Convenience result type used throughout the crate.
4pub type PopsamResult<T> = Result<T, PopsamError>;
5
6/// Errors returned by the public `popsam-core` API.
7#[derive(Debug, Error)]
8pub enum PopsamError {
9    /// The caller supplied no records.
10    #[error("input data is empty")]
11    EmptyInput,
12    /// `report_last_k` must be at least `1`.
13    #[error("report_last_k must be at least 1")]
14    InvalidReportK,
15    /// `elimination_fraction` must be within `(0, 1]`.
16    #[error("elimination_fraction must be in the interval (0, 1]")]
17    InvalidEliminationFraction,
18    /// An embedding vector was empty.
19    #[error("embedding vector for id '{id}' is empty")]
20    EmptyEmbedding {
21        /// The record identifier.
22        id: String,
23    },
24    /// Embedding vectors in one election must all have the same dimension.
25    #[error("embedding vector lengths differ: expected {expected}, got {actual} for id '{id}'")]
26    DimensionMismatch {
27        /// The record identifier.
28        id: String,
29        /// The expected embedding length.
30        expected: usize,
31        /// The embedding length that was actually provided.
32        actual: usize,
33    },
34    /// An embedding vector had zero norm and cannot be normalized.
35    #[error("embedding vector for id '{id}' has zero norm")]
36    ZeroNorm {
37        /// The record identifier.
38        id: String,
39    },
40    /// The configured embedding provider returned an error.
41    #[error("embedding provider error: {0}")]
42    Provider(String),
43    /// Loading a local model or tokenizer failed.
44    #[error("model load error: {0}")]
45    ModelLoad(String),
46    /// File I/O failed.
47    #[error("io error: {0}")]
48    Io(#[from] std::io::Error),
49    /// JSON serialization or deserialization failed.
50    #[error("json error: {0}")]
51    Json(#[from] serde_json::Error),
52    /// CSV parsing failed.
53    #[error("csv error: {0}")]
54    Csv(#[from] csv::Error),
55}