graphlite 0.0.1

GraphLite - A lightweight ISO GQL Graph Database
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
// Copyright (c) 2024-2025 DeepGraph Inc.
// SPDX-License-Identifier: Apache-2.0
//
//! Type validation for ISO GQL
//!
//! Implements type checking and validation rules using TypeSpec

use crate::types::{GqlType, GraphTypeSpec, TypeError, TypeResult};

/// Runtime type constraints for validation
#[derive(Debug, Clone)]
#[allow(dead_code)] // ROADMAP v0.4.0 - Runtime type constraints for data validation
pub struct TypeConstraints {
    pub allow_null: bool,
    pub max_collection_size: Option<usize>,
    pub max_numeric_precision: Option<u8>,
}

impl Default for TypeConstraints {
    fn default() -> Self {
        Self {
            allow_null: true,
            max_collection_size: None,
            max_numeric_precision: None,
        }
    }
}

/// Type validator
#[derive(Debug)]
pub struct TypeValidator;

impl TypeValidator {
    /// Check if two types are compatible (for assignment/comparison)
    pub fn are_compatible(left: &GqlType, right: &GqlType) -> bool {
        match (left, right) {
            // Same type is always compatible
            _ if left == right => true,

            // Numeric type compatibility - can implicit cast between numeric types
            (
                GqlType::SmallInt,
                GqlType::Integer | GqlType::BigInt | GqlType::Int128 | GqlType::Int256,
            ) => true,
            (GqlType::Integer, GqlType::BigInt | GqlType::Int128 | GqlType::Int256) => true,
            (GqlType::BigInt, GqlType::Int128 | GqlType::Int256) => true,
            (GqlType::Int128, GqlType::Int256) => true,

            // Integer to decimal
            (
                GqlType::SmallInt
                | GqlType::Integer
                | GqlType::BigInt
                | GqlType::Int128
                | GqlType::Int256,
                GqlType::Decimal { .. },
            ) => true,

            // Integer to float
            (
                GqlType::SmallInt | GqlType::Integer,
                GqlType::Float { .. } | GqlType::Real | GqlType::Double,
            ) => true,

            // Float widening
            (GqlType::Float { .. }, GqlType::Double) => true,
            (GqlType::Real, GqlType::Double) => true,

            // String types with different max lengths
            (
                GqlType::String {
                    max_length: Some(l_max),
                },
                GqlType::String {
                    max_length: Some(r_max),
                },
            ) => l_max <= r_max,
            (
                GqlType::String {
                    max_length: Some(_),
                },
                GqlType::String { max_length: None },
            ) => true, // Bounded to unbounded is ok
            (
                GqlType::String { max_length: None },
                GqlType::String {
                    max_length: Some(_),
                },
            ) => false, // Unbounded to bounded is not ok
            (GqlType::String { max_length: None }, GqlType::String { max_length: None }) => true,

            // List type compatibility
            (
                GqlType::List {
                    element_type: l_elem,
                    max_length: l_max,
                },
                GqlType::List {
                    element_type: r_elem,
                    max_length: r_max,
                },
            ) => {
                // Check element type compatibility first
                if !Self::are_compatible(l_elem, r_elem) {
                    return false;
                }
                // Check max_length constraints (similar to strings)
                match (l_max, r_max) {
                    (Some(l), Some(r)) => l <= r, // Left max must be <= right max
                    (Some(_), None) => true,      // Bounded to unbounded is ok
                    (None, Some(_)) => false,     // Unbounded to bounded is not ok
                    (None, None) => true,         // Both unbounded is ok
                }
            }

            // Reference type compatibility
            (
                GqlType::Reference {
                    target_type: Some(l),
                },
                GqlType::Reference {
                    target_type: Some(r),
                },
            ) => Self::are_compatible(l, r),

            // Temporal type compatibility
            (
                l @ (GqlType::Date
                | GqlType::Time { .. }
                | GqlType::Timestamp { .. }
                | GqlType::ZonedTime { .. }
                | GqlType::ZonedDateTime { .. }
                | GqlType::LocalTime { .. }
                | GqlType::LocalDateTime { .. }),
                r @ (GqlType::Date
                | GqlType::Time { .. }
                | GqlType::Timestamp { .. }
                | GqlType::ZonedTime { .. }
                | GqlType::ZonedDateTime { .. }
                | GqlType::LocalTime { .. }
                | GqlType::LocalDateTime { .. }),
            ) => Self::are_temporal_compatible(l, r),

            _ => false,
        }
    }

