Skip to main content

eventql_parser/typing/
mod.rs

1use serde::Serialize;
2
3pub mod analysis;
4
5/// A reference to a type stored in the [`TypeArena`](crate::arena::TypeArena).
6#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize)]
7pub struct TypeRef(pub(crate) usize);
8
9/// A reference to a record definition stored in the [`TypeArena`](crate::arena::TypeArena).
10#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize)]
11pub struct Record {
12    pub(crate) id: usize,
13    pub(crate) open: bool,
14}
15
16/// A reference to a function argument type list stored in the [`TypeArena`](crate::arena::TypeArena).
17#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize)]
18pub struct ArgsRef(pub(crate) usize);
19/// Represents function argument types with optional parameter support.
20///
21/// This type allows defining functions that have both required and optional parameters.
22/// The `needed` field specifies how many arguments are required, while `values` contains
23/// all possible argument types (both required and optional).
24///
25#[derive(Debug, Serialize, PartialEq, Eq, Hash, Clone, Copy)]
26pub struct FunArgs {
27    /// All argument types, including both required and optional parameters
28    pub values: ArgsRef,
29    /// Number of required arguments (must be <= values.len())
30    pub needed: usize,
31}
32
33/// Type information for expressions.
34///
35/// This enum represents the type of expressions in the EventQL type system.
36/// Types can be inferred during semantic analysis or left as `Unspecified`.
37#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize)]
38pub enum Type {
39    /// Type has not been determined yet
40    #[default]
41    Unspecified,
42    /// Numeric type (f64)
43    Number,
44    /// String type
45    String,
46    /// Boolean type
47    Bool,
48    /// Array type
49    Array(TypeRef),
50    /// Record (object) type
51    Record(Record),
52    /// Subject pattern type
53    Subject,
54    /// Function type with support for optional parameters.
55    ///
56    /// The `args` field uses [`FunArgs`] to support both required and optional parameters.
57    /// Optional parameters are indicated when `args.needed < args.values.len()`.
58    App {
59        /// Function argument types, supporting optional parameters
60        args: FunArgs,
61        /// Return type of the function
62        result: TypeRef,
63        /// Whether this is an aggregate function (operates on grouped data)
64        aggregate: bool,
65    },
66    /// Date type (e.g., `2026-01-03`)
67    ///
68    /// Used when a field is explicitly converted to a date using the `AS DATE` syntax.
69    Date,
70    /// Time type (e.g., `13:45:39`)
71    ///
72    /// Used when a field is explicitly converted to a time using the `AS TIME` syntax.
73    Time,
74    /// DateTime type (e.g., `2026-01-01T13:45:39Z`)
75    ///
76    /// Used when a field is explicitly converted to a datetime using the `AS DATETIME` syntax.
77    DateTime,
78}
79
80impl Type {
81    /// Returns the inner [`Record`] reference, panicking if this is not a `Type::Record`.
82    pub fn as_record_or_panic(&self) -> Record {
83        if let Self::Record(r) = self {
84            return *r;
85        }
86
87        panic!("expected record type, got {:?}", self);
88    }
89}