rnk 0.17.3

A React-like declarative terminal UI framework for Rust, inspired by Ink
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
//! Component trait for Elm-architecture style components
//!
//! This module provides a trait-based component system that follows the Elm architecture:
//! - **Model**: The component's internal state
//! - **Msg**: Messages that can update the state
//! - **Props**: External properties passed to the component
//! - **init**: Initialize the model from props
//! - **update**: Handle messages and produce side effects
//! - **view**: Render the component to an Element tree
//!
//! # Example
//!
//! ```rust
//! use rnk::core::{Component, Element};
//! use rnk::cmd::Cmd;
//!
//! // Define props
//! #[derive(Clone, Default)]
//! struct CounterProps {
//!     initial: i32,
//! }
//!
//! // Define messages
//! enum CounterMsg {
//!     Increment,
//!     Decrement,
//! }
//!
//! // Define model (state)
//! struct CounterModel {
//!     count: i32,
//! }
//!
//! // Implement the component
//! struct Counter;
//!
//! impl Component for Counter {
//!     type Props = CounterProps;
//!     type Msg = CounterMsg;
//!     type Model = CounterModel;
//!
//!     fn init(props: &Self::Props) -> (Self::Model, Cmd) {
//!         (CounterModel { count: props.initial }, Cmd::none())
//!     }
//!
//!     fn update(model: &mut Self::Model, msg: Self::Msg) -> Cmd {
//!         match msg {
//!             CounterMsg::Increment => model.count += 1,
//!             CounterMsg::Decrement => model.count -= 1,
//!         }
//!         Cmd::none()
//!     }
//!
//!     fn view(model: &Self::Model, _props: &Self::Props) -> Element {
//!         Element::text(format!("Count: {}", model.count))
//!     }
//! }
//! ```

use crate::cmd::Cmd;
use crate::core::Element;

/// A component following the Elm architecture pattern.
///
/// Components encapsulate state (Model), handle updates via messages (Msg),
/// and render to an Element tree. This provides a predictable, testable
/// architecture for building complex UIs.
///
/// # Type Parameters
///
/// - `Props`: External properties passed to the component (should be Clone)
/// - `Msg`: Message type for state updates
/// - `Model`: Internal state type
pub trait Component: Sized {
    /// External properties passed to the component.
    ///
    /// Props are immutable from the component's perspective and are
    /// provided by the parent. They should implement Clone.
    type Props: Clone + Default;

    /// Message type for updating the component's state.
    ///
    /// Messages represent all possible state transitions. Using an enum
    /// ensures exhaustive handling of all cases.
    type Msg;

    /// The component's internal state.
    ///
    /// The model holds all mutable state for the component. It should
    /// only be modified through the `update` function.
    type Model;

    /// Initialize the component's model from props.
    ///
    /// Called once when the component is first mounted. Returns the
    /// initial model and an optional command for side effects (e.g.,
    /// fetching initial data).
    ///
    /// # Arguments
    ///
    /// * `props` - The initial properties
    ///
    /// # Returns
    ///
    /// A tuple of (initial model, initial command)
    fn init(props: &Self::Props) -> (Self::Model, Cmd);

    /// Update the model in response to a message.
    ///
    /// This is the only place where the model should be mutated.
    /// Returns a command for any side effects that should occur.
    ///
    /// # Arguments
    ///
    /// * `model` - Mutable reference to the current model
    /// * `msg` - The message to handle
    ///
    /// # Returns
    ///
    /// A command describing side effects to execute
    fn update(model: &mut Self::Model, msg: Self::Msg) -> Cmd;

    /// Render the component to an Element tree.
    ///
    /// This function should be pure - given the same model and props,
    /// it should always return the same Element tree.
    ///
    /// # Arguments
    ///
    /// * `model` - Reference to the current model
    /// * `props` - Reference to the current props
    ///
    /// # Returns
    ///
    /// The Element tree representing this component's UI
    fn view(model: &Self::Model, props: &Self::Props) -> Element;

