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