reratui 1.1.0

A modern, reactive TUI framework for Rust with React-inspired hooks and components, powered by ratatui
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
//! Context stack with proper lifecycle management.
//!
//! Provides scoped context values that are automatically cleaned up
//! when their provider fiber unmounts. This implements React-like context
//! semantics where nested providers shadow parent values.
//!
//! # Example
//!
//! ```rust,ignore
//! use reratui_fiber::context_stack::{push_context, get_context, pop_context_for_fiber};
//! use reratui_fiber::FiberId;
//!
//! // Provider pushes a value
//! push_context(FiberId(1), "theme-dark".to_string());
//!
//! // Consumer gets the value
//! let theme = get_context::<String>().unwrap();
//! assert_eq!(theme, "theme-dark");
//!
//! // Nested provider shadows the parent
//! push_context(FiberId(2), "theme-light".to_string());
//! let theme = get_context::<String>().unwrap();
//! assert_eq!(theme, "theme-light");
//!
//! // When inner provider unmounts, outer value is restored
//! pop_context_for_fiber(FiberId(2));
//! let theme = get_context::<String>().unwrap();
//! assert_eq!(theme, "theme-dark");
//! ```

use std::any::{Any, TypeId};
use std::cell::RefCell;
use std::collections::HashMap;

use crate::fiber::FiberId;

thread_local! {
    /// Thread-local context stack
    static CONTEXT_STACK: RefCell<ContextStack> = RefCell::new(ContextStack::new());
}

/// Type alias for the provider stack to reduce complexity
type ProviderStack = Vec<(FiberId, Box<dyn Any + Send + Sync>)>;

/// Context stack with proper lifecycle management.
///
/// Each context type has its own stack of values, where each value is
/// associated with the fiber that provided it. When a fiber unmounts,
/// all its context values are automatically removed.
pub struct ContextStack {
    /// Stack of values per type, with fiber ownership
    providers: HashMap<TypeId, ProviderStack>,
}

impl ContextStack {
    /// Create a new empty context stack
    pub fn new() -> Self {
        Self {
            providers: HashMap::new(),
        }
    }

    /// Push a context value (called during render).
    ///
    /// The value is associated with the given fiber and will be automatically
    /// removed when `pop_for_fiber` is called for that fiber.
    pub fn push<T: Send + Sync + 'static>(&mut self, fiber_id: FiberId, value: T) {
        let type_id = TypeId::of::<T>();
        self.providers
            .entry(type_id)
            .or_default()
            .push((fiber_id, Box::new(value)));
    }

    /// Pop all context values for a fiber (called on unmount).
    ///
    /// This removes all context values that were pushed by the given fiber,
    /// restoring any shadowed values from parent providers.
    pub fn pop_for_fiber(&mut self, fiber_id: FiberId) {
        for stack in self.providers.values_mut() {
            stack.retain(|(id, _)| *id != fiber_id);
        }
    }

    /// Get the nearest context value of type T.
    ///
    /// Returns the most recently pushed value of type T, which corresponds
    /// to the nearest ancestor provider in the component tree.
    pub fn get<T: Clone + Send + Sync + 'static>(&self) -> Option<T> {
        let type_id = TypeId::of::<T>();
        self.providers
            .get(&type_id)?
            .last()
            .and_then(|(_, value)| value.downcast_ref::<T>())
            .cloned()
    }

    /// Check if a context of type T exists.
    pub fn has<T: 'static>(&self) -> bool {
        let type_id = TypeId::of::<T>();
        self.providers
            .get(&type_id)
            .map(|stack| !stack.is_empty())
            .unwrap_or(false)
    }

    /// Clear all context values.
    pub fn clear(&mut self) {
        self.providers.clear();
    }

    /// Get the number of providers for a given type.
    #[cfg(test)]
    pub fn provider_count<T: 'static>(&self) -> usize {
        let type_id = TypeId::of::<T>();
        self.providers
            .get(&type_id)
            .map(|stack| stack.len())
            .unwrap_or(0)
    }
}

