zed 0.2.0

A minimal, Redux-like state management library for Rust with advanced features.
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
// Tests to validate that examples work correctly and produce expected results

use serde::{Deserialize, Serialize};
use std::sync::{Arc, Mutex};
use std::thread;
use zed::*;

// Test structures similar to examples
#[derive(Clone, Debug, Serialize, Deserialize)]
struct GameState {
    player: Player,
    world: World,
    ui: UIState,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
struct Player {
    name: String,
    level: u32,
    experience: u32,
    health: u32,
    inventory: Vec<Item>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
struct World {
    current_level: String,
    npcs: Vec<Npc>,
    items: Vec<Item>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
struct UIState {
    current_menu: String,
    notifications: Vec<String>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
struct Item {
    id: String,
    name: String,
    description: String,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
struct Npc {
    id: String,
    name: String,
    dialogue: Vec<String>,
}

#[derive(Clone, Debug)]
enum GameAction {
    PlayerLevelUp,
    PlayerTakeDamage {
        damage: u32,
    },
    PlayerGainExperience {
        exp: u32,
    },
    PlayerPickupItem {
        item: Item,
    },
    WorldChangeLevel {
        level: String,
    },
    WorldSpawnNpc {
        npc: Npc,
    },
    #[allow(dead_code)]
    UIShowNotification {
        message: String,
    },
    UIChangeMenu {
        menu: String,
    },
}

#[derive(Clone, Debug)]
struct CounterState {
    value: i32,
    is_loading: bool,
    error: Option<String>,
}

#[derive(Clone, Debug)]
enum CounterAction {
    Increment,
    Decrement,
    SetValue(i32),
    StartLoading,
    StopLoading,
    SetError(String),
    Reset,
}

#[derive(Clone, Debug)]
struct DocumentState {
    #[allow(dead_code)]
    content: String,
    #[allow(dead_code)]
    cursor_position: usize,
    #[allow(dead_code)]
    version: u32,
}

mod examples_tests {
    use super::*;

