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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 nyvorin

//! Feature flag store template.
//!
//! This module provides a complete feature flag management system including:
//!
//! - Feature flag state management
//! - Local overrides for development
//! - Remote sync capabilities
//! - Leptos component for conditional rendering
//!
//! # Example
//!
//! ```rust,ignore
//! use leptos::prelude::*;
//! use leptos_store::templates::feature_flags::*;
//!
//! #[component]
//! fn App() -> impl IntoView {
//!     let flags = FeatureFlagStore::new();
//!     flags.set_flags(vec![
//!         FeatureFlag::new("dark_mode", true),
//!         FeatureFlag::new("beta_features", false),
//!     ]);
//!     provide_context(flags);
//!
//!     view! {
//!         <Feature flag="dark_mode">
//!             <DarkModeComponent />
//!         </Feature>
//!     }
//! }
//! ```

use crate::store::Store;
use leptos::prelude::*;
use std::collections::HashMap;
use thiserror::Error;

#[cfg(feature = "hydrate")]
use serde::{Deserialize, Serialize};

// ============================================================================
// Types
// ============================================================================

/// A feature flag definition.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "hydrate", derive(Serialize, Deserialize))]
pub struct FeatureFlag {
    /// Unique key for the flag.
    pub key: String,
    /// Whether the flag is enabled.
    pub enabled: bool,
    /// Optional variant for A/B testing.
    pub variant: Option<String>,
    /// Additional metadata.
    #[cfg_attr(feature = "hydrate", serde(default))]
    pub metadata: HashMap<String, String>,
    /// Description of the flag (for documentation).
    pub description: Option<String>,
}

impl FeatureFlag {
    /// Create a new feature flag.
    pub fn new(key: impl Into<String>, enabled: bool) -> Self {
        Self {
            key: key.into(),
            enabled,
            variant: None,
            metadata: HashMap::new(),
            description: None,
        }
    }

    /// Create a feature flag with a variant.
    pub fn with_variant(key: impl Into<String>, enabled: bool, variant: impl Into<String>) -> Self {
        Self {
            key: key.into(),
            enabled,
            variant: Some(variant.into()),
            metadata: HashMap::new(),
            description: None,
        }
    }

    /// Add a description.
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Add metadata.
    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }
}

/// User context for flag evaluation.
///
/// This context is sent to the flag provider to enable
/// user-specific flag values.
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "hydrate", derive(Serialize, Deserialize))]
pub struct UserContext {
    /// User ID (if authenticated).
    pub user_id: Option<String>,
    /// User email (if available).
    pub email: Option<String>,
    /// User attributes for targeting.
    pub attributes: HashMap<String, String>,
    /// Environment (e.g., "development", "staging", "production").
    pub environment: Option<String>,
}

impl UserContext {
    /// Create a new empty user context.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a context for an authenticated user.
    pub fn authenticated(user_id: impl Into<String>) -> Self {
        Self {
            user_id: Some(user_id.into()),
            ..Default::default()
        }
    }

    /// Set the user ID.
    pub fn with_user_id(mut self, id: impl Into<String>) -> Self {
        self.user_id = Some(id.into());
        self
    }

    /// Set the email.
    pub fn with_email(mut self, email: impl Into<String>) -> Self {
        self.email = Some(email.into());
        self
    }

    /// Set the environment.
    pub fn with_environment(mut self, env: impl Into<String>) -> Self {
        self.environment = Some(env.into());
        self
    }

    /// Add an attribute.
    pub fn with_attribute(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.attributes.insert(key.into(), value.into());
        self
    }
}

// ============================================================================
// Errors
// ============================================================================

/// Errors that can occur during flag operations.
#[derive(Debug, Error, Clone)]
pub enum FlagError {
    /// Flag not found.
    #[error("Flag not found: {0}")]
    NotFound(String),

    /// Failed to fetch flags from remote.
    #[error("Failed to fetch flags: {0}")]
    FetchFailed(String),

    /// Invalid flag configuration.
    #[error("Invalid flag configuration: {0}")]
    InvalidConfig(String),

    /// Network error.
    #[error("Network error: {0}")]
    Network(String),
}

// ============================================================================
// State
// ============================================================================

/// Feature flag store state.
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "hydrate", derive(Serialize, Deserialize))]
pub struct FeatureFlagState {
    /// All flags indexed by key.
    flags: HashMap<String, FeatureFlag>,
    /// Local overrides (for development).
    #[cfg_attr(feature = "hydrate", serde(skip))]
    overrides: HashMap<String, bool>,
    /// Whether flags have been loaded.
    loaded: bool,
    /// Last error (transient, not serialized).
    #[cfg_attr(feature = "hydrate", serde(skip))]
    error: Option<FlagError>,
    /// Loading state (transient).
    #[cfg_attr(feature = "hydrate", serde(skip))]
    loading: bool,
}

