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
//! Virtual Node abstraction for reconciliation
//!
//! VNode provides a lightweight, stable representation of UI elements
//! that enables efficient diffing and incremental updates.

use std::any::TypeId;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

use super::Style;

/// Stable node identifier that doesn't change on clone
///
/// Unlike ElementId which generates a new ID on each clone,
/// NodeKey provides stable identity for reconciliation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct NodeKey {
    /// User-provided key (for list items)
    pub user_key: Option<u64>,
    /// Component type identifier
    pub type_id: TypeId,
    /// Index within parent's children
    pub index: usize,
}

impl NodeKey {
    /// Create a new NodeKey with a user-provided key
    pub fn with_key(key: &(impl Hash + ?Sized), type_id: TypeId, index: usize) -> Self {
        let mut hasher = DefaultHasher::new();
        key.hash(&mut hasher);
        Self {
            user_key: Some(hasher.finish()),
            type_id,
            index,
        }
    }

    /// Create a new NodeKey without a user key
    pub fn new(type_id: TypeId, index: usize) -> Self {
        Self {
            user_key: None,
            type_id,
            index,
        }
    }

    /// Create a root NodeKey
    pub fn root() -> Self {
        Self {
            user_key: None,
            type_id: TypeId::of::<()>(),
            index: 0,
        }
    }

    /// Check if this key matches another for reconciliation
    ///
    /// Keys match if:
    /// - Both have user keys and they're equal, OR
    /// - Neither has user key and type_id + index match
    pub fn matches(&self, other: &NodeKey) -> bool {
        match (self.user_key, other.user_key) {
            (Some(a), Some(b)) => a == b && self.type_id == other.type_id,
            (None, None) => self.type_id == other.type_id && self.index == other.index,
            _ => false,
        }
    }
}

/// Virtual node type
#[derive(Debug, Clone, PartialEq)]
pub enum VNodeType {
    /// Root container
    Root,
    /// Box container element
    Box,
    /// Text element with content
    Text(String),
    /// Component reference (stores component type ID)
    Component(TypeId),
}

impl VNodeType {
    /// Get the TypeId for this node type
    pub fn type_id(&self) -> TypeId {
        match self {
            VNodeType::Root => TypeId::of::<RootMarker>(),
            VNodeType::Box => TypeId::of::<BoxMarker>(),
            VNodeType::Text(_) => TypeId::of::<TextMarker>(),
            VNodeType::Component(id) => *id,
        }
    }
}

// Marker types for TypeId generation
struct RootMarker;
struct BoxMarker;
struct TextMarker;

/// Properties for a virtual node
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Props {
    /// Layout and visual style
    pub style: Style,
    /// Key for list reconciliation
    pub key: Option<String>,
    /// Horizontal scroll offset
    pub scroll_offset_x: Option<u16>,
    /// Vertical scroll offset
    pub scroll_offset_y: Option<u16>,
}

impl Props {
    /// Create new empty props
    pub fn new() -> Self {
        Self::default()
    }

    /// Create props with style
    pub fn with_style(style: Style) -> Self {
        Self {
            style,
            ..Default::default()
        }
    }

    /// Set the key
    pub fn key(mut self, key: impl Into<String>) -> Self {
        self.key = Some(key.into());
        self
    }

    /// Set scroll offset
    pub fn scroll(mut self, x: Option<u16>, y: Option<u16>) -> Self {
        self.scroll_offset_x = x;
        self.scroll_offset_y = y;
        self
    }

    /// Convert to taffy style
    pub fn to_taffy(&self) -> taffy::Style {
        self.style.to_taffy()
    }

    /// Check if props have changed (for shouldComponentUpdate)
    pub fn has_changed(&self, other: &Props) -> bool {
        self != other
    }
}

/// Virtual Node - lightweight UI description for reconciliation
///
/// VNode is the core abstraction for the reconciliation system.
/// It provides a stable, comparable representation of UI elements
/// that can be efficiently diffed to produce minimal updates.
#[derive(Debug, Clone)]
pub struct VNode {
    /// Stable node identifier
    pub key: NodeKey,
    /// Node type (Box, Text, Component)
    pub node_type: VNodeType,
    /// Node properties
    pub props: Props,
    /// Child nodes
    pub children: Vec<VNode>,
}

impl VNode {
    /// Create a new VNode
    pub fn new(node_type: VNodeType, props: Props) -> Self {
        let type_id = node_type.type_id();
        Self {
            key: NodeKey::new(type_id, 0),
            node_type,
            props,
            children: Vec::new(),
        }
    }

    /// Create a root VNode
    pub fn root() -> Self {
        Self {
            key: NodeKey::root(),
            node_type: VNodeType::Root,
            props: Props::new(),
            children: Vec::new(),
        }
    }

    /// Create a box VNode
    pub fn box_node() -> Self {
        Self::new(VNodeType::Box, Props::new())
    }

    /// Create a text VNode
    pub fn text(content: impl Into<String>) -> Self {
        Self::new(VNodeType::Text(content.into()), Props::new())
    }

