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