typespec_rs 0.4.2

A Rust implementation of the TypeSpec type system — parser, checker, and emitter
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//! Visibility system for TypeSpec properties
//!
//! Ported from TypeSpec compiler/src/core/visibility/
//!
//! The visibility system determines when properties of a conceptual resource
//! are present. It's based on visibility classes (represented by TypeSpec enums)
//! with visibility modifiers (enum members).
//!
//! NOTE: This is a simplified port. The full TS implementation uses Program-scoped
//! state (WeakMap, useStateMap/useStateSet) which requires infrastructure not yet
//! available. The core data structures and logic are preserved.

use crate::checker::types::TypeId;
use std::collections::{HashMap, HashSet};

// ============================================================================
// Lifecycle visibility (ported from visibility/lifecycle.ts)
// ============================================================================

/// Get the Lifecycle visibility enum TypeId from the standard library.
/// Ported from TS getLifecycleVisibilityEnum().
/// Returns None if the TypeSpec.Lifecycle enum is not available (e.g., stdlib not loaded).
pub fn get_lifecycle_visibility_enum(checker: &crate::checker::Checker) -> Option<TypeId> {
    checker.get_std_type("Lifecycle")
}

/// Visibility modifiers per visibility class (enum type)
pub type VisibilityModifiers = HashMap<TypeId, HashSet<TypeId>>;

/// Visibility store for tracking property visibility
#[derive(Debug, Default)]
pub struct VisibilityStore {
    /// Map of property TypeId → visibility modifiers
    store: HashMap<TypeId, VisibilityModifiers>,
    /// Set of properties with sealed visibility
    sealed_properties: HashSet<TypeId>,
    /// Set of properties with sealed visibility per class
    sealed_classes: HashMap<TypeId, HashSet<TypeId>>,
    /// Default modifier sets per visibility class
    default_modifiers: HashMap<TypeId, HashSet<TypeId>>,
    /// Whether the entire program's visibility is sealed
    program_sealed: bool,
}

impl VisibilityStore {
    pub fn new() -> Self {
        Self::default()
    }

    /// Check if a property's visibility is sealed
    pub fn is_sealed(&self, property_id: TypeId, visibility_class: Option<TypeId>) -> bool {
        if self.program_sealed {
            return true;
        }
        if self.sealed_properties.contains(&property_id) {
            return true;
        }
        if let Some(class_id) = visibility_class
            && let Some(sealed) = self.sealed_classes.get(&property_id)
        {
            return sealed.contains(&class_id);
        }
        false
    }

    /// Seal a property's visibility modifiers
    pub fn seal_visibility(&mut self, property_id: TypeId, visibility_class: Option<TypeId>) {
        if let Some(class_id) = visibility_class {
            self.sealed_classes
                .entry(property_id)
                .or_default()
                .insert(class_id);
        } else {
            self.sealed_properties.insert(property_id);
        }
    }

    /// Seal all visibility modifiers for the program
    pub fn seal_program(&mut self) {
        self.program_sealed = true;
    }

    /// Get or initialize visibility modifiers for a property
    pub fn get_or_initialize(&mut self, property_id: TypeId) -> &mut VisibilityModifiers {
        self.store.entry(property_id).or_default()
    }

    /// Get the visibility modifiers for a property and class
    pub fn get_visibility_for_class(
        &mut self,
        property_id: TypeId,
        visibility_class: TypeId,
    ) -> HashSet<TypeId> {
        let modifiers = self.store.entry(property_id).or_default();
        modifiers
            .get(&visibility_class)
            .cloned()
            .unwrap_or_else(|| self.get_default_modifier_set(visibility_class))
    }

    /// Add visibility modifiers to a property
    pub fn add_visibility_modifiers(
        &mut self,
        property_id: TypeId,
        class_id: TypeId,
        modifiers: &[TypeId],
    ) -> bool {
        if self.is_sealed(property_id, Some(class_id)) {
            return false;
        }
        let vis = self.store.entry(property_id).or_default();
        let set = vis.entry(class_id).or_default();
        for &m in modifiers {
            set.insert(m);
        }
        true
    }