    #[test]
    fn test_game_state_example_functionality() {
        let initial_state = GameState {
            player: Player {
                name: "Hero".to_string(),
                level: 1,
                experience: 0,
                health: 100,
                inventory: vec![],
            },
            world: World {
                current_level: "Forest".to_string(),
                npcs: vec![],
                items: vec![],
            },
            ui: UIState {
                current_menu: "main".to_string(),
                notifications: vec![],
            },
        };

        let store = configure_store(
            initial_state,
            create_reducer(|state: &GameState, action: &GameAction| {
                let mut new_state = state.clone();
                match action {
                    GameAction::PlayerLevelUp => {
                        new_state.player.level += 1;
                        new_state.player.health = 100;
                        new_state
                            .ui
                            .notifications
                            .push("Level Up! You feel stronger.".to_string());
                    }
                    GameAction::PlayerTakeDamage { damage } => {
                        if new_state.player.health > *damage {
                            new_state.player.health -= damage;
                        } else {
                            new_state.player.health = 0;
                            new_state
                                .ui
                                .notifications
                                .push("You have been defeated!".to_string());
                        }
                    }
                    GameAction::PlayerGainExperience { exp } => {
                        new_state.player.experience += exp;
                        if new_state.player.experience >= new_state.player.level * 100 {
                            new_state.player.experience = 0;
                            new_state.player.level += 1;
                            new_state.player.health = 100;
                            new_state
                                .ui
                                .notifications
                                .push("Level Up! You feel stronger.".to_string());
                        }
                    }
                    GameAction::PlayerPickupItem { item } => {
                        new_state.player.inventory.push(item.clone());
                        new_state
                            .ui
                            .notifications
                            .push(format!("Picked up: {}", item.name));
                    }
                    GameAction::WorldChangeLevel { level } => {
                        new_state.world.current_level = level.clone();
                        new_state.world.npcs.clear();
                        new_state.world.items.clear();
                        new_state.ui.notifications.push(format!("Entered: {level}"));
                    }
                    GameAction::WorldSpawnNpc { npc } => {
                        new_state.world.npcs.push(npc.clone());
                        new_state
                            .ui
                            .notifications
                            .push(format!("A {} appears!", npc.name));
                    }
                    GameAction::UIShowNotification { message } => {
                        new_state.ui.notifications.push(message.clone());
                    }
                    GameAction::UIChangeMenu { menu } => {
                        new_state.ui.current_menu = menu.clone();
                    }
                }
                new_state
            }),
        );

        // Test initial state
        let state = store.get_state();
        assert_eq!(state.player.name, "Hero");
        assert_eq!(state.player.level, 1);
        assert_eq!(state.player.health, 100);
        assert_eq!(state.world.current_level, "Forest");
        assert_eq!(state.ui.current_menu, "main");

        // Test level up
        store.dispatch(GameAction::PlayerLevelUp);
        let state = store.get_state();
        assert_eq!(state.player.level, 2);
        assert_eq!(state.player.health, 100);
        assert!(
            state
                .ui
                .notifications
                .contains(&"Level Up! You feel stronger.".to_string())
        );

        // Test damage
        store.dispatch(GameAction::PlayerTakeDamage { damage: 30 });
        let state = store.get_state();
        assert_eq!(state.player.health, 70);

        // Test experience gain and auto level up
        store.dispatch(GameAction::PlayerGainExperience { exp: 200 });
        let state = store.get_state();
        assert_eq!(state.player.level, 3);
        assert_eq!(state.player.experience, 0);
        assert_eq!(state.player.health, 100);

        // Test item pickup
        let sword = Item {
            id: "sword_1".to_string(),
            name: "Iron Sword".to_string(),
            description: "A sturdy iron sword".to_string(),
        };
        store.dispatch(GameAction::PlayerPickupItem {
            item: sword.clone(),
        });
        let state = store.get_state();
        assert_eq!(state.player.inventory.len(), 1);
        assert_eq!(state.player.inventory[0].name, "Iron Sword");

        // Test world change
        store.dispatch(GameAction::WorldChangeLevel {
            level: "Mountain".to_string(),
        });
        let state = store.get_state();
        assert_eq!(state.world.current_level, "Mountain");
        assert_eq!(state.world.npcs.len(), 0);
        assert_eq!(state.world.items.len(), 0);

        // Test Npc spawn
        let goblin = Npc {
            id: "goblin_1".to_string(),
            name: "Goblin".to_string(),
            dialogue: vec!["Grr!".to_string()],
        };
        store.dispatch(GameAction::WorldSpawnNpc {
            npc: goblin.clone(),
        });
        let state = store.get_state();
        assert_eq!(state.world.npcs.len(), 1);
        assert_eq!(state.world.npcs[0].name, "Goblin");

        // Test UI menu change
        store.dispatch(GameAction::UIChangeMenu {
            menu: "inventory".to_string(),
        });
        let state = store.get_state();
        assert_eq!(state.ui.current_menu, "inventory");

        // Test serialization
        let serialized = serde_json::to_string(&state).unwrap();
        let deserialized: GameState = serde_json::from_str(&serialized).unwrap();
        assert_eq!(deserialized.player.name, state.player.name);
        assert_eq!(deserialized.world.current_level, state.world.current_level);
    }

