baml_sys/
error.rs

1//! Error types for baml-sys library loading.
2
3use std::path::PathBuf;
4
5/// Errors that can occur during library loading.
6#[derive(Debug, thiserror::Error)]
7#[allow(missing_docs)] // Error variants are self-documenting via #[error(...)]
8pub enum BamlSysError {
9    /// Library file not found at any search path.
10    #[error("BAML library not found. Searched paths: {searched_paths:?}")]
11    LibraryNotFound { searched_paths: Vec<PathBuf> },
12
13    /// Failed to load the dynamic library.
14    #[error("Failed to load BAML library from {path}: {source}")]
15    LoadFailed {
16        path: PathBuf,
17        #[source]
18        source: libloading::Error,
19    },
20
21    /// Symbol not found in loaded library.
22    #[error("Symbol '{symbol}' not found in BAML library: {source}")]
23    SymbolNotFound {
24        symbol: &'static str,
25        #[source]
26        source: libloading::Error,
27    },
28
29    /// Version mismatch between Rust package and loaded library.
30    #[error("Version mismatch: Rust package expects {expected}, but library reports {actual}")]
31    VersionMismatch { expected: String, actual: String },
32
33    /// Platform not supported.
34    #[error("Platform not supported: {os}/{arch}")]
35    UnsupportedPlatform {
36        os: &'static str,
37        arch: &'static str,
38    },
39
40    /// Failed to determine cache directory.
41    #[error("Failed to determine cache directory: {0}")]
42    CacheDir(String),
43
44    /// Download failed.
45    #[error("Failed to download library: {0}")]
46    DownloadFailed(String),
47
48    /// Checksum mismatch after download.
49    #[error("Checksum mismatch: expected {expected}, got {actual}")]
50    ChecksumMismatch { expected: String, actual: String },
51
52    /// IO error.
53    #[error("IO error: {0}")]
54    Io(#[from] std::io::Error),
55
56    /// Library already initialized with different path.
57    #[error("Library already initialized from {existing_path}, cannot change to {requested_path}")]
58    AlreadyInitialized {
59        existing_path: PathBuf,
60        requested_path: PathBuf,
61    },
62}
63
64/// Result type for baml-sys operations.
65pub type Result<T> = std::result::Result<T, BamlSysError>;