    /// Create a component VNode
    pub fn component<C: 'static>() -> Self {
        Self::new(VNodeType::Component(TypeId::of::<C>()), Props::new())
    }

    /// Set the user key for list reconciliation
    pub fn with_key(mut self, key: &(impl Hash + ?Sized)) -> Self {
        let type_id = self.node_type.type_id();
        self.key = NodeKey::with_key(key, type_id, self.key.index);
        self
    }

    /// Set the index within parent
    pub fn with_index(mut self, index: usize) -> Self {
        self.key.index = index;
        self
    }

    /// Set props
    pub fn with_props(mut self, props: Props) -> Self {
        self.props = props;
        self
    }

    /// Set style
    pub fn with_style(mut self, style: Style) -> Self {
        self.props.style = style;
        self
    }

    /// Add a child node
    pub fn child(mut self, child: VNode) -> Self {
        let index = self.children.len();
        self.children.push(child.with_index(index));
        self
    }

    /// Add multiple children
    pub fn children(mut self, children: impl IntoIterator<Item = VNode>) -> Self {
        for child in children {
            self = self.child(child);
        }
        self
    }

    /// Get text content if this is a text node
    pub fn get_text(&self) -> Option<&str> {
        match &self.node_type {
            VNodeType::Text(s) => Some(s),
            _ => None,
        }
    }

    /// Check if this is a text node
    pub fn is_text(&self) -> bool {
        matches!(self.node_type, VNodeType::Text(_))
    }

    /// Check if this is a box node
    pub fn is_box(&self) -> bool {
        matches!(self.node_type, VNodeType::Box)
    }

    /// Check if this is a component node
    pub fn is_component(&self) -> bool {
        matches!(self.node_type, VNodeType::Component(_))
    }

    /// Check if this is the root node
    pub fn is_root(&self) -> bool {
        matches!(self.node_type, VNodeType::Root)
    }

    /// Count total nodes in tree
    pub fn node_count(&self) -> usize {
        1 + self.children.iter().map(|c| c.node_count()).sum::<usize>()
    }

    /// Find a node by key
    pub fn find_by_key(&self, key: &NodeKey) -> Option<&VNode> {
        if self.key == *key {
            return Some(self);
        }
        for child in &self.children {
            if let Some(found) = child.find_by_key(key) {
                return Some(found);
            }
        }
        None
    }

    /// Find a mutable node by key
    pub fn find_by_key_mut(&mut self, key: &NodeKey) -> Option<&mut VNode> {
        if self.key == *key {
            return Some(self);
        }
        for child in &mut self.children {
            if let Some(found) = child.find_by_key_mut(key) {
                return Some(found);
            }
        }
        None
    }
}

impl PartialEq for VNode {
    fn eq(&self, other: &Self) -> bool {
        self.key == other.key
            && self.node_type == other.node_type
            && self.props == other.props
            && self.children == other.children
    }
}

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

    #[test]
    fn test_node_key_creation() {
        let key1 = NodeKey::new(TypeId::of::<i32>(), 0);
        let key2 = NodeKey::new(TypeId::of::<i32>(), 0);
        assert_eq!(key1, key2);

        let key3 = NodeKey::new(TypeId::of::<i32>(), 1);
        assert_ne!(key1, key3);
    }

    #[test]
    fn test_node_key_with_user_key() {
        let key1 = NodeKey::with_key("item-1", TypeId::of::<i32>(), 0);
        let key2 = NodeKey::with_key("item-1", TypeId::of::<i32>(), 5);
        // Same user key, different index - should still match
        assert!(key1.matches(&key2));

        let key3 = NodeKey::with_key("item-2", TypeId::of::<i32>(), 0);
        assert!(!key1.matches(&key3));
    }

    #[test]
    fn test_node_key_matches() {
        let key1 = NodeKey::new(TypeId::of::<i32>(), 0);
        let key2 = NodeKey::new(TypeId::of::<i32>(), 0);
        assert!(key1.matches(&key2));

        let key3 = NodeKey::new(TypeId::of::<String>(), 0);
        assert!(!key1.matches(&key3));
    }

    #[test]
    fn test_vnode_creation() {
        let node = VNode::box_node();
        assert!(node.is_box());
        assert!(node.children.is_empty());
    }

    #[test]
    fn test_vnode_text() {
        let node = VNode::text("Hello");
        assert!(node.is_text());
        assert_eq!(node.get_text(), Some("Hello"));
    }

    #[test]
    fn test_vnode_children() {
        let node = VNode::box_node()
            .child(VNode::text("A"))
            .child(VNode::text("B"))
            .child(VNode::text("C"));

        assert_eq!(node.children.len(), 3);
        assert_eq!(node.children[0].key.index, 0);
        assert_eq!(node.children[1].key.index, 1);
        assert_eq!(node.children[2].key.index, 2);
    }

    #[test]
    fn test_vnode_with_key() {
        let node = VNode::box_node().with_key("my-key");
        assert!(node.key.user_key.is_some());
    }

    #[test]
    fn test_vnode_node_count() {
        let node = VNode::box_node().child(VNode::text("A")).child(
            VNode::box_node()
                .child(VNode::text("B"))
                .child(VNode::text("C")),
        );

        assert_eq!(node.node_count(), 5);
    }

    #[test]
    fn test_vnode_find_by_key() {
        let _target_key = NodeKey::with_key(&"target", TypeId::of::<TextMarker>(), 0);
        let node = VNode::box_node()
            .child(VNode::box_node().child(VNode::text("Found").with_key("target")));

        // The key won't match exactly because with_key generates a new key
        // But we can verify the structure
        assert_eq!(node.node_count(), 3);
    }

    #[test]
    fn test_props_has_changed() {
        let props1 = Props::new();
        let props2 = Props::new();
        assert!(!props1.has_changed(&props2));

        let props3 = Props::new().key("different");
        assert!(props1.has_changed(&props3));
    }

    #[test]
    fn test_vnode_equality() {
        let node1 = VNode::text("Hello");
        let node2 = VNode::text("Hello");
        // Different instances have different keys by default
        // but same content
        assert_eq!(node1.node_type, node2.node_type);
    }

    #[test]
    fn test_vnode_component() {
        struct MyComponent;
        let node = VNode::component::<MyComponent>();
        assert!(node.is_component());

        if let VNodeType::Component(type_id) = node.node_type {
            assert_eq!(type_id, TypeId::of::<MyComponent>());
        } else {
            panic!("Expected Component type");
        }
    }
}