    #[test]
    fn test_counter_example_functionality() {
        let initial_state = CounterState {
            value: 0,
            is_loading: false,
            error: None,
        };

        let store = configure_store(
            initial_state,
            create_reducer(|state: &CounterState, action: &CounterAction| {
                let mut new_state = state.clone();
                match action {
                    CounterAction::Increment => new_state.value += 1,
                    CounterAction::Decrement => new_state.value -= 1,
                    CounterAction::SetValue(val) => new_state.value = *val,
                    CounterAction::StartLoading => new_state.is_loading = true,
                    CounterAction::StopLoading => new_state.is_loading = false,
                    CounterAction::SetError(msg) => new_state.error = Some(msg.clone()),
                    CounterAction::Reset => {
                        new_state.value = 0;
                        new_state.is_loading = false;
                        new_state.error = None;
                    }
                }
                new_state
            }),
        );

        // Test initial state
        let state = store.get_state();
        assert_eq!(state.value, 0);
        assert!(!state.is_loading);
        assert_eq!(state.error, None);

        // Test increment
        store.dispatch(CounterAction::Increment);
        let state = store.get_state();
        assert_eq!(state.value, 1);

        // Test decrement
        store.dispatch(CounterAction::Decrement);
        let state = store.get_state();
        assert_eq!(state.value, 0);

        // Test set value
        store.dispatch(CounterAction::SetValue(42));
        let state = store.get_state();
        assert_eq!(state.value, 42);

        // Test loading state
        store.dispatch(CounterAction::StartLoading);
        let state = store.get_state();
        assert!(state.is_loading);

        store.dispatch(CounterAction::StopLoading);
        let state = store.get_state();
        assert!(!state.is_loading);

        // Test error state
        store.dispatch(CounterAction::SetError("Test error".to_string()));
        let state = store.get_state();
        assert_eq!(state.error, Some("Test error".to_string()));

        // Test reset
        store.dispatch(CounterAction::Reset);
        let state = store.get_state();
        assert_eq!(state.value, 0);
        assert!(!state.is_loading);
        assert_eq!(state.error, None);
    }

    #[test]
    fn test_reactive_system_example() {
        let initial_state = CounterState {
            value: 0,
            is_loading: false,
            error: None,
        };

        let mut reactive_system = ReactiveSystem::new(initial_state);

        // Track notifications
        let notifications = Arc::new(Mutex::new(Vec::<String>::new()));
        let notifications_clone = notifications.clone();

        reactive_system.on("Increment".to_string(), move |state| {
            let mut notifs = notifications_clone.lock().unwrap();
            notifs.push(format!("Value incremented! Now: {}", state.value));
        });

        let notifications_clone2 = notifications.clone();
        reactive_system.on("Increment".to_string(), move |_| {
            let mut notifs = notifications_clone2.lock().unwrap();
            notifs.push("Another reaction!".to_string());
        });

        // Test initial state
        assert_eq!(notifications.lock().unwrap().len(), 0);

        // Trigger reactions
        reactive_system.trigger("Increment".to_string());

        // Check reactions were triggered
        let notifs = notifications.lock().unwrap();
        assert_eq!(notifs.len(), 2);
        assert!(notifs.iter().any(|n| n.contains("Value incremented! Now:")));
        assert!(notifs.contains(&"Another reaction!".to_string()));
    }

    #[test]
    fn test_state_mesh_example() {
        let initial_state = DocumentState {
            content: "Hello".to_string(),
            cursor_position: 5,
            version: 1,
        };

        let mut node1 = StateNode::new("node1".to_string(), initial_state.clone());
        let node2 = StateNode::new("node2".to_string(), initial_state.clone());
        let node3 = StateNode::new("node3".to_string(), initial_state.clone());

        // Connect nodes
        node1.connect(node2);
        node1.connect(node3);

        // For this test, we'll just verify the basic functionality
        // The actual propagation and state access would need to be implemented
        // based on the actual StateNode API

        // Test that nodes can be created and connected
        assert_eq!(node1.id, "node1");
    }

