leptos-store 0.11.0

Enterprise-grade, type-enforced state management for Leptos
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
640
641
642
643
644
645
646
647
648
649
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 nyvorin

//! Hydration support for SSR stores.
//!
//! This module provides utilities for serializing store state on the server
//! and deserializing it on the client during hydration, ensuring state
//! consistency and avoiding hydration mismatches.
//!
//! # Overview
//!
//! Hydration is the process where client-side code takes over server-rendered
//! HTML and makes it interactive. For state management, this means:
//!
//! 1. **Server**: Serialize store state to JSON and embed in HTML
//! 2. **Client**: Read serialized state and initialize stores with it
//!
//! This ensures that the client's reactive signals start with the same values
//! as the server, preventing hydration mismatches.
//!
//! # Example
//!
//! ```rust,ignore
//! use leptos::prelude::*;
//! use leptos_store::prelude::*;
//! use serde::{Serialize, Deserialize};
//!
//! #[derive(Clone, Debug, Default, Serialize, Deserialize)]
//! pub struct CounterState {
//!     pub count: i32,
//! }
//!
//! #[derive(Clone)]
//! pub struct CounterStore {
//!     state: RwSignal<CounterState>,
//! }
//!
//! impl HydratableStore for CounterStore {
//!     fn serialize_state(&self) -> Result<String, StoreHydrationError> {
//!         let state = self.state.get();
//!         serde_json::to_string(&state)
//!             .map_err(|e| StoreHydrationError::Serialization(e.to_string()))
//!     }
//!
//!     fn from_hydrated_state(data: &str) -> Result<Self, StoreHydrationError> {
//!         let state: CounterState = serde_json::from_str(data)
//!             .map_err(|e| StoreHydrationError::Deserialization(e.to_string()))?;
//!         Ok(Self {
//!             state: RwSignal::new(state),
//!         })
//!     }
//!
//!     fn store_key() -> &'static str {
//!         "counter"
//!     }
//! }
//! ```

#[cfg(feature = "hydrate")]
use crate::store::Store;
use thiserror::Error;

/// Errors that can occur during store hydration.
#[derive(Debug, Error, Clone, PartialEq)]
pub enum StoreHydrationError {
    /// Failed to serialize state.
    #[error("Serialization error: {0}")]
    Serialization(String),

    /// Failed to deserialize state.
    #[error("Deserialization error: {0}")]
    Deserialization(String),

    /// Hydration data not found in DOM.
    #[error("Hydration data not found for key: {0}")]
    NotFound(String),

    /// Invalid hydration data format.
    #[error("Invalid hydration data: {0}")]
    InvalidData(String),

    /// DOM access error (WASM-specific).
    #[error("DOM error: {0}")]
    DomError(String),
}

/// Trait for stores that support SSR hydration.
///
/// Implement this trait to enable automatic state transfer between
/// server and client during SSR hydration.
///
/// # Type Bounds
///
/// When the `hydrate` feature is enabled, the state type must implement
/// `serde::Serialize` and `serde::de::DeserializeOwned`.
///
/// # Example
///
/// ```rust,ignore
/// use leptos_store::prelude::*;
/// use serde::{Serialize, Deserialize};
///
/// #[derive(Clone, Default, Serialize, Deserialize)]
/// struct MyState {
///     value: i32,
/// }
///
/// #[derive(Clone)]
/// struct MyStore {
///     state: RwSignal<MyState>,
/// }
///
/// impl HydratableStore for MyStore {
///     fn serialize_state(&self) -> Result<String, StoreHydrationError> {
///         serde_json::to_string(&self.state.get())
///             .map_err(|e| StoreHydrationError::Serialization(e.to_string()))
///     }
///
///     fn from_hydrated_state(data: &str) -> Result<Self, StoreHydrationError> {
///         let state: MyState = serde_json::from_str(data)
///             .map_err(|e| StoreHydrationError::Deserialization(e.to_string()))?;
///         Ok(Self { state: RwSignal::new(state) })
///     }
///
///     fn store_key() -> &'static str {
///         "my_store"
///     }
/// }
/// ```
#[cfg(feature = "hydrate")]
pub trait HydratableStore: Store + Sized {
    /// Serialize the store's state to a JSON string.
    ///
    /// This is called on the server during SSR to capture the current
    /// state for transfer to the client.
    fn serialize_state(&self) -> Result<String, StoreHydrationError>;

