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
//! # Reducer Module
//!
//! This module provides traits and utilities for creating reducers - pure functions that
//! take a current state and an action, and return a new state. This is a core concept
//! in Redux-style state management.
//!
//! ## Example
//!
//! ```rust
//! use zed::{Reducer, create_reducer};
//!
//! #[derive(Clone, Debug, PartialEq)]
//! struct CounterState {
//! value: i32,
//! }
//!
//! #[derive(Debug)]
//! enum CounterAction {
//! Increment,
//! Decrement,
//! Set(i32),
//! }
//!
//! // Create a reducer using a closure
//! let counter_reducer = create_reducer(|state: &CounterState, action: &CounterAction| {
//! match action {
//! CounterAction::Increment => CounterState { value: state.value + 1 },
//! CounterAction::Decrement => CounterState { value: state.value - 1 },
//! CounterAction::Set(val) => CounterState { value: *val },
//! }
//! });
//!
//! let state = CounterState { value: 0 };
//! let new_state = counter_reducer.reduce(&state, &CounterAction::Increment);
//! assert_eq!(new_state.value, 1);
//! ```
use PhantomData;
/// A trait for implementing reducers that transform state based on actions.
///
/// Reducers are pure functions that take the current state and an action,
/// and return a new state. They should not have side effects and should
/// be deterministic.
/// A reducer implementation that wraps a closure function.
///
/// This allows you to easily create reducers from functions or closures
/// without having to implement the Reducer trait manually.
///
/// # Example
///
/// ```rust
/// use zed::{create_reducer, Reducer};
///
/// #[derive(Clone)]
/// struct State { count: i32 }
///
/// enum Action { Increment }
///
/// let reducer = create_reducer(|state: &State, _action: &Action| State { count: state.count + 1 });
///
/// let state = State { count: 0 };
/// let new_state = reducer.reduce(&state, &Action::Increment);
/// assert_eq!(new_state.count, 1);
/// ```
,
/// Creates a new reducer from a closure function.
///
/// This is a convenience function that wraps a closure in a ClosureReducer
/// and handles the phantom data automatically.
///
/// # Arguments
///
/// * `f` - A function that takes a state reference and action reference, returns new state
///
/// # Returns
///
/// A ClosureReducer that implements the Reducer trait
///
/// # Example
///
/// ```rust
/// use zed::{create_reducer, Reducer};
///
/// #[derive(Clone)]
/// struct AppState { counter: i32 }
///
/// enum AppAction { Increment, Decrement }
///
/// let reducer = create_reducer(|state: &AppState, action: &AppAction| {
/// match action {
/// AppAction::Increment => AppState { counter: state.counter + 1 },
/// AppAction::Decrement => AppState { counter: state.counter - 1 },
/// }
/// });
///
/// let initial_state = AppState { counter: 0 };
/// let new_state = reducer.reduce(&initial_state, &AppAction::Increment);
/// assert_eq!(new_state.counter, 1);
/// ```