Skip to main content

stateset_embedded/
search_config.rs

1//! Search configuration operations for managing search settings
2//!
3//! # Example
4//!
5//! ```rust,ignore
6//! use stateset_embedded::{Commerce, CreateSearchConfig};
7//!
8//! let commerce = Commerce::new("./store.db")?;
9//!
10//! let config = commerce.search_config().create(CreateSearchConfig {
11//!     name: "Default Search".into(),
12//!     ..Default::default()
13//! })?;
14//!
15//! // Set it as the active configuration
16//! let config = commerce.search_config().set_active(config.id)?;
17//! println!("Active config: {}", config.name);
18//! # Ok::<(), stateset_embedded::CommerceError>(())
19//! ```
20
21use stateset_core::{
22    CreateSearchConfig, Result, SearchConfig, SearchConfigFilter, SearchConfigId,
23    UpdateSearchConfig,
24};
25use stateset_db::{Database, DatabaseCapability};
26use std::sync::Arc;
27
28/// Search configuration operations.
29pub struct SearchConfigs {
30    db: Arc<dyn Database>,
31}
32
33impl std::fmt::Debug for SearchConfigs {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        f.debug_struct("SearchConfigs").finish_non_exhaustive()
36    }
37}
38
39impl SearchConfigs {
40    pub(crate) fn new(db: Arc<dyn Database>) -> Self {
41        Self { db }
42    }
43
44    /// Whether search configuration management is supported by the active backend.
45    #[must_use]
46    pub fn is_supported(&self) -> bool {
47        self.db.supports_capability(DatabaseCapability::SearchConfigs)
48    }
49
50    fn ensure_supported(&self) -> Result<()> {
51        self.db.ensure_capability(DatabaseCapability::SearchConfigs)
52    }
53
54    /// Create a new search configuration.
55    ///
56    /// # Example
57    ///
58    /// ```rust,ignore
59    /// use stateset_embedded::{Commerce, CreateSearchConfig};
60    ///
61    /// let commerce = Commerce::new("./store.db")?;
62    ///
63    /// let config = commerce.search_config().create(CreateSearchConfig {
64    ///     name: "Product Search v2".into(),
65    ///     ..Default::default()
66    /// })?;
67    /// # Ok::<(), stateset_embedded::CommerceError>(())
68    /// ```
69    pub fn create(&self, input: CreateSearchConfig) -> Result<SearchConfig> {
70        self.ensure_supported()?;
71        self.db.search_configs().create(input)
72    }
73
74    /// Get a search configuration by ID.
75    pub fn get(&self, id: SearchConfigId) -> Result<Option<SearchConfig>> {
76        self.ensure_supported()?;
77        self.db.search_configs().get(id)
78    }
79
80    /// Update a search configuration.
81    pub fn update(&self, id: SearchConfigId, input: UpdateSearchConfig) -> Result<SearchConfig> {
82        self.ensure_supported()?;
83        self.db.search_configs().update(id, input)
84    }
85
86    /// List search configurations with optional filtering.
87    pub fn list(&self, filter: SearchConfigFilter) -> Result<Vec<SearchConfig>> {
88        self.ensure_supported()?;
89        self.db.search_configs().list(filter)
90    }
91
92    /// Delete a search configuration.
93    pub fn delete(&self, id: SearchConfigId) -> Result<()> {
94        self.ensure_supported()?;
95        self.db.search_configs().delete(id)
96    }
97
98    /// Get the currently active search configuration.
99    pub fn get_active(&self) -> Result<Option<SearchConfig>> {
100        self.ensure_supported()?;
101        self.db.search_configs().get_active()
102    }
103
104    /// Set a configuration as active (deactivating any current one).
105    pub fn set_active(&self, id: SearchConfigId) -> Result<SearchConfig> {
106        self.ensure_supported()?;
107        self.db.search_configs().set_active(id)
108    }
109}