impl FeatureFlagState {
    /// Check if a flag is enabled.
    ///
    /// This checks overrides first, then the actual flag value.
    pub fn is_enabled(&self, key: &str) -> bool {
        // Check overrides first
        if let Some(&override_value) = self.overrides.get(key) {
            return override_value;
        }

        // Check actual flag
        self.flags.get(key).map(|f| f.enabled).unwrap_or(false)
    }

    /// Get a flag's variant.
    pub fn get_variant(&self, key: &str) -> Option<String> {
        self.flags.get(key).and_then(|f| f.variant.clone())
    }

    /// Get a flag by key.
    pub fn get_flag(&self, key: &str) -> Option<&FeatureFlag> {
        self.flags.get(key)
    }
}

// ============================================================================
// Store
// ============================================================================

/// Feature flag store for managing feature flags.
///
/// # Example
///
/// ```rust
/// use leptos_store::templates::feature_flags::{FeatureFlagStore, FeatureFlag};
///
/// let store = FeatureFlagStore::new();
///
/// // Set flags (e.g., from server response)
/// store.set_flags(vec![
///     FeatureFlag::new("new_checkout", true),
///     FeatureFlag::with_variant("homepage_hero", true, "variant_a"),
/// ]);
///
/// // Check flags
/// if store.is_enabled("new_checkout") {
///     // Show new checkout flow
/// }
///
/// // Set local override for development
/// store.set_override("experimental_feature", true);
/// ```
#[derive(Clone)]
pub struct FeatureFlagStore {
    state: RwSignal<FeatureFlagState>,
}

impl Default for FeatureFlagStore {
    fn default() -> Self {
        Self::new()
    }
}

impl FeatureFlagStore {
    /// Create a new feature flag store.
    pub fn new() -> Self {
        Self {
            state: RwSignal::new(FeatureFlagState::default()),
        }
    }

    /// Create a store with initial flags.
    pub fn with_flags(flags: Vec<FeatureFlag>) -> Self {
        let store = Self::new();
        store.set_flags(flags);
        store
    }

    // ========================================================================
    // Getters
    // ========================================================================

    /// Check if a flag is enabled.
    ///
    /// This checks local overrides first, then the actual flag value.
    /// Returns `false` for unknown flags.
    pub fn is_enabled(&self, key: &str) -> bool {
        self.state.with(|s| s.is_enabled(key))
    }

    /// Get a flag's variant.
    pub fn get_variant(&self, key: &str) -> Option<String> {
        self.state.with(|s| s.get_variant(key))
    }

    /// Get all flags.
    pub fn all_flags(&self) -> Vec<FeatureFlag> {
        self.state.with(|s| s.flags.values().cloned().collect())
    }

    /// Get a specific flag.
    pub fn get_flag(&self, key: &str) -> Option<FeatureFlag> {
        self.state.with(|s| s.flags.get(key).cloned())
    }

    /// Get all flag keys.
    pub fn flag_keys(&self) -> Vec<String> {
        self.state.with(|s| s.flags.keys().cloned().collect())
    }

    /// Check if flags have been loaded.
    pub fn is_loaded(&self) -> bool {
        self.state.with(|s| s.loaded)
    }

    /// Check if currently loading.
    pub fn is_loading(&self) -> bool {
        self.state.with(|s| s.loading)
    }

    /// Get the current error.
    pub fn error(&self) -> Option<FlagError> {
        self.state.with(|s| s.error.clone())
    }

    /// Get all current overrides.
    pub fn overrides(&self) -> HashMap<String, bool> {
        self.state.with(|s| s.overrides.clone())
    }

    // ========================================================================
    // Mutators
    // ========================================================================

    /// Set flags from a list.
    ///
    /// This replaces all existing flags.
    pub fn set_flags(&self, flags: Vec<FeatureFlag>) {
        self.state.update(|s| {
            s.flags = flags.into_iter().map(|f| (f.key.clone(), f)).collect();
            s.loaded = true;
            s.error = None;
        });
    }

    /// Add or update a single flag.
    pub fn set_flag(&self, flag: FeatureFlag) {
        self.state.update(|s| {
            s.flags.insert(flag.key.clone(), flag);
        });
    }

    /// Remove a flag.
    pub fn remove_flag(&self, key: &str) {
        self.state.update(|s| {
            s.flags.remove(key);
        });
    }

    /// Set a local override for a flag.
    ///
    /// Overrides take precedence over actual flag values.
    /// Useful for development and testing.
    pub fn set_override(&self, key: impl Into<String>, enabled: bool) {
        self.state.update(|s| {
            s.overrides.insert(key.into(), enabled);
        });
    }

    /// Remove a local override.
    pub fn remove_override(&self, key: &str) {
        self.state.update(|s| {
            s.overrides.remove(key);
        });
    }

