icydb_core/interface/
query.rs

1use crate::{
2    Key,
3    db::query::{DeleteQuery, LoadQuery, SaveQuery},
4    runtime_error::{ErrorClass, ErrorOrigin, RuntimeError},
5};
6use thiserror::Error as ThisError;
7
8///
9/// QueryError
10///
11
12#[derive(Debug, ThisError)]
13pub enum QueryError {
14    #[error("entity not found: {0}")]
15    EntityNotFound(String),
16}
17
18impl From<QueryError> for RuntimeError {
19    fn from(err: QueryError) -> Self {
20        Self::new(err.class(), ErrorOrigin::Interface, err.to_string())
21    }
22}
23
24impl QueryError {
25    pub(crate) const fn class(&self) -> ErrorClass {
26        match self {
27            Self::EntityNotFound(_) => ErrorClass::Unsupported,
28        }
29    }
30}
31
32/// Function pointer that executes a load query for a specific entity type.
33pub type LoadHandler = fn(LoadQuery) -> Result<Vec<Key>, RuntimeError>;
34
35/// Function pointer that executes a save query for a specific entity type.
36pub type SaveHandler = fn(SaveQuery) -> Result<Key, RuntimeError>;
37
38/// Function pointer that executes a delete query for a specific entity type.
39pub type DeleteHandler = fn(DeleteQuery) -> Result<Vec<Key>, RuntimeError>;
40
41/// Metadata and typed handlers for a single entity path.
42///
43/// Generated actor code exposes a `dispatch_entity(path)` function that returns this,
44/// letting you authorize per-entity before invoking the handlers. No canister
45/// endpoints are generated automatically.
46#[derive(Clone, Copy)]
47pub struct EntityDispatch {
48    pub entity_id: u64,
49    pub path: &'static str,
50    pub load_keys: LoadHandler,
51    pub save_key: SaveHandler,
52    pub delete_keys: DeleteHandler,
53}