kql_language_tools/
classification.rs

1//! Classification types and services for KQL syntax highlighting
2//!
3//! This module provides types and functionality for classifying KQL syntax
4//! elements for syntax highlighting purposes.
5
6use serde::{Deserialize, Serialize};
7
8/// Classification kind for syntax highlighting
9///
10/// These values match the `ClassificationKind` enum from Kusto.Language
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
12#[serde(rename_all = "PascalCase")]
13pub enum ClassificationKind {
14    /// Plain text (no special highlighting)
15    PlainText,
16    /// A comment
17    Comment,
18    /// Punctuation characters: (), ;:
19    Punctuation,
20    /// A directive: #
21    Directive,
22    /// A non-string literal (number, boolean, etc.)
23    Literal,
24    /// A string literal
25    StringLiteral,
26    /// A type name
27    Type,
28    /// An identifier
29    Identifier,
30    /// A column name
31    Column,
32    /// A table name
33    Table,
34    /// A database name
35    Database,
36    /// A scalar function
37    ScalarFunction,
38    /// An aggregate function
39    AggregateFunction,
40    /// A keyword
41    Keyword,
42    /// An operator
43    Operator,
44    /// A variable
45    Variable,
46    /// A parameter
47    Parameter,
48    /// A command keyword
49    CommandKeyword,
50    /// A query operator (pipe operators like where, project, etc.)
51    QueryOperator,
52    /// A scalar operator (mathematical/logical operators)
53    ScalarOperator,
54    /// A materializable expression
55    MaterializedViewFunction,
56    /// Plugin name
57    Plugin,
58    /// Option name
59    Option,
60    /// Client directive
61    ClientDirective,
62    /// Query parameter
63    QueryParameter,
64    /// Cluster name
65    Cluster,
66}
67
68impl ClassificationKind {
69    /// Parse from a string
70    #[allow(dead_code)]
71    #[must_use]
72    pub fn parse(s: &str) -> Self {
73        match s {
74            "Comment" => Self::Comment,
75            "Punctuation" => Self::Punctuation,
76            "Directive" => Self::Directive,
77            "Literal" => Self::Literal,
78            "StringLiteral" => Self::StringLiteral,
79            "Type" => Self::Type,
80            "Identifier" => Self::Identifier,
81            "Column" => Self::Column,
82            "Table" => Self::Table,
83            "Database" => Self::Database,
84            "ScalarFunction" => Self::ScalarFunction,
85            "AggregateFunction" => Self::AggregateFunction,
86            "Keyword" => Self::Keyword,
87            "Operator" => Self::Operator,
88            "Variable" => Self::Variable,
89            "Parameter" => Self::Parameter,
90            "CommandKeyword" => Self::CommandKeyword,
91            "QueryOperator" => Self::QueryOperator,
92            "ScalarOperator" => Self::ScalarOperator,
93            "MaterializedViewFunction" => Self::MaterializedViewFunction,
94            "Plugin" => Self::Plugin,
95            "Option" => Self::Option,
96            "ClientDirective" => Self::ClientDirective,
97            "QueryParameter" => Self::QueryParameter,
98            "Cluster" => Self::Cluster,
99            // "PlainText" and unknown values default to PlainText
100            _ => Self::PlainText,
101        }
102    }
103}
104
105/// A classified span for syntax highlighting
106#[derive(Debug, Clone, Serialize, Deserialize)]
107pub struct ClassifiedSpan {
108    /// Start offset (0-based)
109    pub start: usize,
110    /// Length of the span
111    pub length: usize,
112    /// Classification kind
113    pub kind: ClassificationKind,
114}
115
116/// Result of syntax classification
117#[derive(Debug, Clone, Default, Serialize, Deserialize)]
118pub struct ClassificationResult {
119    /// Classified spans
120    pub spans: Vec<ClassifiedSpan>,
121}