Skip to main content

token_count/
error.rs

1//! Error types for token counting operations
2
3use thiserror::Error;
4
5/// Errors that can occur during token counting
6#[derive(Error, Debug)]
7pub enum TokenError {
8    #[error("Input contains invalid UTF-8 at byte {offset}")]
9    InvalidUtf8 { offset: usize },
10
11    #[error("Unknown model: '{model}'. {suggestion}")]
12    UnknownModel { model: String, suggestion: String },
13
14    #[error("Input size ({size} bytes) exceeds maximum limit ({limit} bytes). Consider processing in smaller chunks.")]
15    InputTooLarge { size: usize, limit: usize },
16
17    #[error("IO error: {0}")]
18    Io(#[from] std::io::Error),
19
20    #[error("Tokenization error: {0}")]
21    Tokenization(String),
22}
23
24impl TokenError {
25    /// Get the exit code for this error
26    pub fn exit_code(&self) -> i32 {
27        match self {
28            Self::InvalidUtf8 { .. } => 1,
29            Self::UnknownModel { .. } => 2,
30            Self::InputTooLarge { .. } => 1,
31            Self::Io(_) => 1,
32            Self::Tokenization(_) => 1,
33        }
34    }
35}