    /// Clear all local overrides.
    pub fn clear_overrides(&self) {
        self.state.update(|s| {
            s.overrides.clear();
        });
    }

    /// Set loading state.
    pub fn set_loading(&self, loading: bool) {
        self.state.update(|s| {
            s.loading = loading;
        });
    }

    /// Set error state.
    pub fn set_error(&self, error: Option<FlagError>) {
        self.state.update(|s| {
            s.error = error;
            s.loading = false;
        });
    }

    /// Clear all flags.
    pub fn clear(&self) {
        self.state.update(|s| {
            s.flags.clear();
            s.loaded = false;
        });
    }

    // ========================================================================
    // Actions
    // ========================================================================

    /// Enable a flag (sets it to true).
    pub fn enable(&self, key: &str) {
        self.state.update(|s| {
            if let Some(flag) = s.flags.get_mut(key) {
                flag.enabled = true;
            }
        });
    }

    /// Disable a flag (sets it to false).
    pub fn disable(&self, key: &str) {
        self.state.update(|s| {
            if let Some(flag) = s.flags.get_mut(key) {
                flag.enabled = false;
            }
        });
    }

    /// Toggle a flag.
    pub fn toggle(&self, key: &str) {
        self.state.update(|s| {
            if let Some(flag) = s.flags.get_mut(key) {
                flag.enabled = !flag.enabled;
            }
        });
    }
}

