Skip to main content

wami_core/
traits.rs

1//! Domain-agnostic traits shared across the workspace.
2//!
3//! These live in `wami-core` rather than a crate of their own because they are
4//! 37 lines with no dependencies of their own, and a separate crate for them
5//! would have to be published for anyone to depend on `wami` at all. See #129.
6//!
7//! [`Service`] and [`ServiceRegistry`] are what `wami_macros::Service` and
8//! `wami_macros::register_services!` expand to, so a crate using those macros
9//! needs `wami-core` in scope — the macros name it absolutely, as `::wami_core`.
10
11use crate::error::Result;
12use std::sync::Arc;
13
14/// Generic CRUD trait for backing stores.
15#[allow(clippy::result_large_err)]
16pub trait Store<T>: Send + Sync {
17    /// Insert or update a model in the store.
18    fn insert(&self, model: T) -> Result<()>;
19
20    /// Retrieve a model by identifier.
21    fn get(&self, id: &str) -> Result<Option<T>>;
22
23    /// Delete a model by identifier.
24    fn delete(&self, id: &str) -> Result<()>;
25}
26
27/// Abstraction over high-level services exposed by the platform.
28pub trait Service: Send + Sync {
29    type Request;
30    type Response;
31    type Error;
32
33    fn handle(&self, req: Self::Request) -> std::result::Result<Self::Response, Self::Error>;
34}
35
36/// Dependency-injection mechanism for resolving services at runtime.
37pub trait ServiceRegistry: Send + Sync {
38    fn register<S>(&mut self, name: &str, service: Arc<S>)
39    where
40        S: Service + 'static;
41
42    fn get<S>(&self, name: &str) -> Option<Arc<S>>
43    where
44        S: Service + 'static;
45}