kql_language_tools/
error.rs1use std::path::PathBuf;
4use thiserror::Error;
5
6#[derive(Debug, Error)]
8pub enum Error {
9 #[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 #[error("Failed to load native library from {path}: {message}")]
15 LibraryLoadFailed { path: PathBuf, message: String },
16
17 #[error("Symbol '{symbol}' not found in native library")]
19 SymbolNotFound { symbol: String },
20
21 #[error("Library initialization failed: {message}")]
23 InitializationFailed { message: String },
24
25 #[error("Native call failed with code {code}: {message}")]
27 NativeError { code: i32, message: String },
28
29 #[error("Output buffer too small (needed {needed} bytes, had {available})")]
31 BufferTooSmall { needed: usize, available: usize },
32
33 #[error("JSON error: {0}")]
35 Json(#[from] serde_json::Error),
36
37 #[error("UTF-8 conversion error: {0}")]
39 Utf8(#[from] std::str::Utf8Error),
40
41 #[error("Library not initialized. Call KqlValidator::new() first.")]
43 NotInitialized,
44
45 #[error("Internal error: {message}")]
47 Internal { message: String },
48}
49
50impl Error {
51 #[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 #[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}