impl Default for ContextStack {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Thread-local API functions
// ============================================================================

/// Push a context value to the thread-local stack.
///
/// The value is associated with the given fiber and will be automatically
/// removed when `pop_context_for_fiber` is called for that fiber.
pub fn push_context<T: Send + Sync + 'static>(fiber_id: FiberId, value: T) {
    CONTEXT_STACK.with(|stack| {
        stack.borrow_mut().push(fiber_id, value);
    });
}

/// Get the nearest context value from the thread-local stack.
///
/// Returns the most recently pushed value of type T, which corresponds
/// to the nearest ancestor provider in the component tree.
pub fn get_context<T: Clone + Send + Sync + 'static>() -> Option<T> {
    CONTEXT_STACK.with(|stack| stack.borrow().get::<T>())
}

/// Pop all context values for a fiber from the thread-local stack.
///
/// This removes all context values that were pushed by the given fiber,
/// restoring any shadowed values from parent providers.
pub fn pop_context_for_fiber(fiber_id: FiberId) {
    CONTEXT_STACK.with(|stack| {
        stack.borrow_mut().pop_for_fiber(fiber_id);
    });
}

/// Check if a context of type T exists in the thread-local stack.
pub fn has_context<T: 'static>() -> bool {
    CONTEXT_STACK.with(|stack| stack.borrow().has::<T>())
}

/// Clear all context values from the thread-local stack.
pub fn clear_context_stack() {
    CONTEXT_STACK.with(|stack| {
        stack.borrow_mut().clear();
    });
}

/// Execute a closure with the thread-local context stack.
pub fn with_context_stack<R, F: FnOnce(&ContextStack) -> R>(f: F) -> R {
    CONTEXT_STACK.with(|stack| f(&stack.borrow()))
}

