presentar-core 0.3.4

Core types and traits for Presentar UI framework
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
//! State management for Presentar applications.
//!
//! This module implements the Elm Architecture pattern for predictable state
//! management: `State + Message → (State, Command)`.
//!
//! # Examples
//!
//! ```
//! use presentar_core::{State, Command};
//! use serde::{Deserialize, Serialize};
//!
//! // Define your application state
//! #[derive(Clone, Default, Serialize, Deserialize)]
//! struct AppState {
//!     count: i32,
//! }
//!
//! // Define messages that modify state
//! enum AppMessage {
//!     Increment,
//!     Decrement,
//!     Reset,
//! }
//!
//! impl State for AppState {
//!     type Message = AppMessage;
//!
//!     fn update(&mut self, msg: Self::Message) -> Command<Self::Message> {
//!         match msg {
//!             AppMessage::Increment => self.count += 1,
//!             AppMessage::Decrement => self.count -= 1,
//!             AppMessage::Reset => self.count = 0,
//!         }
//!         Command::None
//!     }
//! }
//!
//! let mut state = AppState::default();
//! state.update(AppMessage::Increment);
//! assert_eq!(state.count, 1);
//! ```

use serde::{Deserialize, Serialize};
use std::future::Future;
use std::pin::Pin;

/// Application state trait.
///
/// Implements the Elm Architecture: State + Message → (State, Command)
pub trait State: Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync {
    /// Message type for state updates
    type Message: Send;

    /// Update state in response to a message.
    ///
    /// Returns a command for side effects (async operations, navigation, etc.)
    fn update(&mut self, msg: Self::Message) -> Command<Self::Message>;
}

/// Commands for side effects.
///
/// Commands represent effects that should happen after a state update:
/// - Async tasks (data fetching, file operations)
/// - Navigation
/// - State persistence
#[derive(Default)]
pub enum Command<M> {
    /// No command
    #[default]
    None,
    /// Execute multiple commands
    Batch(Vec<Self>),
    /// Execute an async task
    Task(Pin<Box<dyn Future<Output = M> + Send>>),
    /// Navigate to a route
    Navigate {
        /// Route path
        route: String,
    },
    /// Save state to storage
    SaveState {
        /// Storage key
        key: String,
    },
    /// Load state from storage
    LoadState {
        /// Storage key
        key: String,
        /// Callback with loaded state
        on_load: fn(Option<Vec<u8>>) -> M,
    },
}

impl<M> Command<M> {
    /// Create a task command from an async block.
    pub fn task<F>(future: F) -> Self
    where
        F: Future<Output = M> + Send + 'static,
    {
        Self::Task(Box::pin(future))
    }

    /// Create a batch of commands.
    pub fn batch(commands: impl IntoIterator<Item = Self>) -> Self {
        Self::Batch(commands.into_iter().collect())
    }

    /// Check if this is the none command.
    #[must_use]
    pub const fn is_none(&self) -> bool {
        matches!(self, Self::None)
    }

    /// Map the message type using a function.
    pub fn map<N, F>(self, f: F) -> Command<N>
    where
        F: Fn(M) -> N + Send + Sync + 'static,
        M: Send + 'static,
        N: Send + 'static,
    {
        let f: std::sync::Arc<dyn Fn(M) -> N + Send + Sync> = std::sync::Arc::new(f);
        self.map_inner(&f)
    }

    fn map_inner<N>(self, f: &std::sync::Arc<dyn Fn(M) -> N + Send + Sync>) -> Command<N>
    where
        M: Send + 'static,
        N: Send + 'static,
    {
        match self {
            Self::None => Command::None,
            Self::Batch(cmds) => Command::Batch(cmds.into_iter().map(|c| c.map_inner(f)).collect()),
            Self::Task(fut) => {
                let f = f.clone();
                Command::Task(Box::pin(async move { f(fut.await) }))
            }
            Self::Navigate { route } => Command::Navigate { route },
            Self::SaveState { key } => Command::SaveState { key },
            Self::LoadState { .. } => {
                // Can't easily map LoadState due to function pointer
                // In practice, LoadState is usually at the top level
                Command::None
            }
        }
    }
}

