Skip to main content

fm_bindings/
error.rs

1// Error types for Foundation Models bindings
2
3use std::fmt;
4
5/// Errors that can occur when using Foundation Models
6#[derive(Debug, Clone)]
7pub enum Error {
8    /// The Foundation Model is not available on this system
9    /// This typically means Apple Intelligence is not enabled
10    ModelNotAvailable,
11
12    /// The system returned an error during generation
13    GenerationError(String),
14
15    /// Invalid input was provided (e.g., empty prompt, null bytes)
16    InvalidInput(String),
17
18    /// An internal FFI error occurred
19    InternalError(String),
20
21    /// A mutex or synchronization primitive was poisoned
22    /// This indicates a panic occurred while holding a lock
23    PoisonError,
24}
25
26impl fmt::Display for Error {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            Error::ModelNotAvailable => {
30                write!(
31                    f,
32                    "Foundation Model not available. Enable Apple Intelligence in System Settings."
33                )
34            }
35            Error::GenerationError(msg) => {
36                write!(f, "Generation error: {}", msg)
37            }
38            Error::InvalidInput(msg) => {
39                write!(f, "Invalid input: {}", msg)
40            }
41            Error::InternalError(msg) => {
42                write!(f, "Internal error: {}", msg)
43            }
44            Error::PoisonError => {
45                write!(
46                    f,
47                    "Synchronization primitive poisoned due to panic while holding lock"
48                )
49            }
50        }
51    }
52}
53
54impl std::error::Error for Error {}
55
56/// Result type alias for Foundation Models operations
57pub type Result<T> = std::result::Result<T, Error>;