    /// Create a new store from serialized state data.
    ///
    /// This is called on the client during hydration to restore the
    /// store's state from the server-rendered data.
    fn from_hydrated_state(data: &str) -> Result<Self, StoreHydrationError>;

    /// Returns a unique key for this store type.
    ///
    /// This key is used to identify the store's data in the hydration
    /// script tag. Must be unique across all stores in the application.
    fn store_key() -> &'static str;
}

/// The ID prefix used for hydration script tags.
pub const HYDRATION_SCRIPT_PREFIX: &str = "__LEPTOS_STORE_STATE__";

/// Generate the full script element ID for a store.
#[cfg(feature = "hydrate")]
pub fn hydration_script_id(store_key: &str) -> String {
    format!("{HYDRATION_SCRIPT_PREFIX}{store_key}")
}

/// Serialize a store's state to JSON for embedding in HTML.
///
/// # Arguments
///
/// * `store` - The store to serialize
///
/// # Returns
///
/// A JSON string representation of the store's state.
#[cfg(feature = "hydrate")]
pub fn serialize_store_state<S: HydratableStore>(store: &S) -> Result<String, StoreHydrationError> {
    store.serialize_state()
}

/// Read hydration data from the DOM.
///
/// This function looks for a script tag with the store's hydration ID
/// and extracts the serialized state data.
///
/// # Arguments
///
/// * `store_key` - The unique key for the store
///
/// # Returns
///
/// The serialized state data as a string.
#[cfg(all(feature = "hydrate", target_arch = "wasm32"))]
pub fn read_hydration_data(store_key: &str) -> Result<String, StoreHydrationError> {
    use wasm_bindgen::JsCast;

    let window = web_sys::window()
        .ok_or_else(|| StoreHydrationError::DomError("No window object".to_string()))?;

    let document = window
        .document()
        .ok_or_else(|| StoreHydrationError::DomError("No document object".to_string()))?;

    let script_id = hydration_script_id(store_key);
    let element = document
        .get_element_by_id(&script_id)
        .ok_or_else(|| StoreHydrationError::NotFound(store_key.to_string()))?;

    let script = element
        .dyn_into::<web_sys::HtmlScriptElement>()
        .map_err(|_| StoreHydrationError::InvalidData("Element is not a script tag".to_string()))?;

    let content = script.text().map_err(|e| {
        StoreHydrationError::DomError(format!("Failed to read script content: {:?}", e))
    })?;

    Ok(content)
}

/// Stub for non-WASM targets.
#[cfg(all(feature = "hydrate", not(target_arch = "wasm32")))]
pub fn read_hydration_data(store_key: &str) -> Result<String, StoreHydrationError> {
    Err(StoreHydrationError::DomError(format!(
        "DOM access not available on this platform for key: {store_key}"
    )))
}

/// Hydrate a store from DOM data.
///
/// This function reads the serialized state from the DOM and creates
/// a new store instance with the hydrated state.
///
/// # Type Parameters
///
/// * `S` - The store type to hydrate
///
/// # Returns
///
/// A new store instance with the hydrated state, or an error if
/// hydration fails.
#[cfg(feature = "hydrate")]
pub fn hydrate_store<S: HydratableStore>() -> Result<S, StoreHydrationError> {
    let data = read_hydration_data(S::store_key())?;
    S::from_hydrated_state(&data)
}