    /// Remove visibility modifiers from a property
    pub fn remove_visibility_modifiers(
        &mut self,
        property_id: TypeId,
        class_id: TypeId,
        modifiers: &[TypeId],
    ) -> bool {
        if self.is_sealed(property_id, Some(class_id)) {
            return false;
        }
        let default_set = self.get_default_modifier_set(class_id);
        let vis = self.store.entry(property_id).or_default();
        let set = vis.entry(class_id).or_insert(default_set);
        for &m in modifiers {
            set.remove(&m);
        }
        true
    }

    /// Get the default modifier set for a visibility class
    fn get_default_modifier_set(&self, visibility_class: TypeId) -> HashSet<TypeId> {
        self.default_modifiers
            .get(&visibility_class)
            .cloned()
            .unwrap_or_default()
    }

    /// Set the default modifier set for a visibility class
    pub fn set_default_modifier_set(
        &mut self,
        visibility_class: TypeId,
        default_set: HashSet<TypeId>,
    ) {
        self.default_modifiers.insert(visibility_class, default_set);
    }

    /// Clear all visibility modifiers for a property in a given class
    pub fn clear_visibility_for_class(&mut self, property_id: TypeId, class_id: TypeId) {
        if let Some(modifiers) = self.store.get_mut(&property_id) {
            modifiers.remove(&class_id);
        }
    }

    /// Reset visibility modifiers for a property in a given class to the default set
    pub fn reset_visibility_for_class(&mut self, property_id: TypeId, class_id: TypeId) {
        self.clear_visibility_for_class(property_id, class_id);
        if let Some(defaults) = self.default_modifiers.get(&class_id) {
            let entry = self.store.entry(property_id).or_default();
            entry.insert(class_id, defaults.clone());
        }
    }
}

/// A visibility filter that determines if a property is visible
#[derive(Debug, Clone, Default)]
pub struct VisibilityFilter {
    /// Property must have ALL of these visibility modifiers
    pub all: Option<HashSet<TypeId>>,
    /// Property must have ANY of these visibility modifiers
    pub any: Option<HashSet<TypeId>>,
    /// Property must have NONE of these visibility modifiers
    pub none: Option<HashSet<TypeId>>,
}