/// Execute a closure with mutable access to the thread-local context stack.
pub fn with_context_stack_mut<R, F: FnOnce(&mut ContextStack) -> R>(f: F) -> R {
    CONTEXT_STACK.with(|stack| f(&mut stack.borrow_mut()))
}

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

    #[test]
    fn test_context_stack_creation() {
        let stack = ContextStack::new();
        assert!(stack.providers.is_empty());
    }

    #[test]
    fn test_push_and_get() {
        let mut stack = ContextStack::new();
        let fiber_id = FiberId(1);

        stack.push(fiber_id, 42i32);
        assert_eq!(stack.get::<i32>(), Some(42));
    }

    #[test]
    fn test_nested_providers_shadow() {
        let mut stack = ContextStack::new();
        let fiber1 = FiberId(1);
        let fiber2 = FiberId(2);

        stack.push(fiber1, "outer".to_string());
        assert_eq!(stack.get::<String>(), Some("outer".to_string()));

        stack.push(fiber2, "inner".to_string());
        assert_eq!(stack.get::<String>(), Some("inner".to_string()));
    }

    #[test]
    fn test_pop_for_fiber() {
        let mut stack = ContextStack::new();
        let fiber1 = FiberId(1);
        let fiber2 = FiberId(2);

        stack.push(fiber1, "outer".to_string());
        stack.push(fiber2, "inner".to_string());

        stack.pop_for_fiber(fiber2);
        assert_eq!(stack.get::<String>(), Some("outer".to_string()));

        stack.pop_for_fiber(fiber1);
        assert_eq!(stack.get::<String>(), None);
    }

    #[test]
    fn test_multiple_types() {
        let mut stack = ContextStack::new();
        let fiber_id = FiberId(1);

        stack.push(fiber_id, 42i32);
        stack.push(fiber_id, "hello".to_string());

        assert_eq!(stack.get::<i32>(), Some(42));
        assert_eq!(stack.get::<String>(), Some("hello".to_string()));
    }

    #[test]
    fn test_has_context() {
        let mut stack = ContextStack::new();
        let fiber_id = FiberId(1);

        assert!(!stack.has::<i32>());

        stack.push(fiber_id, 42i32);
        assert!(stack.has::<i32>());
        assert!(!stack.has::<String>());
    }

    #[test]
    fn test_get_nonexistent_returns_none() {
        let stack = ContextStack::new();
        assert_eq!(stack.get::<i32>(), None);
    }

    #[test]
    fn test_clear() {
        let mut stack = ContextStack::new();
        let fiber_id = FiberId(1);

        stack.push(fiber_id, 42i32);
        stack.push(fiber_id, "hello".to_string());

        assert!(stack.has::<i32>());
        assert!(stack.has::<String>());

        stack.clear();

        assert!(!stack.has::<i32>());
        assert!(!stack.has::<String>());
    }

    #[test]
    fn test_provider_count() {
        let mut stack = ContextStack::new();
        let fiber1 = FiberId(1);
        let fiber2 = FiberId(2);

        assert_eq!(stack.provider_count::<i32>(), 0);

        stack.push(fiber1, 1i32);
        assert_eq!(stack.provider_count::<i32>(), 1);

        stack.push(fiber2, 2i32);
        assert_eq!(stack.provider_count::<i32>(), 2);

        stack.pop_for_fiber(fiber2);
        assert_eq!(stack.provider_count::<i32>(), 1);
    }

    #[test]
    fn test_thread_local_push_and_get() {
        clear_context_stack();

        let fiber_id = FiberId(1);
        push_context(fiber_id, 42i32);

        assert_eq!(get_context::<i32>(), Some(42));
        assert!(has_context::<i32>());

        clear_context_stack();
    }

    #[test]
    fn test_thread_local_pop_for_fiber() {
        clear_context_stack();

        let fiber1 = FiberId(1);
        let fiber2 = FiberId(2);

        push_context(fiber1, "outer".to_string());
        push_context(fiber2, "inner".to_string());

        assert_eq!(get_context::<String>(), Some("inner".to_string()));

        pop_context_for_fiber(fiber2);
        assert_eq!(get_context::<String>(), Some("outer".to_string()));

        clear_context_stack();
    }

    #[test]
    fn test_with_context_stack() {
        clear_context_stack();

        let fiber_id = FiberId(1);
        push_context(fiber_id, 42i32);

        let has_int = with_context_stack(|stack| stack.has::<i32>());
        assert!(has_int);

        clear_context_stack();
    }

    #[test]
    fn test_with_context_stack_mut() {
        clear_context_stack();

        with_context_stack_mut(|stack| {
            stack.push(FiberId(1), 42i32);
        });

        assert!(has_context::<i32>());

        clear_context_stack();
    }

    #[test]
    fn test_default_impl() {
        let stack: ContextStack = Default::default();
        assert!(!stack.has::<i32>());
    }

    #[test]
    fn test_deeply_nested_providers() {
        let mut stack = ContextStack::new();

        // Simulate a deep component tree
        for i in 1..=5 {
            stack.push(FiberId(i), format!("level-{}", i));
        }

        // Should get the innermost value
        assert_eq!(stack.get::<String>(), Some("level-5".to_string()));

        // Pop from innermost to outermost
        for i in (1..=5).rev() {
            assert_eq!(stack.get::<String>(), Some(format!("level-{}", i)));
            stack.pop_for_fiber(FiberId(i));
        }

        assert_eq!(stack.get::<String>(), None);
    }

    #[test]
    fn test_multiple_contexts_same_fiber() {
        let mut stack = ContextStack::new();
        let fiber_id = FiberId(1);

        // A single fiber can provide multiple context types
        stack.push(fiber_id, 42i32);
        stack.push(fiber_id, "theme".to_string());
        stack.push(fiber_id, true);

        assert_eq!(stack.get::<i32>(), Some(42));
        assert_eq!(stack.get::<String>(), Some("theme".to_string()));
        assert_eq!(stack.get::<bool>(), Some(true));

        // Popping the fiber removes all its contexts
        stack.pop_for_fiber(fiber_id);

        assert_eq!(stack.get::<i32>(), None);
        assert_eq!(stack.get::<String>(), None);
        assert_eq!(stack.get::<bool>(), None);
    }
}

