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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 web-mech
//! # leptos-store
//!
//! Enterprise-grade, type-enforced state management for Leptos.
//!
//! This crate provides a structured, SSR-safe state management architecture
//! inspired by Vuex and Pinia, translated into idiomatic Rust for Leptos.
//!
//! ## Core Concepts
//!
//! Each store is a **domain module** composed of:
//!
//! 1. **State** - Read-only externally, reactive data container
//! 2. **Getters** - Derived, read-only computed values
//! 3. **Mutators** - Pure, synchronous state mutations
//! 4. **Actions** - Synchronous orchestration with side effects
//! 5. **Async Actions** - Asynchronous orchestration
//!
//! ## Mutation Rules
//!
//! | Layer | Can Write State | Async | Side Effects |
//! |-------|-----------------|-------|--------------|
//! | Components | ❌ | ❌ | ❌ |
//! | Getters | ❌ | ❌ | ❌ |
//! | Mutators | ✅ | ❌ | ❌ |
//! | Actions | ❌ | ❌ | ✅ |
//! | Async Actions | ❌ | ✅ | ✅ |
//!
//! **Only mutators may write state.**
//!
//! ## Feature Flags
//!
//! | Feature | Default | Description |
//! |---------|---------|-------------|
//! | `ssr` | ✅ Yes | Server-side rendering support |
//! | `hydrate` | ❌ No | SSR hydration with automatic state serialization |
//! | `csr` | ❌ No | Client-side rendering only |
//! | `middleware` | ❌ No | Middleware system, audit trail, store coordination |
//! | `devtools` | ❌ No | DevTools integration with time-travel debugging |
//! | `persist-web` | ❌ No | Browser-based state persistence |
//!
//! ### Choosing Features
//!
//! - **CSR only (SPA)**: Use `features = ["csr"]`
//! - **SSR without hydration**: Use default features (`ssr`)
//! - **Full SSR with hydration**: Use `ssr` on server, `hydrate` on client
//!
//! ### Why is `hydrate` opt-in?
//!
//! The `hydrate` feature adds:
//! - `serde` and `serde_json` for state serialization
//! - `web-sys` and `wasm-bindgen` for DOM access
//! - Approximately 50KB to your WASM bundle
//!
//! If you don't need state transfer from server to client, you can skip this overhead.
//!
//! ## Selectors — Fine-Grained Reactivity
//!
//! Selectors create memoized views into specific slices of store state.
//! Only re-compute when their particular slice changes, preventing
//! unnecessary re-renders.
//!
//! ```rust,ignore
//! let user_name = create_selector(&store, |s| s.user.name.clone());
//! let total = combine_selectors(count, discount, |c, d| c * d);
//! let badge = map_selector(count, |n| format!("{n} items"));
//! let active = filter_selector(count, |n| *n > 0);
//! ```
//!
//! See the [`selectors`] module for full documentation.
//!
//! ## Deployment Models
//!
//! | Model | Feature | Description |
//! |-------|---------|-------------|
//! | **SSR** | `ssr` (default) | Store created per-request on server |
//! | **Hydrate** | `hydrate` | Server renders + serializes; client picks up |
//! | **CSR** | `csr` | Browser-only, no server involvement |
//!
//! ## Available Macros
//!
//! | Macro | Purpose | Feature |
//! |-------|---------|---------|
//! | `define_state!` | Define state structs with default values | - |
//! | `define_hydratable_state!` | Define state with serde derives | `hydrate` |
//! | `define_action!` | Define synchronous action structs | - |
//! | `define_async_action!` | Define async action structs with error types | - |
//! | `impl_store!` | Implement Store trait for an existing type | - |
//! | `impl_hydratable_store!` | Implement HydratableStore trait | `hydrate` |
//! | `store!` | Complete store definition in one macro | - |
//! | `selector!` | Batch-create multiple selectors from a store | - |
//! | `namespace!` | Define typed namespace combining multiple stores | - |
//! | `derive_state_diff!` | Generate `StateDiff` impl for field-level diffing | `middleware` |
//!
//! See the [`macros`] module for detailed documentation and examples.
//!
//! ## Hydration Support
//!
//! When building full SSR applications where state needs to transfer from
//! server to client, enable the `hydrate` feature:
//!
//! ```toml
//! [dependencies]
//! leptos-store = { version = "0.2", default-features = false }
//!
//! [features]
//! ssr = ["leptos-store/ssr"]
//! hydrate = ["leptos-store/hydrate"]
//! ```
//!
//! This enables:
//! - `HydratableStore` trait for state serialization
//! - `provide_hydrated_store()` for server-side state embedding
//! - `use_hydrated_store()` for client-side state recovery
//!
//! See the `hydration` module (requires `hydrate` feature) for implementation details.
//!
//! ## Example
//!
//! ```rust
//! use leptos::prelude::*;
//! use leptos_store::prelude::*;
//!
//! // Define your state
//! #[derive(Clone, Debug, Default)]
//! pub struct CounterState {
//! pub count: i32,
//! }
//!
//! // Define your store
//! #[derive(Clone)]
//! pub struct CounterStore {
//! state: RwSignal<CounterState>,
//! }
//!
//! impl Store for CounterStore {
//! type State = CounterState;
//!
//! fn state(&self) -> ReadSignal<Self::State> {
//! self.state.read_only()
//! }
//! }
//!
//! // Define mutators
//! impl CounterStore {
//! pub fn increment(&self) {
//! self.state.update(|s| s.count += 1);
//! }
//!
//! pub fn decrement(&self) {
//! self.state.update(|s| s.count -= 1);
//! }
//! }
//! ```
// Enable doc_cfg for docs.rs to show feature requirements
// New modules for extended functionality
pub use *;