/// Check if a property is visible according to the given filter
pub fn is_visible(
    store: &mut VisibilityStore,
    property_id: TypeId,
    filter: &VisibilityFilter,
) -> bool {
    // Check ALL constraint
    if let Some(ref all) = filter.all {
        for &modifier_id in all {
            // Simplified: just check if modifier is in any class's set
            if let Some(modifiers) = store.store.get(&property_id) {
                let found = modifiers.values().any(|set| set.contains(&modifier_id));
                if !found {
                    return false;
                }
            } else {
                return false;
            }
        }
    }

    // Check NONE constraint
    if let Some(ref none) = filter.none {
        for &modifier_id in none {
            if let Some(modifiers) = store.store.get(&property_id) {
                let found = modifiers.values().any(|set| set.contains(&modifier_id));
                if found {
                    return false;
                }
            }
        }
    }

    // Check ANY constraint
    if let Some(ref any) = filter.any {
        if any.is_empty() {
            return false;
        }
        let mut found_any = false;
        for &modifier_id in any {
            if let Some(modifiers) = store.store.get(&property_id)
                && modifiers.values().any(|set| set.contains(&modifier_id))
            {
                found_any = true;
                break;
            }
        }
        if !found_any {
            return false;
        }
    }

    true
}

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

    #[test]
    fn test_visibility_store_seal_property() {
        let mut store = VisibilityStore::new();
        assert!(!store.is_sealed(1, None));
        store.seal_visibility(1, None);
        assert!(store.is_sealed(1, None));
    }

    #[test]
    fn test_visibility_store_seal_class() {
        let mut store = VisibilityStore::new();
        assert!(!store.is_sealed(1, Some(10)));
        store.seal_visibility(1, Some(10));
        assert!(store.is_sealed(1, Some(10)));
        assert!(!store.is_sealed(1, Some(20)));
    }

    #[test]
    fn test_visibility_store_seal_program() {
        let mut store = VisibilityStore::new();
        store.seal_program();
        assert!(store.is_sealed(1, None));
        assert!(store.is_sealed(1, Some(10)));
    }

    #[test]
    fn test_add_visibility_modifiers() {
        let mut store = VisibilityStore::new();
        assert!(store.add_visibility_modifiers(1, 10, &[100, 101]));
        let vis = store.get_visibility_for_class(1, 10);
        assert!(vis.contains(&100));
        assert!(vis.contains(&101));
    }

    #[test]
    fn test_add_visibility_when_sealed() {
        let mut store = VisibilityStore::new();
        store.seal_visibility(1, Some(10));
        assert!(!store.add_visibility_modifiers(1, 10, &[100]));
    }

    #[test]
    fn test_remove_visibility_modifiers() {
        let mut store = VisibilityStore::new();
        store.add_visibility_modifiers(1, 10, &[100, 101]);
        assert!(store.remove_visibility_modifiers(1, 10, &[100]));
        let vis = store.get_visibility_for_class(1, 10);
        assert!(!vis.contains(&100));
        assert!(vis.contains(&101));
    }

    #[test]
    fn test_default_modifier_set() {
        let mut store = VisibilityStore::new();
        store.set_default_modifier_set(10, HashSet::from([100, 101, 102]));
        let vis = store.get_visibility_for_class(1, 10);
        assert_eq!(vis.len(), 3);
    }

    #[test]
    fn test_is_visible_with_all_filter() {
        let mut store = VisibilityStore::new();
        store.add_visibility_modifiers(1, 10, &[100, 101]);
        let filter = VisibilityFilter {
            all: Some(HashSet::from([100, 101])),
            ..Default::default()
        };
        assert!(is_visible(&mut store, 1, &filter));
    }

    #[test]
    fn test_is_visible_with_all_filter_missing() {
        let mut store = VisibilityStore::new();
        store.add_visibility_modifiers(1, 10, &[100]);
        let filter = VisibilityFilter {
            all: Some(HashSet::from([100, 101])),
            ..Default::default()
        };
        assert!(!is_visible(&mut store, 1, &filter));
    }

    #[test]
    fn test_is_visible_with_any_filter() {
        let mut store = VisibilityStore::new();
        store.add_visibility_modifiers(1, 10, &[100]);
        let filter = VisibilityFilter {
            any: Some(HashSet::from([100, 101])),
            ..Default::default()
        };
        assert!(is_visible(&mut store, 1, &filter));
    }

    #[test]
    fn test_is_visible_with_any_filter_empty() {
        let mut store = VisibilityStore::new();
        let filter = VisibilityFilter {
            any: Some(HashSet::new()),
            ..Default::default()
        };
        assert!(!is_visible(&mut store, 1, &filter));
    }

    #[test]
    fn test_is_visible_with_none_filter() {
        let mut store = VisibilityStore::new();
        store.add_visibility_modifiers(1, 10, &[100, 101]);
        let filter = VisibilityFilter {
            none: Some(HashSet::from([102])),
            ..Default::default()
        };
        assert!(is_visible(&mut store, 1, &filter));
    }

    #[test]
    fn test_is_visible_with_none_filter_excluded() {
        let mut store = VisibilityStore::new();
        store.add_visibility_modifiers(1, 10, &[100]);
        let filter = VisibilityFilter {
            none: Some(HashSet::from([100])),
            ..Default::default()
        };
        assert!(!is_visible(&mut store, 1, &filter));
    }

    // ============================================================================
    // Additional visibility tests (ported from TS visibility.test.ts)
    // ============================================================================

    #[test]
    fn test_default_visibility_no_modifiers() {
        // A property with no visibility modifiers should not be visible with any filter
        let mut store = VisibilityStore::new();
        let filter = VisibilityFilter {
            any: Some(HashSet::from([100])),
            ..Default::default()
        };
        assert!(!is_visible(&mut store, 1, &filter));
    }

    #[test]
    fn test_visibility_multiple_classes() {
        // A property can have visibility in multiple classes
        let mut store = VisibilityStore::new();
        store.add_visibility_modifiers(1, 10, &[100]);
        store.add_visibility_modifiers(1, 20, &[200]);
        // Should be visible in class 10
        let vis_10 = store.get_visibility_for_class(1, 10);
        assert!(vis_10.contains(&100));
        // Should be visible in class 20
        let vis_20 = store.get_visibility_for_class(1, 20);
        assert!(vis_20.contains(&200));
    }

    #[test]
    fn test_visibility_add_same_modifier_twice() {
        // Adding the same modifier twice should be idempotent
        let mut store = VisibilityStore::new();
        assert!(store.add_visibility_modifiers(1, 10, &[100]));
        assert!(store.add_visibility_modifiers(1, 10, &[100]));
        let vis = store.get_visibility_for_class(1, 10);
        assert!(vis.contains(&100));
    }

    #[test]
    fn test_visibility_remove_nonexistent() {
        // Removing a modifier that doesn't exist should return true (no error)
        let mut store = VisibilityStore::new();
        assert!(store.remove_visibility_modifiers(1, 10, &[100]));
    }

    #[test]
    fn test_visibility_clear_modifiers() {
        // Clear all visibility for a class
        let mut store = VisibilityStore::new();
        store.add_visibility_modifiers(1, 10, &[100, 101]);
        store.clear_visibility_for_class(1, 10);
        let vis = store.get_visibility_for_class(1, 10);
        assert!(vis.is_empty());
    }

    #[test]
    fn test_visibility_reset_modifiers() {
        // Reset visibility to default for a class
        let mut store = VisibilityStore::new();
        store.set_default_modifier_set(10, HashSet::from([100, 101]));
        store.add_visibility_modifiers(1, 10, &[200]);
        store.reset_visibility_for_class(1, 10);
        let vis = store.get_visibility_for_class(1, 10);
        // Should be back to defaults
        assert!(vis.contains(&100));
        assert!(vis.contains(&101));
        assert!(!vis.contains(&200));
    }

    #[test]
    fn test_visibility_seal_then_add_fails() {
        // Adding modifiers after sealing should fail
        let mut store = VisibilityStore::new();
        store.add_visibility_modifiers(1, 10, &[100]);
        store.seal_visibility(1, Some(10));
        assert!(!store.add_visibility_modifiers(1, 10, &[101]));
        // Original modifier should still be there
        let vis = store.get_visibility_for_class(1, 10);
        assert!(vis.contains(&100));
        assert!(!vis.contains(&101));
    }

    #[test]
    fn test_visibility_seal_program_then_add_fails() {
        // Adding modifiers after sealing the program should fail
        let mut store = VisibilityStore::new();
        store.add_visibility_modifiers(1, 10, &[100]);
        store.seal_program();
        assert!(!store.add_visibility_modifiers(2, 10, &[100]));
    }

    #[test]
    fn test_visibility_remove_when_sealed_fails() {
        // Removing modifiers after sealing should fail
        let mut store = VisibilityStore::new();
        store.add_visibility_modifiers(1, 10, &[100]);
        store.seal_visibility(1, Some(10));
        assert!(!store.remove_visibility_modifiers(1, 10, &[100]));
    }

    #[test]
    fn test_is_visible_combined_filters() {
        // Test with both all and any filters
        let mut store = VisibilityStore::new();
        store.add_visibility_modifiers(1, 10, &[100, 101]);
        let filter = VisibilityFilter {
            all: Some(HashSet::from([100])),
            any: Some(HashSet::from([101])),
            ..Default::default()
        };
        assert!(is_visible(&mut store, 1, &filter));
    }

    #[test]
    fn test_is_visible_combined_filters_fails() {
        // Combined filter where one part fails
        let mut store = VisibilityStore::new();
        store.add_visibility_modifiers(1, 10, &[100]);
        let filter = VisibilityFilter {
            all: Some(HashSet::from([100, 101])), // 101 not present
            any: Some(HashSet::from([100])),
            ..Default::default()
        };
        assert!(!is_visible(&mut store, 1, &filter));
    }

    #[test]
    fn test_is_visible_no_filters() {
        // No filters means everything is visible
        let mut store = VisibilityStore::new();
        let filter = VisibilityFilter::default();
        assert!(is_visible(&mut store, 1, &filter));
    }

    #[test]
    fn test_visibility_default_modifiers_applied_to_new_property() {
        // When a default modifier set exists, new properties get those defaults
        let mut store = VisibilityStore::new();
        store.set_default_modifier_set(10, HashSet::from([100, 101]));
        let vis = store.get_visibility_for_class(1, 10);
        assert!(vis.contains(&100));
        assert!(vis.contains(&101));
    }

    #[test]
    fn test_visibility_preserves_other_classes() {
        // Modifying visibility for one class shouldn't affect another
        let mut store = VisibilityStore::new();
        store.add_visibility_modifiers(1, 10, &[100]);
        store.add_visibility_modifiers(1, 20, &[200]);
        store.clear_visibility_for_class(1, 10);
        // Class 10 should be empty
        assert!(store.get_visibility_for_class(1, 10).is_empty());
        // Class 20 should still have 200
        assert!(store.get_visibility_for_class(1, 20).contains(&200));
    }

    #[test]
    fn test_visibility_multiple_properties() {
        // Multiple properties with different visibility
        let mut store = VisibilityStore::new();
        store.add_visibility_modifiers(1, 10, &[100]);
        store.add_visibility_modifiers(2, 10, &[101]);
        let filter = VisibilityFilter {
            any: Some(HashSet::from([100])),
            ..Default::default()
        };
        assert!(is_visible(&mut store, 1, &filter));
        assert!(!is_visible(&mut store, 2, &filter));
    }

    #[test]
    fn test_visibility_none_filter_with_multiple_classes() {
        // None filter should exclude based on any class
        let mut store = VisibilityStore::new();
        store.add_visibility_modifiers(1, 10, &[100]);
        store.add_visibility_modifiers(1, 20, &[200]);
        let filter = VisibilityFilter {
            none: Some(HashSet::from([100])),
            ..Default::default()
        };
        assert!(!is_visible(&mut store, 1, &filter));
    }

    #[test]
    fn test_visibility_all_filter_with_multiple_classes() {
        // All filter should require modifiers across all classes
        let mut store = VisibilityStore::new();
        store.add_visibility_modifiers(1, 10, &[100]);
        store.add_visibility_modifiers(1, 20, &[200]);
        let filter = VisibilityFilter {
            all: Some(HashSet::from([100, 200])),
            ..Default::default()
        };
        assert!(is_visible(&mut store, 1, &filter));
    }

    // ==================== Lifecycle visibility tests ====================

    #[test]
    fn test_get_lifecycle_visibility_enum() {
        use crate::parser::parse;
        let result = parse("");
        let mut checker = crate::checker::Checker::new();
        checker.set_parse_result(result.root_id, result.builder);
        checker.check_program();
        // Without stdlib loaded, Lifecycle enum may not be available
        let result = get_lifecycle_visibility_enum(&checker);
        // Just verify the function doesn't panic
        let _ = result;
    }
}