/// Check if hydration data is available for a store.
///
/// This is useful for conditional hydration logic where you want
/// to fall back to default state if no hydration data exists.
#[cfg(all(feature = "hydrate", target_arch = "wasm32"))]
pub fn has_hydration_data(store_key: &str) -> bool {
    if let Some(window) = web_sys::window() {
        if let Some(document) = window.document() {
            let script_id = hydration_script_id(store_key);
            return document.get_element_by_id(&script_id).is_some();
        }
    }
    false
}

/// Stub for non-WASM targets.
#[cfg(all(feature = "hydrate", not(target_arch = "wasm32")))]
pub fn has_hydration_data(_store_key: &str) -> bool {
    false
}

/// Generate the HTML for a hydration script tag.
///
/// This is used during SSR to embed the serialized store state
/// in the HTML document.
///
/// # Arguments
///
/// * `store_key` - The unique key for the store
/// * `data` - The serialized state data
///
/// # Returns
///
/// An HTML string containing the script tag with the embedded data.
#[cfg(feature = "hydrate")]
pub fn hydration_script_html(store_key: &str, data: &str) -> String {
    let script_id = hydration_script_id(store_key);
    // Escape any script closing tags in the data
    let escaped_data = data.replace("</script>", "<\\/script>");
    format!(r#"<script id="{script_id}" type="application/json">{escaped_data}</script>"#)
}

/// A builder for creating hydration-aware stores.
///
/// This builder provides a fluent API for creating stores that
/// automatically handle hydration on the client.
#[cfg(feature = "hydrate")]
pub struct HydrationBuilder<S: HydratableStore> {
    fallback: Option<S>,
}

#[cfg(feature = "hydrate")]
impl<S: HydratableStore> Default for HydrationBuilder<S> {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "hydrate")]
impl<S: HydratableStore> HydrationBuilder<S> {
    /// Create a new hydration builder.
    pub fn new() -> Self {
        Self { fallback: None }
    }

    /// Set a fallback store to use if hydration fails.
    ///
    /// If hydration data is not found or deserialization fails,
    /// this fallback store will be used instead.
    pub fn with_fallback(mut self, store: S) -> Self {
        self.fallback = Some(store);
        self
    }

    /// Build the store, attempting hydration first.
    ///
    /// This will try to hydrate from DOM data. If hydration fails
    /// and a fallback was provided, the fallback will be used.
    ///
    /// # Panics
    ///
    /// Panics if hydration fails and no fallback was provided.
    pub fn build(self) -> S {
        match hydrate_store::<S>() {
            Ok(store) => store,
            Err(e) => {
                if let Some(fallback) = self.fallback {
                    fallback
                } else {
                    panic!("Store hydration failed and no fallback provided: {e}")
                }
            }
        }
    }