    /// Determine if the component should re-render.
    ///
    /// Called before `view` to optimize rendering. If this returns false,
    /// the previous render result is reused.
    ///
    /// Default implementation always returns true (always re-render).
    ///
    /// # Arguments
    ///
    /// * `_model` - Reference to the current model
    /// * `_old_props` - Reference to the previous props
    /// * `_new_props` - Reference to the new props
    ///
    /// # Returns
    ///
    /// true if the component should re-render, false otherwise
    #[allow(unused_variables)]
    fn should_render(
        model: &Self::Model,
        old_props: &Self::Props,
        new_props: &Self::Props,
    ) -> bool {
        true
    }

    /// Called when the component is about to be unmounted.
    ///
    /// Use this for cleanup tasks like canceling subscriptions or
    /// releasing resources.
    ///
    /// Default implementation does nothing.
    ///
    /// # Arguments
    ///
    /// * `_model` - Reference to the current model
    #[allow(unused_variables)]
    fn unmount(model: &Self::Model) {}

    /// Called after the component is first mounted.
    ///
    /// Use this for initialization tasks that require the component to be
    /// fully mounted, such as starting subscriptions or fetching data.
    /// Returns a command for any side effects that should occur.
    ///
    /// Default implementation returns no command.
    ///
    /// # Arguments
    ///
    /// * `_model` - Reference to the current model
    ///
    /// # Returns
    ///
    /// A command describing side effects to execute after mount
    #[allow(unused_variables)]
    fn on_mount(model: &Self::Model) -> Cmd {
        Cmd::none()
    }
}

/// A stateless component that only depends on props.
///
/// Stateless components are simpler and more efficient when no internal
/// state is needed. They're essentially pure functions from Props to Element.
///
/// # Example
///
/// ```rust
/// use rnk::core::{StatelessComponent, Element};
///
/// #[derive(Clone, Default)]
/// struct GreetingProps {
///     name: String,
/// }
///
/// struct Greeting;
///
/// impl StatelessComponent for Greeting {
///     type Props = GreetingProps;
///
///     fn render(props: &Self::Props) -> Element {
///         Element::text(format!("Hello, {}!", props.name))
///     }
/// }
/// ```
pub trait StatelessComponent: Sized {
    /// External properties passed to the component.
    type Props: Clone + Default;

    /// Render the component to an Element tree.
    ///
    /// # Arguments
    ///
    /// * `props` - Reference to the current props
    ///
    /// # Returns
    ///
    /// The Element tree representing this component's UI
    fn render(props: &Self::Props) -> Element;

    /// Determine if the component should re-render.
    ///
    /// Default implementation always returns true.
    #[allow(unused_variables)]
    fn should_render(old_props: &Self::Props, new_props: &Self::Props) -> bool {
        true
    }
}

/// Blanket implementation: StatelessComponent automatically implements Component
impl<T: StatelessComponent> Component for T {
    type Props = <T as StatelessComponent>::Props;
    type Msg = (); // No messages for stateless components
    type Model = (); // No model for stateless components

    fn init(_props: &Self::Props) -> (Self::Model, Cmd) {
        ((), Cmd::none())
    }

    fn update(_model: &mut Self::Model, _msg: Self::Msg) -> Cmd {
        Cmd::none()
    }

    fn view(_model: &Self::Model, props: &Self::Props) -> Element {
        T::render(props)
    }

    fn should_render(
        _model: &Self::Model,
        old_props: &Self::Props,
        new_props: &Self::Props,
    ) -> bool {
        T::should_render(old_props, new_props)
    }
}

/// Runtime state for a mounted component instance.
///
/// This struct holds the runtime state needed to manage a component
/// throughout its lifecycle.
pub struct ComponentInstance<C: Component> {
    /// The component's current model
    pub model: C::Model,
    /// The component's current props
    pub props: C::Props,
    /// Pending commands to execute
    pending_cmds: Vec<Cmd>,
}