    /// Check if temporal types are compatible
    fn are_temporal_compatible(left: &GqlType, right: &GqlType) -> bool {
        match (left, right) {
            // Same temporal type
            _ if std::mem::discriminant(left) == std::mem::discriminant(right) => true,

            // Date to timestamp conversions
            (
                GqlType::Date,
                GqlType::Timestamp { .. }
                | GqlType::ZonedDateTime { .. }
                | GqlType::LocalDateTime { .. },
            ) => true,

            // Time conversions
            (GqlType::Time { .. }, GqlType::ZonedTime { .. } | GqlType::LocalTime { .. }) => true,
            (GqlType::ZonedTime { .. }, GqlType::Time { .. } | GqlType::LocalTime { .. }) => true,
            (GqlType::LocalTime { .. }, GqlType::Time { .. }) => true,

            _ => false,
        }
    }

    /// Validate list size constraints
    #[allow(dead_code)] // ROADMAP v0.4.0 - List size validation for schema constraints
    pub fn validate_list_size(list_type: &GqlType, size: usize) -> bool {
        match list_type {
            GqlType::List {
                max_length: Some(max),
                ..
            } => size <= *max,
            GqlType::List {
                max_length: None, ..
            } => true,
            _ => false,
        }
    }

    /// Validate that a value type matches expected type
    #[allow(dead_code)] // ROADMAP v0.5.0 - Type validation for static analysis (see ROADMAP.md §7)
    pub fn validate_value_type(value_type: &GqlType, expected_type: &GqlType) -> TypeResult<()> {
        if Self::are_compatible(value_type, expected_type) {
            Ok(())
        } else {
            Err(TypeError::TypeMismatch {
                expected: format!("{}", expected_type),
                actual: format!("{}", value_type),
            })
        }
    }

    /// Validate function argument types
    pub fn validate_function_args(
        function_name: &str,
        arg_types: &[GqlType],
        expected_types: &[GqlType],
        variadic: bool,
    ) -> TypeResult<()> {
        if !variadic && arg_types.len() != expected_types.len() {
            return Err(TypeError::InvalidTypeSpecification(format!(
                "Function {} expects {} arguments, got {}",
                function_name,
                expected_types.len(),
                arg_types.len()
            )));
        }

        let check_count = if variadic {
            expected_types.len().min(arg_types.len())
        } else {
            expected_types.len()
        };

        for i in 0..check_count {
            if !Self::are_compatible(&arg_types[i], &expected_types[i]) {
                return Err(TypeError::TypeMismatch {
                    expected: format!("{}", expected_types[i]),
                    actual: format!("{}", arg_types[i]),
                });
            }
        }

        Ok(())
    }

    /// Validate graph schema compliance (simplified)
    #[allow(dead_code)] // ROADMAP v0.5.0 - Type validation for static analysis (see ROADMAP.md §7)
    pub fn validate_graph_element(
        _graph_type: &GraphTypeSpec,
        element_type: &GqlType,
    ) -> TypeResult<()> {
        // Simplified validation - just check if it's a graph-related type
        match element_type {
            GqlType::Graph { .. } => Ok(()),
            _ => Err(TypeError::GraphSchemaViolation(
                "Element is not a graph type".to_string(),
            )),
        }
    }

    /// Validate nullability constraints
    #[allow(dead_code)] // ROADMAP v0.5.0 - Type validation for static analysis (see ROADMAP.md §7)
    pub fn validate_nullability(_value_type: &GqlType, allow_null: bool) -> TypeResult<()> {
        // Note: TypeSpec doesn't have a Null variant - null handling is done at the Value level
        if !allow_null {
            // In a full implementation, we'd check if the value is null at runtime
            // For now, we'll assume all types can be nullable
            Err(TypeError::NullabilityViolation(
                "Null value not allowed in non-nullable context".to_string(),
            ))
        } else {
            Ok(())
        }
    }