impl Store for FeatureFlagStore {
    type State = FeatureFlagState;

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

// ============================================================================
// Hydration Support
// ============================================================================

#[cfg(feature = "hydrate")]
impl crate::hydration::HydratableStore for FeatureFlagStore {
    fn serialize_state(&self) -> Result<String, crate::hydration::StoreHydrationError> {
        let state = self.state.get_untracked();
        serde_json::to_string(&state)
            .map_err(|e| crate::hydration::StoreHydrationError::Serialization(e.to_string()))
    }

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

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

// ============================================================================
// Feature Component
// ============================================================================

/// Component that conditionally renders children based on a feature flag.
///
/// # Example
///
/// ```rust,ignore
/// use leptos::prelude::*;
/// use leptos_store::templates::feature_flags::*;
///
/// #[component]
/// fn App() -> impl IntoView {
///     view! {
///         <Feature flag="new_feature">
///             <NewFeatureComponent />
///         </Feature>
///
///         // With fallback
///         <Feature flag="premium_feature">
///             <PremiumContent />
///             <Fallback slot>
///                 <UpgradePrompt />
///             </Fallback>
///         </Feature>
///     }
/// }
/// ```
#[component]
pub fn Feature(
    /// The feature flag key to check.
    flag: &'static str,
    /// Whether to invert the condition (render if flag is disabled).
    #[prop(optional)]
    invert: bool,
    /// Children to render if the flag check passes.
    children: ChildrenFn,
) -> impl IntoView {
    let store = use_context::<FeatureFlagStore>();

    let is_enabled = move || {
        store
            .as_ref()
            .map(|s| {
                let enabled = s.is_enabled(flag);
                if invert { !enabled } else { enabled }
            })
            .unwrap_or(false)
    };

    move || {
        if is_enabled() {
            children().into_any()
        } else {
            ().into_any()
        }
    }
}

/// Component that renders content based on a flag variant.
///
/// # Example
///
/// ```rust,ignore
/// use leptos::prelude::*;
/// use leptos_store::templates::feature_flags::*;
///
/// #[component]
/// fn App() -> impl IntoView {
///     view! {
///         <FeatureVariant flag="hero_style" variant="modern">
///             <ModernHero />
///         </FeatureVariant>
///         <FeatureVariant flag="hero_style" variant="classic">
///             <ClassicHero />
///         </FeatureVariant>
///     }
/// }
/// ```
#[component]
pub fn FeatureVariant(
    /// The feature flag key.
    flag: &'static str,
    /// The variant to match.
    variant: &'static str,
    /// Children to render if the variant matches.
    children: ChildrenFn,
) -> impl IntoView {
    let store = use_context::<FeatureFlagStore>();

    let matches = move || {
        store
            .as_ref()
            .and_then(|s| s.get_variant(flag))
            .map(|v| v == variant)
            .unwrap_or(false)
    };

    move || {
        if matches() {
            children().into_any()
        } else {
            ().into_any()
        }
    }
}

// ============================================================================
// Context Helpers
// ============================================================================

/// Provide a feature flag store to the component tree.
pub fn provide_feature_flags(store: FeatureFlagStore) {
    provide_context(store);
}

/// Access the feature flag store from context.
pub fn use_feature_flags() -> FeatureFlagStore {
    use_context::<FeatureFlagStore>().expect("FeatureFlagStore not found in context")
}

/// Check if a feature is enabled (convenience function).
pub fn use_feature(flag: &'static str) -> impl Fn() -> bool + Clone {
    let store = use_context::<FeatureFlagStore>();
    move || store.as_ref().map(|s| s.is_enabled(flag)).unwrap_or(false)
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_feature_flag_creation() {
        let flag = FeatureFlag::new("test", true);
        assert_eq!(flag.key, "test");
        assert!(flag.enabled);
        assert!(flag.variant.is_none());
    }

    #[test]
    fn test_feature_flag_with_variant() {
        let flag = FeatureFlag::with_variant("ab_test", true, "variant_a");
        assert_eq!(flag.key, "ab_test");
        assert_eq!(flag.variant, Some("variant_a".to_string()));
    }

    #[test]
    fn test_feature_flag_with_metadata() {
        let flag = FeatureFlag::new("test", true)
            .with_description("Test flag")
            .with_metadata("owner", "team-a");

        assert_eq!(flag.description, Some("Test flag".to_string()));
        assert_eq!(flag.metadata.get("owner"), Some(&"team-a".to_string()));
    }

    #[test]
    fn test_user_context() {
        let ctx = UserContext::authenticated("user123")
            .with_email("test@example.com")
            .with_environment("production")
            .with_attribute("plan", "premium");

        assert_eq!(ctx.user_id, Some("user123".to_string()));
        assert_eq!(ctx.email, Some("test@example.com".to_string()));
        assert_eq!(ctx.environment, Some("production".to_string()));
        assert_eq!(ctx.attributes.get("plan"), Some(&"premium".to_string()));
    }

    #[test]
    fn test_feature_flag_store_creation() {
        let store = FeatureFlagStore::new();
        assert!(!store.is_loaded());
        assert!(!store.is_loading());
    }

    #[test]
    fn test_feature_flag_store_set_flags() {
        let store = FeatureFlagStore::new();
        store.set_flags(vec![
            FeatureFlag::new("flag1", true),
            FeatureFlag::new("flag2", false),
        ]);

        assert!(store.is_loaded());
        assert!(store.is_enabled("flag1"));
        assert!(!store.is_enabled("flag2"));
        assert!(!store.is_enabled("flag3")); // Unknown flag
    }

    #[test]
    fn test_feature_flag_store_overrides() {
        let store = FeatureFlagStore::new();
        store.set_flags(vec![FeatureFlag::new("feature", false)]);

        // Flag is disabled by default
        assert!(!store.is_enabled("feature"));

        // Override to enabled
        store.set_override("feature", true);
        assert!(store.is_enabled("feature"));

        // Remove override
        store.remove_override("feature");
        assert!(!store.is_enabled("feature"));

        // Clear all overrides
        store.set_override("feature", true);
        store.clear_overrides();
        assert!(!store.is_enabled("feature"));
    }

    #[test]
    fn test_feature_flag_store_variants() {
        let store = FeatureFlagStore::new();
        store.set_flags(vec![FeatureFlag::with_variant("hero", true, "modern")]);

        assert_eq!(store.get_variant("hero"), Some("modern".to_string()));
        assert_eq!(store.get_variant("unknown"), None);
    }

    #[test]
    fn test_feature_flag_store_toggle() {
        let store = FeatureFlagStore::new();
        store.set_flags(vec![FeatureFlag::new("test", false)]);

        assert!(!store.is_enabled("test"));

        store.toggle("test");
        assert!(store.is_enabled("test"));

        store.toggle("test");
        assert!(!store.is_enabled("test"));
    }

    #[test]
    fn test_feature_flag_store_enable_disable() {
        let store = FeatureFlagStore::new();
        store.set_flags(vec![FeatureFlag::new("test", false)]);

        store.enable("test");
        assert!(store.is_enabled("test"));

        store.disable("test");
        assert!(!store.is_enabled("test"));
    }

    #[test]
    fn test_feature_flag_store_with_flags() {
        let store = FeatureFlagStore::with_flags(vec![
            FeatureFlag::new("a", true),
            FeatureFlag::new("b", false),
        ]);

        assert!(store.is_loaded());
        assert_eq!(store.flag_keys().len(), 2);
    }

    #[test]
    fn test_feature_flag_store_remove_flag() {
        let store = FeatureFlagStore::with_flags(vec![
            FeatureFlag::new("a", true),
            FeatureFlag::new("b", true),
        ]);

        assert_eq!(store.all_flags().len(), 2);

        store.remove_flag("a");
        assert_eq!(store.all_flags().len(), 1);
        assert!(store.get_flag("a").is_none());
    }

    #[test]
    fn test_flag_error_display() {
        assert!(
            FlagError::NotFound("test".to_string())
                .to_string()
                .contains("not found")
        );
        assert!(
            FlagError::FetchFailed("timeout".to_string())
                .to_string()
                .contains("fetch")
        );
    }
}