use std::path::PathBuf;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error("Native library not found. Searched paths: {searched_paths:?}. Set KQL_LANGUAGE_TOOLS_PATH to specify location.")]
LibraryNotFound { searched_paths: Vec<PathBuf> },
#[error("Failed to load native library from {path}: {message}")]
LibraryLoadFailed { path: PathBuf, message: String },
#[error("Symbol '{symbol}' not found in native library")]
SymbolNotFound { symbol: String },
#[error("Library initialization failed: {message}")]
InitializationFailed { message: String },
#[error("Native call failed with code {code}: {message}")]
NativeError { code: i32, message: String },
#[error("Output buffer too small (needed {needed} bytes, had {available})")]
BufferTooSmall { needed: usize, available: usize },
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
#[error("UTF-8 conversion error: {0}")]
Utf8(#[from] std::str::Utf8Error),
#[error("Library not initialized. Call KqlValidator::new() first.")]
NotInitialized,
#[error("Internal error: {message}")]
Internal { message: String },
}
impl Error {
#[must_use]
pub fn library_load_failed(path: impl Into<PathBuf>, err: impl std::fmt::Display) -> Self {
Self::LibraryLoadFailed {
path: path.into(),
message: err.to_string(),
}
}
#[must_use]
pub fn from_native_code(code: i32, context: &str) -> Self {
let message = match code {
-1 => "Buffer too small".to_string(),
-2 => "Parse error in input".to_string(),
-3 => "Internal error".to_string(),
_ => format!("Unknown error code: {code}"),
};
Self::NativeError {
code,
message: format!("{context}: {message}"),
}
}
}