impl<C: Component> ComponentInstance<C> {
    /// Create a new component instance with the given props.
    ///
    /// # Arguments
    ///
    /// * `props` - Initial properties for the component
    ///
    /// # Returns
    ///
    /// A new ComponentInstance with initialized model
    pub fn new(props: C::Props) -> Self {
        let (model, cmd) = C::init(&props);
        let mut instance = Self {
            model,
            props,
            pending_cmds: Vec::new(),
        };
        if !cmd.is_none() {
            instance.pending_cmds.push(cmd);
        }
        // Call on_mount lifecycle hook
        let mount_cmd = C::on_mount(&instance.model);
        if !mount_cmd.is_none() {
            instance.pending_cmds.push(mount_cmd);
        }
        instance
    }

    /// Send a message to update the component.
    ///
    /// # Arguments
    ///
    /// * `msg` - The message to send
    pub fn send(&mut self, msg: C::Msg) {
        let cmd = C::update(&mut self.model, msg);
        if !cmd.is_none() {
            self.pending_cmds.push(cmd);
        }
    }

    /// Update the component's props.
    ///
    /// # Arguments
    ///
    /// * `new_props` - The new properties
    ///
    /// # Returns
    ///
    /// true if the component should re-render
    pub fn update_props(&mut self, new_props: C::Props) -> bool {
        let should_render = C::should_render(&self.model, &self.props, &new_props);
        self.props = new_props;
        should_render
    }

    /// Render the component to an Element tree.
    ///
    /// # Returns
    ///
    /// The Element tree representing this component's UI
    pub fn view(&self) -> Element {
        C::view(&self.model, &self.props)
    }

    /// Take all pending commands.
    ///
    /// # Returns
    ///
    /// A vector of pending commands, leaving the internal queue empty
    pub fn take_cmds(&mut self) -> Vec<Cmd> {
        std::mem::take(&mut self.pending_cmds)
    }

    /// Check if there are pending commands.
    ///
    /// # Returns
    ///
    /// true if there are pending commands
    pub fn has_pending_cmds(&self) -> bool {
        !self.pending_cmds.is_empty()
    }