    #[test]
    fn test_timeline_example() {
        let initial_state = CounterState {
            value: 0,
            is_loading: false,
            error: None,
        };

        let mut timeline = StateManager::new(
            initial_state,
            |state: &CounterState, action: &dyn std::any::Any| {
                if let Some(counter_action) = action.downcast_ref::<CounterAction>() {
                    let mut new_state = state.clone();
                    match counter_action {
                        CounterAction::Increment => new_state.value += 1,
                        CounterAction::Decrement => new_state.value -= 1,
                        CounterAction::SetValue(val) => new_state.value = *val,
                        _ => {}
                    }
                    new_state
                } else {
                    state.clone()
                }
            },
        );

        // Test initial state
        let state = timeline.current_state();
        assert_eq!(state.value, 0);

        // Test dispatch
        timeline.dispatch(CounterAction::Increment);
        timeline.dispatch(CounterAction::Increment);
        timeline.dispatch(CounterAction::Increment);

        let state = timeline.current_state();
        assert_eq!(state.value, 3);

        // Test rewind
        timeline.rewind(2);
        let state = timeline.current_state();
        assert_eq!(state.value, 1);

        // Test dispatch after rewind
        timeline.dispatch(CounterAction::SetValue(10));
        let state = timeline.current_state();
        assert_eq!(state.value, 10);

        // Test branch
        let mut branch = timeline.branch();
        branch.dispatch(CounterAction::Increment);

        let branch_state = branch.current_state();
        let main_state = timeline.current_state();

        assert_eq!(branch_state.value, 11);
        assert_eq!(main_state.value, 10);
    }

    #[test]
    fn test_capsule_example() {
        let initial_state = CounterState {
            value: 0,
            is_loading: false,
            error: None,
        };

        let mut capsule = Capsule::new(initial_state);

        // Test initial state
        let state = capsule.get_state();
        assert_eq!(state.value, 0);

        // Test dispatch (without logic, state won't change)
        capsule.dispatch(CounterAction::Increment);
        let state = capsule.get_state();
        assert_eq!(state.value, 0); // No logic, so no change

        // Test with logic
        let mut capsule_with_logic = Capsule::new(CounterState {
            value: 0,
            is_loading: false,
            error: None,
        })
        .with_logic(
            |state: &mut CounterState, action: CounterAction| match action {
                CounterAction::Increment => state.value += 1,
                CounterAction::Decrement => state.value -= 1,
                _ => {}
            },
        );

        capsule_with_logic.dispatch(CounterAction::Increment);
        capsule_with_logic.dispatch(CounterAction::Increment);

        let state = capsule_with_logic.get_state();
        assert_eq!(state.value, 2);
    }

    #[test]
    fn test_performance_characteristics() {
        let initial_state = CounterState {
            value: 0,
            is_loading: false,
            error: None,
        };

        let store = configure_store(
            initial_state,
            create_reducer(|state: &CounterState, action: &CounterAction| {
                let mut new_state = state.clone();
                if let CounterAction::Increment = action {
                    new_state.value += 1
                }
                new_state
            }),
        );

        // Test rapid dispatch performance
        let start = std::time::Instant::now();
        for _ in 0..1000 {
            store.dispatch(CounterAction::Increment);
        }
        let elapsed = start.elapsed();

        // Should complete in reasonable time (less than 100ms)
        assert!(elapsed.as_millis() < 100);

        let state = store.get_state();
        assert_eq!(state.value, 1000);
    }

    #[test]
    fn test_concurrent_example_behavior() {
        let initial_state = CounterState {
            value: 0,
            is_loading: false,
            error: None,
        };

        let store = Arc::new(configure_store(
            initial_state,
            create_reducer(|state: &CounterState, action: &CounterAction| {
                let mut new_state = state.clone();
                match action {
                    CounterAction::Increment => new_state.value += 1,
                    CounterAction::Decrement => new_state.value -= 1,
                    _ => {}
                }
                new_state
            }),
        ));

        let mut handles = vec![];

        // Spawn multiple threads that increment
        for _ in 0..10 {
            let store_clone = store.clone();
            let handle = thread::spawn(move || {
                for _ in 0..100 {
                    store_clone.dispatch(CounterAction::Increment);
                }
            });
            handles.push(handle);
        }

        // Wait for all threads to complete
        for handle in handles {
            handle.join().unwrap();
        }

        let state = store.get_state();
        assert_eq!(state.value, 1000);
    }
}