#[cfg(test)]
mod property_tests {
    use super::*;
    use proptest::prelude::*;

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        /// **Property 11: Context Stack Shadowing**
        /// **Validates: Requirements 6.3, 6.5**
        ///
        /// For any nested context providers of the same type, use_context SHALL
        /// return the value from the innermost (most recently pushed) provider.
        #[test]
        fn prop_context_stack_shadowing(
            outer_value in any::<i32>(),
            inner_value in any::<i32>(),
            deepest_value in any::<i32>()
        ) {
            // Ensure values are different for clear testing
            prop_assume!(outer_value != inner_value);
            prop_assume!(inner_value != deepest_value);
            prop_assume!(outer_value != deepest_value);

            clear_context_stack();

            let outer_fiber = FiberId(1);
            let inner_fiber = FiberId(2);
            let deepest_fiber = FiberId(3);

            // Push outer context
            push_context(outer_fiber, outer_value);

            // Property: Should get outer value
            let retrieved = get_context::<i32>();
            prop_assert_eq!(retrieved, Some(outer_value),
                "Should get outer value when only outer provider exists");

            // Push inner context (shadows outer)
            push_context(inner_fiber, inner_value);

            // Property: Should get inner value (shadowing outer)
            let retrieved = get_context::<i32>();
            prop_assert_eq!(retrieved, Some(inner_value),
                "Should get inner value when inner provider shadows outer");

            // Push deepest context (shadows both)
            push_context(deepest_fiber, deepest_value);

            // Property: Should get deepest value (shadowing both)
            let retrieved = get_context::<i32>();
            prop_assert_eq!(retrieved, Some(deepest_value),
                "Should get deepest value when deepest provider shadows all");

            // Pop deepest - should restore inner
            pop_context_for_fiber(deepest_fiber);
            let retrieved = get_context::<i32>();
            prop_assert_eq!(retrieved, Some(inner_value),
                "Should restore inner value after deepest unmounts");

            // Pop inner - should restore outer
            pop_context_for_fiber(inner_fiber);
            let retrieved = get_context::<i32>();
            prop_assert_eq!(retrieved, Some(outer_value),
                "Should restore outer value after inner unmounts");

            // Pop outer - should have no context
            pop_context_for_fiber(outer_fiber);
            let retrieved = get_context::<i32>();
            prop_assert_eq!(retrieved, None,
                "Should have no context after all providers unmount");
        }

        /// **Property 12: Context Cleanup on Unmount**
        /// **Validates: Requirements 6.4**
        ///
        /// For any fiber that provided context values, when the fiber unmounts,
        /// all its context values SHALL be removed from the stack, restoring any
        /// shadowed values.
        #[test]
        fn prop_context_cleanup_on_unmount(
            values in prop::collection::vec(any::<i32>(), 1..10)
        ) {
            clear_context_stack();

            // Create fibers and push contexts
            let fiber_ids: Vec<FiberId> = (0..values.len())
                .map(|i| FiberId(i as u64))
                .collect();

            for (i, &value) in values.iter().enumerate() {
                push_context(fiber_ids[i], value);
            }

            // Property: Should get the last value (most recent provider)
            let retrieved = get_context::<i32>();
            prop_assert_eq!(retrieved, Some(*values.last().unwrap()),
                "Should get last value before any unmounts");

            // Unmount fibers in reverse order
            for i in (0..fiber_ids.len()).rev() {
                pop_context_for_fiber(fiber_ids[i]);

                // Property: After unmounting, should get the previous value (if any)
                let expected = if i > 0 {
                    Some(values[i - 1])
                } else {
                    None
                };
                let retrieved = get_context::<i32>();
                prop_assert_eq!(retrieved, expected,
                    "After unmounting fiber {}, should get correct value", i);
            }

            // Property: All contexts should be cleaned up
            let retrieved = get_context::<i32>();
            prop_assert_eq!(retrieved, None,
                "All contexts should be cleaned up after all unmounts");
        }

