Skip to main content

trueno_rag/
error.rs

1//! Error types for Trueno-RAG
2
3use thiserror::Error;
4
5/// Result type for Trueno-RAG operations
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Error types for RAG pipeline operations
9#[derive(Error, Debug)]
10pub enum Error {
11    /// Document is empty or invalid
12    #[error("empty document: {0}")]
13    EmptyDocument(String),
14
15    /// Chunk too large for processing
16    #[error("chunk exceeds maximum size: {size} > {max}")]
17    ChunkTooLarge {
18        /// Actual size
19        size: usize,
20        /// Maximum allowed size
21        max: usize,
22    },
23
24    /// Embedding dimension mismatch
25    #[error("embedding dimension mismatch: expected {expected}, got {actual}")]
26    DimensionMismatch {
27        /// Expected dimension
28        expected: usize,
29        /// Actual dimension
30        actual: usize,
31    },
32
33    /// Index not found
34    #[error("index not found: {0}")]
35    IndexNotFound(String),
36
37    /// Vector store error
38    #[error("vector store error: {0}")]
39    VectorStore(String),
40
41    /// Serialization error (serde_json)
42    #[error("serialization error: {0}")]
43    Serialization(#[from] serde_json::Error),
44
45    /// Serialization error (bincode/compression) - GH-2
46    #[error("serialization error: {0}")]
47    SerializationError(String),
48
49    /// IO error
50    #[error("IO error: {0}")]
51    Io(#[from] std::io::Error),
52
53    /// Invalid configuration
54    #[error("invalid configuration: {0}")]
55    InvalidConfig(String),
56
57    /// Query processing error
58    #[error("query error: {0}")]
59    Query(String),
60
61    /// Embedding error (GH-1: production embeddings)
62    #[error("embedding error: {0}")]
63    Embedding(String),
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    #[test]
71    fn test_error_display_empty_document() {
72        let err = Error::EmptyDocument("test.txt".to_string());
73        assert_eq!(err.to_string(), "empty document: test.txt");
74    }
75
76    #[test]
77    fn test_error_display_chunk_too_large() {
78        let err = Error::ChunkTooLarge {
79            size: 1000,
80            max: 512,
81        };
82        assert_eq!(err.to_string(), "chunk exceeds maximum size: 1000 > 512");
83    }
84
85    #[test]
86    fn test_error_display_dimension_mismatch() {
87        let err = Error::DimensionMismatch {
88            expected: 384,
89            actual: 768,
90        };
91        assert_eq!(
92            err.to_string(),
93            "embedding dimension mismatch: expected 384, got 768"
94        );
95    }
96
97    #[test]
98    fn test_error_from_io() {
99        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
100        let err = Error::from(io_err);
101        assert!(err.to_string().contains("file not found"));
102    }
103
104    #[test]
105    fn test_result_type() {
106        fn may_fail(succeed: bool) -> Result<i32> {
107            if succeed {
108                Ok(42)
109            } else {
110                Err(Error::InvalidConfig("test".to_string()))
111            }
112        }
113
114        assert_eq!(may_fail(true).unwrap(), 42);
115        assert!(may_fail(false).is_err());
116    }
117}