Skip to main content

stateset_embedded/
integration_mappings.rs

1//! Integration mapping operations (external↔internal value translation).
2
3use stateset_core::{
4    CreateIntegrationMapping, IntegrationMapping, IntegrationMappingFilter, IntegrationMappingId,
5    MappingLookup, Result, UpdateIntegrationMapping,
6};
7use stateset_db::{Database, DatabaseCapability};
8use std::sync::Arc;
9
10/// Integration mapping operations.
11pub struct IntegrationMappings {
12    db: Arc<dyn Database>,
13}
14
15impl std::fmt::Debug for IntegrationMappings {
16    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17        f.debug_struct("IntegrationMappings").finish_non_exhaustive()
18    }
19}
20
21impl IntegrationMappings {
22    pub(crate) fn new(db: Arc<dyn Database>) -> Self {
23        Self { db }
24    }
25
26    /// Whether integration mappings are supported by the active backend.
27    #[must_use]
28    pub fn is_supported(&self) -> bool {
29        self.db.supports_capability(DatabaseCapability::IntegrationMappings)
30    }
31
32    fn ensure(&self) -> Result<()> {
33        self.db.ensure_capability(DatabaseCapability::IntegrationMappings)
34    }
35
36    /// Create a new integration mapping.
37    pub fn create(&self, input: CreateIntegrationMapping) -> Result<IntegrationMapping> {
38        self.ensure()?;
39        self.db.integration_mappings().create(input)
40    }
41
42    /// Get a mapping by ID.
43    pub fn get(&self, id: IntegrationMappingId) -> Result<Option<IntegrationMapping>> {
44        self.ensure()?;
45        self.db.integration_mappings().get(id)
46    }
47
48    /// Update a mapping.
49    pub fn update(
50        &self,
51        id: IntegrationMappingId,
52        input: UpdateIntegrationMapping,
53    ) -> Result<IntegrationMapping> {
54        self.ensure()?;
55        self.db.integration_mappings().update(id, input)
56    }
57
58    /// List mappings with optional filtering.
59    pub fn list(&self, filter: IntegrationMappingFilter) -> Result<Vec<IntegrationMapping>> {
60        self.ensure()?;
61        self.db.integration_mappings().list(filter)
62    }
63
64    /// Delete a mapping.
65    pub fn delete(&self, id: IntegrationMappingId) -> Result<()> {
66        self.ensure()?;
67        self.db.integration_mappings().delete(id)
68    }
69
70    /// Bulk upsert mappings. Returns the number of rows affected.
71    pub fn bulk_upsert(&self, items: Vec<CreateIntegrationMapping>) -> Result<u64> {
72        self.ensure()?;
73        self.db.integration_mappings().bulk_upsert(items)
74    }
75
76    /// Resolve the internal value for an external value.
77    pub fn resolve(&self, lookup: &MappingLookup) -> Result<Option<String>> {
78        self.ensure()?;
79        self.db.integration_mappings().resolve(lookup)
80    }
81}