    /// Unmount the component, performing cleanup.
    pub fn unmount(self) {
        C::unmount(&self.model);
    }
}

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

    // Test props
    #[derive(Clone, Default)]
    struct TestProps {
        value: i32,
    }

    // Test messages
    enum TestMsg {
        Increment,
        Set(i32),
    }

    // Test model
    struct TestModel {
        count: i32,
    }

    // Test component
    struct TestComponent;

    impl Component for TestComponent {
        type Props = TestProps;
        type Msg = TestMsg;
        type Model = TestModel;

        fn init(props: &Self::Props) -> (Self::Model, Cmd) {
            (TestModel { count: props.value }, Cmd::none())
        }

        fn update(model: &mut Self::Model, msg: Self::Msg) -> Cmd {
            match msg {
                TestMsg::Increment => model.count += 1,
                TestMsg::Set(v) => model.count = v,
            }
            Cmd::none()
        }

        fn view(model: &Self::Model, _props: &Self::Props) -> Element {
            Element::text(format!("Count: {}", model.count))
        }

        fn should_render(
            _model: &Self::Model,
            old_props: &Self::Props,
            new_props: &Self::Props,
        ) -> bool {
            old_props.value != new_props.value
        }
    }

    #[test]
    fn test_component_init() {
        let props = TestProps { value: 42 };
        let (model, cmd) = TestComponent::init(&props);
        assert_eq!(model.count, 42);
        assert!(cmd.is_none());
    }

    #[test]
    fn test_component_update() {
        let props = TestProps { value: 0 };
        let (mut model, _) = TestComponent::init(&props);

        TestComponent::update(&mut model, TestMsg::Increment);
        assert_eq!(model.count, 1);

        TestComponent::update(&mut model, TestMsg::Set(100));
        assert_eq!(model.count, 100);
    }

    #[test]
    fn test_component_instance() {
        let props = TestProps { value: 10 };
        let mut instance = ComponentInstance::<TestComponent>::new(props);

        assert_eq!(instance.model.count, 10);

        instance.send(TestMsg::Increment);
        assert_eq!(instance.model.count, 11);

        instance.send(TestMsg::Set(50));
        assert_eq!(instance.model.count, 50);
    }

    #[test]
    fn test_component_instance_props_update() {
        let props = TestProps { value: 10 };
        let mut instance = ComponentInstance::<TestComponent>::new(props);

        // Same value - should not re-render
        let should_render = instance.update_props(TestProps { value: 10 });
        assert!(!should_render);

        // Different value - should re-render
        let should_render = instance.update_props(TestProps { value: 20 });
        assert!(should_render);
    }

    // Test stateless component
    #[derive(Clone, Default)]
    struct StatelessProps {
        text: String,
    }

    struct StatelessTest;

    impl StatelessComponent for StatelessTest {
        type Props = StatelessProps;

        fn render(props: &Self::Props) -> Element {
            Element::text(props.text.clone())
        }
    }

    #[test]
    fn test_stateless_component() {
        let props = StatelessProps {
            text: "Hello".to_string(),
        };
        let element = StatelessTest::render(&props);
        // Element should be created (we can't easily inspect it, but it shouldn't panic)
        let _ = element;
    }

    #[test]
    fn test_stateless_as_component() {
        // StatelessComponent should work as Component via blanket impl
        let props = StatelessProps {
            text: "Test".to_string(),
        };
        let (model, cmd) = <StatelessTest as Component>::init(&props);
        assert_eq!(model, ());
        assert!(cmd.is_none());

        let mut model = ();
        let cmd = <StatelessTest as Component>::update(&mut model, ());
        assert!(cmd.is_none());
    }

    // Test component with lifecycle hooks
    use std::sync::Arc;
    use std::sync::atomic::{AtomicBool, Ordering};

    #[derive(Clone, Default)]
    struct LifecycleProps {
        mounted: Arc<AtomicBool>,
        unmounted: Arc<AtomicBool>,
    }

    struct LifecycleModel {
        mounted: Arc<AtomicBool>,
        unmounted: Arc<AtomicBool>,
    }

    struct LifecycleComponent;

    impl Component for LifecycleComponent {
        type Props = LifecycleProps;
        type Msg = ();
        type Model = LifecycleModel;

        fn init(props: &Self::Props) -> (Self::Model, Cmd) {
            (
                LifecycleModel {
                    mounted: props.mounted.clone(),
                    unmounted: props.unmounted.clone(),
                },
                Cmd::none(),
            )
        }

        fn update(_model: &mut Self::Model, _msg: Self::Msg) -> Cmd {
            Cmd::none()
        }

        fn view(_model: &Self::Model, _props: &Self::Props) -> Element {
            Element::text("")
        }

        fn on_mount(model: &Self::Model) -> Cmd {
            model.mounted.store(true, Ordering::SeqCst);
            Cmd::none()
        }

        fn unmount(model: &Self::Model) {
            model.unmounted.store(true, Ordering::SeqCst);
        }
    }

    #[test]
    fn test_lifecycle_hooks() {
        let mounted = Arc::new(AtomicBool::new(false));
        let unmounted = Arc::new(AtomicBool::new(false));

        let props = LifecycleProps {
            mounted: mounted.clone(),
            unmounted: unmounted.clone(),
        };

        // Create instance - on_mount should be called
        let instance = ComponentInstance::<LifecycleComponent>::new(props);
        assert!(mounted.load(Ordering::SeqCst), "on_mount should be called");
        assert!(
            !unmounted.load(Ordering::SeqCst),
            "unmount should not be called yet"
        );

        // Unmount - unmount should be called
        instance.unmount();
        assert!(
            unmounted.load(Ordering::SeqCst),
            "unmount should be called after unmount()"
        );
    }
}