scirs2_text/
error.rs

1//! Error types for the text processing module
2
3use thiserror::Error;
4
5/// Error type for text processing operations
6#[derive(Error, Debug, Clone)]
7pub enum TextError {
8    /// Invalid input text
9    #[error("Invalid input: {0}")]
10    InvalidInput(String),
11
12    /// Tokenization error
13    #[error("Tokenization error: {0}")]
14    TokenizationError(String),
15
16    /// Text processing error
17    #[error("Text processing error: {0}")]
18    ProcessingError(String),
19
20    /// Vocabulary error
21    #[error("Vocabulary error: {0}")]
22    VocabularyError(String),
23
24    /// Embedding error
25    #[error("Embedding error: {0}")]
26    EmbeddingError(String),
27
28    /// Distance calculation error
29    #[error("Distance calculation error: {0}")]
30    DistanceError(String),
31
32    /// IO error
33    #[error("IO error: {0}")]
34    IoError(String),
35
36    /// Other error
37    #[error("Error: {0}")]
38    Other(String),
39
40    /// Model not fitted error
41    #[error("Model not fitted: {0}")]
42    ModelNotFitted(String),
43
44    /// Runtime error
45    #[error("Runtime error: {0}")]
46    RuntimeError(String),
47}
48
49/// Result type for text processing operations
50pub type Result<T> = std::result::Result<T, TextError>;
51
52/// Implement From trait for converting std::io::Error to TextError
53impl From<std::io::Error> for TextError {
54    fn from(err: std::io::Error) -> Self {
55        TextError::IoError(err.to_string())
56    }
57}
58
59/// Implement From trait for converting CoreError to TextError
60impl From<scirs2_core::CoreError> for TextError {
61    fn from(err: scirs2_core::CoreError) -> Self {
62        TextError::RuntimeError(err.to_string())
63    }
64}
65
66/// Implement From trait for converting serde_json::Error to TextError
67#[cfg(feature = "serde-support")]
68impl From<serde_json::Error> for TextError {
69    fn from(err: serde_json::Error) -> Self {
70        TextError::InvalidInput(format!("JSON parsing error: {}", err))
71    }
72}