    /// Validate collection element types (simplified)
    #[allow(dead_code)] // ROADMAP v0.5.0 - Type validation for static analysis (see ROADMAP.md §7)
    pub fn validate_collection_elements(
        list_type: &GqlType,
        element_types: &[GqlType],
    ) -> TypeResult<()> {
        match list_type {
            GqlType::List {
                element_type,
                max_length,
            } => {
                // Check size constraint
                if let Some(max) = max_length {
                    if element_types.len() > *max {
                        return Err(TypeError::CollectionTypeMismatch(format!(
                            "List size {} exceeds maximum {}",
                            element_types.len(),
                            max
                        )));
                    }
                }

                // Check element types
                for element_type_actual in element_types {
                    if !Self::are_compatible(element_type_actual, element_type) {
                        return Err(TypeError::CollectionTypeMismatch(format!(
                            "Element type {:?} not compatible with list element type {:?}",
                            element_type_actual, element_type
                        )));
                    }
                }

                Ok(())
            }
            _ => Err(TypeError::CollectionTypeMismatch(
                "Not a list type".to_string(),
            )),
        }
    }

    /// Enhanced validation for deeply nested complex types
    #[allow(dead_code)] // ROADMAP v0.5.0 - Type validation for static analysis (see ROADMAP.md §7)
    pub fn validate_nested_type_structure(
        value_type: &GqlType,
        expected_type: &GqlType,
        max_depth: usize,
    ) -> TypeResult<()> {
        Self::validate_nested_type_structure_impl(value_type, expected_type, max_depth, 0)
    }

    #[allow(dead_code)] // ROADMAP v0.5.0 - Type validation for static analysis (see ROADMAP.md §7)
    fn validate_nested_type_structure_impl(
        value_type: &GqlType,
        expected_type: &GqlType,
        max_depth: usize,
        current_depth: usize,
    ) -> TypeResult<()> {
        // Prevent infinite recursion
        if current_depth > max_depth {
            return Err(TypeError::InvalidTypeSpecification(
                "Type structure too deeply nested".to_string(),
            ));
        }

        match (value_type, expected_type) {
            // Direct type match
            _ if Self::are_compatible(value_type, expected_type) => Ok(()),

            // Nested list validation
            (
                GqlType::List {
                    element_type: value_elem,
                    max_length: value_max,
                },
                GqlType::List {
                    element_type: expected_elem,
                    max_length: expected_max,
                },
            ) => {
                // Validate max length constraint
                match (value_max, expected_max) {
                    (Some(v_max), Some(e_max)) if v_max > e_max => {
                        return Err(TypeError::CollectionTypeMismatch(format!(
                            "List max length {} exceeds expected maximum {}",
                            v_max, e_max
                        )));
                    }
                    _ => {}
                }

                // Recursively validate element types
                Self::validate_nested_type_structure_impl(
                    value_elem,
                    expected_elem,
                    max_depth,
                    current_depth + 1,
                )
            }

            // Nested reference validation
            (
                GqlType::Reference {
                    target_type: Some(value_target),
                },
                GqlType::Reference {
                    target_type: Some(expected_target),
                },
            ) => Self::validate_nested_type_structure_impl(
                value_target,
                expected_target,
                max_depth,
                current_depth + 1,
            ),

            // Reference dereferencing validation
            (
                GqlType::Reference {
                    target_type: Some(value_target),
                },
                expected_type,
            ) => Self::validate_nested_type_structure_impl(
                value_target,
                expected_type,
                max_depth,
                current_depth + 1,
            ),

            _ => Err(TypeError::TypeMismatch {
                expected: format!("{}", expected_type),
                actual: format!("{}", value_type),
            }),
        }
    }

