kql-language-tools 0.1.0

Rust bindings to Kusto.Language for KQL validation and language services
Documentation
//! Error types for KQL Language Tools

use std::path::PathBuf;
use thiserror::Error;

/// Errors that can occur when using KQL Language Tools
#[derive(Debug, Error)]
pub enum Error {
    /// The native library could not be found
    #[error("Native library not found. Searched paths: {searched_paths:?}. Set KQL_LANGUAGE_TOOLS_PATH to specify location.")]
    LibraryNotFound { searched_paths: Vec<PathBuf> },

    /// The native library failed to load
    #[error("Failed to load native library from {path}: {message}")]
    LibraryLoadFailed { path: PathBuf, message: String },

    /// A required symbol was not found in the library
    #[error("Symbol '{symbol}' not found in native library")]
    SymbolNotFound { symbol: String },

    /// The library initialization failed
    #[error("Library initialization failed: {message}")]
    InitializationFailed { message: String },

    /// Native library call returned an error code
    #[error("Native call failed with code {code}: {message}")]
    NativeError { code: i32, message: String },

    /// Output buffer was too small
    #[error("Output buffer too small (needed {needed} bytes, had {available})")]
    BufferTooSmall { needed: usize, available: usize },

    /// JSON serialization/deserialization failed
    #[error("JSON error: {0}")]
    Json(#[from] serde_json::Error),

    /// UTF-8 conversion failed
    #[error("UTF-8 conversion error: {0}")]
    Utf8(#[from] std::str::Utf8Error),

    /// The library is not initialized
    #[error("Library not initialized. Call KqlValidator::new() first.")]
    NotInitialized,

    /// An internal error occurred
    #[error("Internal error: {message}")]
    Internal { message: String },
}

impl Error {
    /// Create a library load failure 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(),
        }
    }

    /// Create a native error from a return code
    #[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}"),
        }
    }
}