        /// **Property: Multiple context types are independent**
        /// **Validates: Requirement 6.3**
        ///
        /// Different context types should not interfere with each other.
        #[test]
        fn prop_multiple_context_types_independent(
            int_value in any::<i32>(),
            string_value in any::<String>(),
            bool_value in any::<bool>()
        ) {
            clear_context_stack();

            let fiber1 = FiberId(1);
            let fiber2 = FiberId(2);
            let fiber3 = FiberId(3);

            // Push different types
            push_context(fiber1, int_value);
            push_context(fiber2, string_value.clone());
            push_context(fiber3, bool_value);

            // Property: Each type should be retrievable independently
            let retrieved_int = get_context::<i32>();
            prop_assert_eq!(retrieved_int, Some(int_value),
                "Should get correct i32 value");

            let retrieved_string = get_context::<String>();
            prop_assert_eq!(retrieved_string, Some(string_value.clone()),
                "Should get correct String value");

            let retrieved_bool = get_context::<bool>();
            prop_assert_eq!(retrieved_bool, Some(bool_value),
                "Should get correct bool value");

            // Pop one type - others should remain
            pop_context_for_fiber(fiber2);

            let retrieved_int = get_context::<i32>();
            prop_assert_eq!(retrieved_int, Some(int_value),
                "i32 should still be available after String unmount");

            let retrieved_string = get_context::<String>();
            prop_assert_eq!(retrieved_string, None,
                "String should be gone after unmount");

            let retrieved_bool = get_context::<bool>();
            prop_assert_eq!(retrieved_bool, Some(bool_value),
                "bool should still be available after String unmount");
        }

        /// **Property: Fiber can provide multiple context types**
        /// **Validates: Requirement 6.1**
        ///
        /// A single fiber should be able to provide multiple context types,
        /// and all should be cleaned up when the fiber unmounts.
        #[test]
        fn prop_fiber_provides_multiple_types(
            int_value in any::<i32>(),
            string_value in any::<String>()
        ) {
            clear_context_stack();

            let fiber = FiberId(1);

            // Single fiber provides multiple types
            push_context(fiber, int_value);
            push_context(fiber, string_value.clone());

            // Property: Both types should be available
            let retrieved_int = get_context::<i32>();
            prop_assert_eq!(retrieved_int, Some(int_value),
                "i32 should be available");

            let retrieved_string = get_context::<String>();
            prop_assert_eq!(retrieved_string, Some(string_value),
                "String should be available");

            // Unmount the fiber
            pop_context_for_fiber(fiber);

            // Property: Both types should be cleaned up
            let retrieved_int = get_context::<i32>();
            prop_assert_eq!(retrieved_int, None,
                "i32 should be cleaned up after fiber unmount");

            let retrieved_string = get_context::<String>();
            prop_assert_eq!(retrieved_string, None,
                "String should be cleaned up after fiber unmount");
        }

        /// **Property: Context shadowing works with same fiber**
        /// **Validates: Requirement 6.5**
        ///
        /// If a fiber pushes multiple values of the same type, the last one
        /// should be the active one.
        #[test]
        fn prop_same_fiber_multiple_values(
            first_value in any::<i32>(),
            second_value in any::<i32>()
        ) {
            prop_assume!(first_value != second_value);

            clear_context_stack();

            let fiber = FiberId(1);

            // Push first value
            push_context(fiber, first_value);
            let retrieved = get_context::<i32>();
            prop_assert_eq!(retrieved, Some(first_value),
                "Should get first value");

            // Push second value from same fiber
            push_context(fiber, second_value);
            let retrieved = get_context::<i32>();
            prop_assert_eq!(retrieved, Some(second_value),
                "Should get second value (shadows first)");

            // Unmount fiber - both values should be removed
            pop_context_for_fiber(fiber);
            let retrieved = get_context::<i32>();
            prop_assert_eq!(retrieved, None,
                "Both values should be removed after fiber unmount");
        }
    }
}