    /// Validate reference target type resolution
    #[allow(dead_code)] // ROADMAP v0.5.0 - Type validation for static analysis (see ROADMAP.md §7)
    pub fn validate_reference_target(
        ref_type: &GqlType,
        target_value_type: &GqlType,
    ) -> TypeResult<()> {
        match ref_type {
            GqlType::Reference {
                target_type: Some(expected_target),
            } => Self::validate_value_type(target_value_type, expected_target),
            GqlType::Reference { target_type: None } => {
                // Untyped reference - accepts any type
                Ok(())
            }
            _ => Err(TypeError::InvalidTypeSpecification(
                "Not a reference type".to_string(),
            )),
        }
    }

    /// Validate complex type constraints at runtime
    #[allow(dead_code)] // ROADMAP v0.5.0 - Type validation for static analysis (see ROADMAP.md §7)
    pub fn validate_runtime_constraints(
        value_type: &GqlType,
        constraints: &TypeConstraints,
    ) -> TypeResult<()> {
        // Check nullability
        if !constraints.allow_null && Self::is_nullable_value(value_type) {
            return Err(TypeError::NullabilityViolation(
                "Null value not allowed".to_string(),
            ));
        }

        // Check size constraints for collections
        if let Some(max_size) = constraints.max_collection_size {
            match value_type {
                GqlType::List { max_length, .. } => {
                    if let Some(list_max) = max_length {
                        if *list_max > max_size {
                            return Err(TypeError::CollectionTypeMismatch(format!(
                                "List max size {} exceeds constraint {}",
                                list_max, max_size
                            )));
                        }
                    }
                }
                GqlType::String { max_length } => {
                    if let Some(str_max) = max_length {
                        if *str_max > max_size {
                            return Err(TypeError::InvalidTypeSpecification(format!(
                                "String max length {} exceeds constraint {}",
                                str_max, max_size
                            )));
                        }
                    }
                }
                _ => {}
            }
        }

        // Check precision constraints for numeric types
        if let Some(max_precision) = constraints.max_numeric_precision {
            match value_type {
                GqlType::Decimal {
                    precision: Some(p), ..
                } if *p > max_precision => {
                    return Err(TypeError::NumericOverflow(format!(
                        "Decimal precision {} exceeds maximum {}",
                        p, max_precision
                    )));
                }
                GqlType::Float { precision: Some(p) } if *p > max_precision => {
                    return Err(TypeError::NumericOverflow(format!(
                        "Float precision {} exceeds maximum {}",
                        p, max_precision
                    )));
                }
                _ => {}
            }
        }

        Ok(())
    }

    /// Check if a type represents a nullable value
    #[allow(dead_code)] // ROADMAP v0.5.0 - Type validation for static analysis (see ROADMAP.md §7)
    fn is_nullable_value(value_type: &GqlType) -> bool {
        match value_type {
            GqlType::Reference { target_type: None } => true,
            _ => false,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_type_compatibility() {
        assert!(TypeValidator::are_compatible(
            &GqlType::Boolean,
            &GqlType::Boolean
        ));
        assert!(TypeValidator::are_compatible(
            &GqlType::Integer,
            &GqlType::BigInt
        ));
        assert!(!TypeValidator::are_compatible(
            &GqlType::BigInt,
            &GqlType::Integer
        ));
    }

    #[test]
    fn test_string_compatibility() {
        let unbounded = GqlType::String { max_length: None };
        let bounded_10 = GqlType::String {
            max_length: Some(10),
        };
        let bounded_20 = GqlType::String {
            max_length: Some(20),
        };

        assert!(TypeValidator::are_compatible(&bounded_10, &unbounded));
        assert!(!TypeValidator::are_compatible(&unbounded, &bounded_10));
        assert!(TypeValidator::are_compatible(&bounded_10, &bounded_20));
        assert!(!TypeValidator::are_compatible(&bounded_20, &bounded_10));
    }

    #[test]
    fn test_list_validation() {
        let list_type = GqlType::List {
            element_type: Box::new(GqlType::Integer),
            max_length: Some(3),
        };

        let elements = vec![GqlType::Integer, GqlType::SmallInt];
        assert!(TypeValidator::validate_collection_elements(&list_type, &elements).is_ok());

        let too_many = vec![GqlType::Integer; 4];
        assert!(TypeValidator::validate_collection_elements(&list_type, &too_many).is_err());
    }
}