/// A simple counter state for testing.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CounterState {
    /// Current count
    pub count: i32,
}

/// Messages for the counter state.
#[derive(Debug, Clone)]
pub enum CounterMessage {
    /// Increment the counter
    Increment,
    /// Decrement the counter
    Decrement,
    /// Set the counter to a specific value
    Set(i32),
    /// Reset to zero
    Reset,
}

impl State for CounterState {
    type Message = CounterMessage;

    fn update(&mut self, msg: Self::Message) -> Command<Self::Message> {
        match msg {
            CounterMessage::Increment => self.count += 1,
            CounterMessage::Decrement => self.count -= 1,
            CounterMessage::Set(value) => self.count = value,
            CounterMessage::Reset => self.count = 0,
        }
        Command::None
    }
}

/// Type alias for state change subscribers.
type Subscriber<S> = Box<dyn Fn(&S) + Send + Sync>;

/// Store manages state lifecycle with subscriptions and time-travel debugging.
pub struct Store<S: State> {
    state: S,
    history: Vec<S>,
    history_index: usize,
    max_history: usize,
    subscribers: Vec<Subscriber<S>>,
}

impl<S: State> Store<S> {
    /// Create a new store with initial state.
    pub fn new(initial: S) -> Self {
        Self {
            state: initial,
            history: Vec::new(),
            history_index: 0,
            max_history: 100,
            subscribers: Vec::new(),
        }
    }

    /// Create a store with custom history limit.
    pub fn with_history_limit(initial: S, max_history: usize) -> Self {
        Self {
            state: initial,
            history: Vec::new(),
            history_index: 0,
            max_history,
            subscribers: Vec::new(),
        }
    }

    /// Get current state.
    pub const fn state(&self) -> &S {
        &self.state
    }

    /// Dispatch a message to update state.
    pub fn dispatch(&mut self, msg: S::Message) -> Command<S::Message> {
        // Save current state to history
        if self.max_history > 0 {
            // Truncate future history if we're not at the end
            if self.history_index < self.history.len() {
                self.history.truncate(self.history_index);
            }

            self.history.push(self.state.clone());

            // Limit history size
            if self.history.len() > self.max_history {
                self.history.remove(0);
            } else {
                self.history_index = self.history.len();
            }
        }

        // Update state
        let cmd = self.state.update(msg);

        // Notify subscribers
        self.notify_subscribers();

        cmd
    }

    /// Subscribe to state changes.
    pub fn subscribe<F>(&mut self, callback: F)
    where
        F: Fn(&S) + Send + Sync + 'static,
    {
        self.subscribers.push(Box::new(callback));
    }

    /// Get number of history entries.
    pub fn history_len(&self) -> usize {
        self.history.len()
    }

    /// Can undo to previous state.
    pub const fn can_undo(&self) -> bool {
        self.history_index > 0
    }

    /// Can redo to next state.
    pub fn can_redo(&self) -> bool {
        self.history_index < self.history.len()
    }

    /// Undo to previous state.
    pub fn undo(&mut self) -> bool {
        if self.can_undo() {
            // If we're at the end, save current state first
            if self.history_index == self.history.len() {
                self.history.push(self.state.clone());
            }

            self.history_index -= 1;
            self.state = self.history[self.history_index].clone();
            self.notify_subscribers();
            true
        } else {
            false
        }
    }

    /// Redo to next state.
    pub fn redo(&mut self) -> bool {
        if self.history_index < self.history.len().saturating_sub(1) {
            self.history_index += 1;
            self.state = self.history[self.history_index].clone();
            self.notify_subscribers();
            true
        } else {
            false
        }
    }

    /// Jump to a specific point in history.
    pub fn jump_to(&mut self, index: usize) -> bool {
        if index < self.history.len() {
            self.history_index = index;
            self.state = self.history[index].clone();
            self.notify_subscribers();
            true
        } else {
            false
        }
    }

    /// Clear history.
    pub fn clear_history(&mut self) {
        self.history.clear();
        self.history_index = 0;
    }

