kql_language_tools/
error.rs

1//! Error types for KQL Language Tools
2
3use std::path::PathBuf;
4use thiserror::Error;
5
6/// Errors that can occur when using KQL Language Tools
7#[derive(Debug, Error)]
8pub enum Error {
9    /// The native library could not be found
10    #[error("Native library not found. Searched paths: {searched_paths:?}. Set KQL_LANGUAGE_TOOLS_PATH to specify location.")]
11    LibraryNotFound { searched_paths: Vec<PathBuf> },
12
13    /// The native library failed to load
14    #[error("Failed to load native library from {path}: {message}")]
15    LibraryLoadFailed { path: PathBuf, message: String },
16
17    /// A required symbol was not found in the library
18    #[error("Symbol '{symbol}' not found in native library")]
19    SymbolNotFound { symbol: String },
20
21    /// The library initialization failed
22    #[error("Library initialization failed: {message}")]
23    InitializationFailed { message: String },
24
25    /// Native library call returned an error code
26    #[error("Native call failed with code {code}: {message}")]
27    NativeError { code: i32, message: String },
28
29    /// Output buffer was too small
30    #[error("Output buffer too small (needed {needed} bytes, had {available})")]
31    BufferTooSmall { needed: usize, available: usize },
32
33    /// JSON serialization/deserialization failed
34    #[error("JSON error: {0}")]
35    Json(#[from] serde_json::Error),
36
37    /// UTF-8 conversion failed
38    #[error("UTF-8 conversion error: {0}")]
39    Utf8(#[from] std::str::Utf8Error),
40
41    /// The library is not initialized
42    #[error("Library not initialized. Call KqlValidator::new() first.")]
43    NotInitialized,
44
45    /// An internal error occurred
46    #[error("Internal error: {message}")]
47    Internal { message: String },
48}
49
50impl Error {
51    /// Create a library load failure error
52    #[must_use]
53    pub fn library_load_failed(path: impl Into<PathBuf>, err: impl std::fmt::Display) -> Self {
54        Self::LibraryLoadFailed {
55            path: path.into(),
56            message: err.to_string(),
57        }
58    }
59
60    /// Create a native error from a return code
61    #[must_use]
62    pub fn from_native_code(code: i32, context: &str) -> Self {
63        let message = match code {
64            -1 => "Buffer too small".to_string(),
65            -2 => "Parse error in input".to_string(),
66            -3 => "Internal error".to_string(),
67            _ => format!("Unknown error code: {code}"),
68        };
69        Self::NativeError {
70            code,
71            message: format!("{context}: {message}"),
72        }
73    }
74}