    /// Build the store, returning a Result.
    ///
    /// This will try to hydrate from DOM data. If hydration fails
    /// and a fallback was provided, the fallback will be returned.
    pub fn try_build(self) -> Result<S, StoreHydrationError> {
        match hydrate_store::<S>() {
            Ok(store) => Ok(store),
            Err(e) => {
                if let Some(fallback) = self.fallback {
                    Ok(fallback)
                } else {
                    Err(e)
                }
            }
        }
    }
}

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

    #[test]
    fn test_hydration_error_display() {
        let err = StoreHydrationError::Serialization("test error".to_string());
        assert_eq!(err.to_string(), "Serialization error: test error");

        let err = StoreHydrationError::Deserialization("parse error".to_string());
        assert_eq!(err.to_string(), "Deserialization error: parse error");

        let err = StoreHydrationError::NotFound("my_store".to_string());
        assert_eq!(
            err.to_string(),
            "Hydration data not found for key: my_store"
        );

        let err = StoreHydrationError::InvalidData("bad format".to_string());
        assert_eq!(err.to_string(), "Invalid hydration data: bad format");

        let err = StoreHydrationError::DomError("no window".to_string());
        assert_eq!(err.to_string(), "DOM error: no window");
    }

    #[test]
    fn test_hydration_script_id() {
        #[cfg(feature = "hydrate")]
        {
            let id = hydration_script_id("my_store");
            assert_eq!(id, "__LEPTOS_STORE_STATE__my_store");
        }
    }

    #[test]
    fn test_hydration_script_html() {
        #[cfg(feature = "hydrate")]
        {
            let html = hydration_script_html("counter", r#"{"count":42}"#);
            assert!(html.contains(r#"id="__LEPTOS_STORE_STATE__counter""#));
            assert!(html.contains(r#"type="application/json""#));
            assert!(html.contains(r#"{"count":42}"#));
        }
    }

    #[test]
    fn test_hydration_script_html_escapes_script_tags() {
        #[cfg(feature = "hydrate")]
        {
            let html = hydration_script_html("test", r#"{"value":"</script>"}"#);
            assert!(html.contains(r#"<\/script>"#));
            assert!(!html.contains(r#"</script>"}"#));
        }
    }

    // ========================================================================
    // Integration tests for the full hydration workflow
    // ========================================================================

    #[cfg(feature = "hydrate")]
    mod hydration_integration {
        use super::*;
        use crate::store::Store;
        use leptos::prelude::*;
        use serde::{Deserialize, Serialize};

        /// Test state with various field types
        #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
        struct TestState {
            count: i32,
            name: String,
            items: Vec<String>,
            optional: Option<bool>,
        }

        /// Test store that implements HydratableStore
        #[derive(Clone)]
        struct TestHydratableStore {
            state: RwSignal<TestState>,
        }

        impl TestHydratableStore {
            fn new() -> Self {
                Self {
                    state: RwSignal::new(TestState::default()),
                }
            }

            fn with_state(state: TestState) -> Self {
                Self {
                    state: RwSignal::new(state),
                }
            }
        }

        impl Store for TestHydratableStore {
            type State = TestState;

            fn state(&self) -> leptos::prelude::ReadSignal<Self::State> {
                self.state.read_only()
            }
        }

        impl HydratableStore for TestHydratableStore {
            fn serialize_state(&self) -> Result<String, StoreHydrationError> {
                let state = self.state.get();
                serde_json::to_string(&state)
                    .map_err(|e| StoreHydrationError::Serialization(e.to_string()))
            }

            fn from_hydrated_state(data: &str) -> Result<Self, StoreHydrationError> {
                let state: TestState = serde_json::from_str(data)
                    .map_err(|e| StoreHydrationError::Deserialization(e.to_string()))?;
                Ok(Self::with_state(state))
            }

            fn store_key() -> &'static str {
                "test_store"
            }
        }

        #[test]
        fn test_store_serialization_roundtrip() {
            // Create a store with specific state
            let original_state = TestState {
                count: 42,
                name: "Test Store".to_string(),
                items: vec!["item1".to_string(), "item2".to_string()],
                optional: Some(true),
            };

            let store = TestHydratableStore::with_state(original_state.clone());

            // Serialize
            let serialized = store
                .serialize_state()
                .expect("Serialization should succeed");

            // Verify JSON structure
            assert!(serialized.contains("42"));
            assert!(serialized.contains("Test Store"));
            assert!(serialized.contains("item1"));

            // Deserialize into new store
            let restored_store = TestHydratableStore::from_hydrated_state(&serialized)
                .expect("Deserialization should succeed");

            // Verify state matches
            let restored_state = restored_store.state.get();
            assert_eq!(restored_state, original_state);
        }

        #[test]
        fn test_store_default_state_roundtrip() {
            let store = TestHydratableStore::new();

            let serialized = store
                .serialize_state()
                .expect("Serialization should succeed");
            let restored = TestHydratableStore::from_hydrated_state(&serialized)
                .expect("Deserialization should succeed");

            assert_eq!(restored.state.get(), TestState::default());
        }

        #[test]
        fn test_store_key_is_correct() {
            assert_eq!(TestHydratableStore::store_key(), "test_store");
        }

        #[test]
        fn test_full_hydration_html_generation() {
            let state = TestState {
                count: 100,
                name: "Hydration Test".to_string(),
                items: vec!["a".to_string(), "b".to_string()],
                optional: None,
            };

            let store = TestHydratableStore::with_state(state);
            let serialized = store.serialize_state().unwrap();

            // Generate the full hydration HTML
            let html = hydration_script_html(TestHydratableStore::store_key(), &serialized);

            // Verify the HTML structure
            assert!(html.starts_with("<script"));
            assert!(html.ends_with("</script>"));
            assert!(html.contains("__LEPTOS_STORE_STATE__test_store"));
            assert!(html.contains("application/json"));
            assert!(html.contains("100")); // count value
            assert!(html.contains("Hydration Test")); // name value
        }

        #[test]
        fn test_serialize_store_state_helper() {
            let store = TestHydratableStore::with_state(TestState {
                count: 999,
                ..Default::default()
            });

            let serialized = serialize_store_state(&store).unwrap();
            assert!(serialized.contains("999"));
        }

        #[test]
        fn test_hydration_builder_with_fallback() {
            // Since we can't read from DOM in tests, the builder should use fallback
            let fallback = TestHydratableStore::with_state(TestState {
                count: 123,
                name: "Fallback".to_string(),
                ..Default::default()
            });

            let store = HydrationBuilder::<TestHydratableStore>::new()
                .with_fallback(fallback)
                .try_build()
                .expect("Should succeed with fallback");

            // Should get the fallback state (since DOM isn't available in tests)
            assert_eq!(store.state.get().count, 123);
            assert_eq!(store.state.get().name, "Fallback");
        }

        #[test]
        fn test_deserialization_error_handling() {
            // Invalid JSON
            let result = TestHydratableStore::from_hydrated_state("not valid json");
            assert!(result.is_err());

            if let Err(StoreHydrationError::Deserialization(msg)) = result {
                assert!(!msg.is_empty());
            } else {
                panic!("Expected Deserialization error");
            }

            // Valid JSON but wrong structure
            let result = TestHydratableStore::from_hydrated_state(r#"{"wrong":"field"}"#);
            assert!(result.is_err());
        }

        #[test]
        fn test_special_characters_in_state() {
            let state = TestState {
                count: 0,
                name: r#"Test with "quotes" and <tags> and </script>"#.to_string(),
                items: vec!["<script>alert('xss')</script>".to_string()],
                optional: None,
            };

            let store = TestHydratableStore::with_state(state.clone());
            let serialized = store.serialize_state().unwrap();

            // The serialization should handle special characters
            let restored = TestHydratableStore::from_hydrated_state(&serialized).unwrap();
            assert_eq!(restored.state.get(), state);

            // The HTML output should escape script tags
            let html = hydration_script_html(TestHydratableStore::store_key(), &serialized);
            // Script tags in the content should be escaped
            assert!(!html.contains("</script>\""));
        }

        #[test]
        fn test_empty_state_roundtrip() {
            let state = TestState {
                count: 0,
                name: String::new(),
                items: vec![],
                optional: None,
            };

            let store = TestHydratableStore::with_state(state.clone());
            let serialized = store.serialize_state().unwrap();
            let restored = TestHydratableStore::from_hydrated_state(&serialized).unwrap();

            assert_eq!(restored.state.get(), state);
        }

        #[test]
        fn test_large_state_roundtrip() {
            // Test with a larger state to ensure no size issues
            let state = TestState {
                count: i32::MAX,
                name: "x".repeat(10000),
                items: (0..1000).map(|i| format!("item_{i}")).collect(),
                optional: Some(true),
            };

            let store = TestHydratableStore::with_state(state.clone());
            let serialized = store.serialize_state().unwrap();
            let restored = TestHydratableStore::from_hydrated_state(&serialized).unwrap();

            assert_eq!(restored.state.get(), state);
        }
    }
}