    fn notify_subscribers(&self) {
        for subscriber in &self.subscribers {
            subscriber(&self.state);
        }
    }
}

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

    #[test]
    fn test_counter_increment() {
        let mut state = CounterState::default();
        state.update(CounterMessage::Increment);
        assert_eq!(state.count, 1);
    }

    #[test]
    fn test_counter_decrement() {
        let mut state = CounterState { count: 5 };
        state.update(CounterMessage::Decrement);
        assert_eq!(state.count, 4);
    }

    #[test]
    fn test_counter_set() {
        let mut state = CounterState::default();
        state.update(CounterMessage::Set(42));
        assert_eq!(state.count, 42);
    }

    #[test]
    fn test_counter_reset() {
        let mut state = CounterState { count: 100 };
        state.update(CounterMessage::Reset);
        assert_eq!(state.count, 0);
    }

    #[test]
    fn test_command_none() {
        let cmd: Command<()> = Command::None;
        assert!(cmd.is_none());
    }

    #[test]
    fn test_command_default() {
        let cmd: Command<()> = Command::default();
        assert!(cmd.is_none());
    }

    #[test]
    fn test_command_batch() {
        let cmd: Command<i32> = Command::batch([
            Command::Navigate {
                route: "/a".to_string(),
            },
            Command::Navigate {
                route: "/b".to_string(),
            },
        ]);
        assert!(!cmd.is_none());
        if let Command::Batch(cmds) = cmd {
            assert_eq!(cmds.len(), 2);
        } else {
            panic!("Expected Batch command");
        }
    }

    #[test]
    fn test_command_navigate() {
        let cmd: Command<()> = Command::Navigate {
            route: "/home".to_string(),
        };
        if let Command::Navigate { route } = cmd {
            assert_eq!(route, "/home");
        } else {
            panic!("Expected Navigate command");
        }
    }

    #[test]
    fn test_command_save_state() {
        let cmd: Command<()> = Command::SaveState {
            key: "app_state".to_string(),
        };
        if let Command::SaveState { key } = cmd {
            assert_eq!(key, "app_state");
        } else {
            panic!("Expected SaveState command");
        }
    }

    #[test]
    fn test_counter_serialization() {
        let state = CounterState { count: 42 };
        let json = serde_json::to_string(&state).unwrap();
        let loaded: CounterState = serde_json::from_str(&json).unwrap();
        assert_eq!(loaded.count, 42);
    }

    #[test]
    fn test_command_map() {
        let cmd: Command<i32> = Command::Navigate {
            route: "/test".to_string(),
        };
        let mapped: Command<String> = cmd.map(|_i| "mapped".to_string());

        if let Command::Navigate { route } = mapped {
            assert_eq!(route, "/test");
        } else {
            panic!("Expected Navigate command after map");
        }
    }

    #[test]
    fn test_command_map_none() {
        let cmd: Command<i32> = Command::None;
        let mapped: Command<String> = cmd.map(|i| i.to_string());
        assert!(mapped.is_none());
    }

    #[test]
    fn test_command_batch_map() {
        let cmd: Command<i32> = Command::batch([
            Command::SaveState {
                key: "key1".to_string(),
            },
            Command::SaveState {
                key: "key2".to_string(),
            },
        ]);

        let mapped: Command<String> = cmd.map(|i| format!("val_{i}"));

        if let Command::Batch(cmds) = mapped {
            assert_eq!(cmds.len(), 2);
        } else {
            panic!("Expected Batch command after map");
        }
    }

    // =========================================================================
    // Store Tests
    // =========================================================================

    #[test]
    fn test_store_new() {
        let store = Store::new(CounterState::default());
        assert_eq!(store.state().count, 0);
    }

    #[test]
    fn test_store_dispatch() {
        let mut store = Store::new(CounterState::default());
        store.dispatch(CounterMessage::Increment);
        assert_eq!(store.state().count, 1);
    }

    #[test]
    fn test_store_history() {
        let mut store = Store::new(CounterState::default());

        store.dispatch(CounterMessage::Increment);
        store.dispatch(CounterMessage::Increment);
        store.dispatch(CounterMessage::Increment);

        assert_eq!(store.state().count, 3);
        assert_eq!(store.history_len(), 3);
    }

    #[test]
    fn test_store_undo() {
        let mut store = Store::new(CounterState::default());

        store.dispatch(CounterMessage::Increment);
        store.dispatch(CounterMessage::Increment);
        assert_eq!(store.state().count, 2);

        assert!(store.can_undo());
        assert!(store.undo());
        assert_eq!(store.state().count, 1);

        assert!(store.undo());
        assert_eq!(store.state().count, 0);
    }

    #[test]
    fn test_store_redo() {
        let mut store = Store::new(CounterState::default());

        store.dispatch(CounterMessage::Increment);
        store.dispatch(CounterMessage::Increment);
        store.undo();
        store.undo();

        assert_eq!(store.state().count, 0);
        assert!(store.can_redo());

        assert!(store.redo());
        assert_eq!(store.state().count, 1);

        assert!(store.redo());
        assert_eq!(store.state().count, 2);
    }

    #[test]
    fn test_store_undo_at_start() {
        let mut store = Store::new(CounterState::default());
        assert!(!store.can_undo());
        assert!(!store.undo());
    }

    #[test]
    fn test_store_redo_at_end() {
        let mut store = Store::new(CounterState::default());
        store.dispatch(CounterMessage::Increment);
        assert!(!store.can_redo());
        assert!(!store.redo());
    }

    #[test]
    fn test_store_history_truncation() {
        let mut store = Store::new(CounterState::default());

        store.dispatch(CounterMessage::Set(1));
        store.dispatch(CounterMessage::Set(2));
        store.dispatch(CounterMessage::Set(3));

        // Undo to 1
        store.undo();
        store.undo();
        assert_eq!(store.state().count, 1);

        // New dispatch should truncate redo history
        store.dispatch(CounterMessage::Set(10));
        assert_eq!(store.state().count, 10);

        // Cannot redo to 2 or 3 anymore
        assert!(!store.redo());
    }

    #[test]
    fn test_store_jump_to() {
        let mut store = Store::new(CounterState::default());

        store.dispatch(CounterMessage::Set(10));
        store.dispatch(CounterMessage::Set(20));
        store.dispatch(CounterMessage::Set(30));

        assert!(store.jump_to(0));
        assert_eq!(store.state().count, 0);

        assert!(store.jump_to(2));
        assert_eq!(store.state().count, 20);
    }

    #[test]
    fn test_store_jump_invalid() {
        let mut store = Store::new(CounterState::default());
        store.dispatch(CounterMessage::Increment);

        assert!(!store.jump_to(100));
    }

    #[test]
    fn test_store_clear_history() {
        let mut store = Store::new(CounterState::default());

        store.dispatch(CounterMessage::Increment);
        store.dispatch(CounterMessage::Increment);
        assert!(store.history_len() > 0);

        store.clear_history();
        assert_eq!(store.history_len(), 0);
        assert!(!store.can_undo());
    }

    #[test]
    fn test_store_with_history_limit() {
        let mut store = Store::with_history_limit(CounterState::default(), 3);

        for i in 1..=10 {
            store.dispatch(CounterMessage::Set(i));
        }

        // History should be capped at 3
        assert!(store.history_len() <= 3);
    }

    #[test]
    fn test_store_subscribe() {
        use std::sync::atomic::{AtomicI32, Ordering};
        use std::sync::Arc;

        let call_count = Arc::new(AtomicI32::new(0));
        let call_count_clone = call_count.clone();

        let mut store = Store::new(CounterState::default());
        store.subscribe(move |_| {
            call_count_clone.fetch_add(1, Ordering::SeqCst);
        });

        store.dispatch(CounterMessage::Increment);
        store.dispatch(CounterMessage::Increment);

        assert_eq!(call_count.load(Ordering::SeqCst), 2);
    }

    #[test]
    fn test_store_no_history() {
        let mut store = Store::with_history_limit(CounterState::default(), 0);

        store.dispatch(CounterMessage::Increment);
        store.dispatch(CounterMessage::Increment);

        assert_eq!(store.history_len(), 0);
        assert!(!store.can_undo());
    }
}