Skip to main content

icydb_core/interface/
query.rs

1use crate::{
2    db::query::{SaveQuery, plan::__internal::ExecutablePlanErased},
3    error::{ErrorClass, ErrorOrigin, InternalError},
4    prelude::*,
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 InternalError {
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(ExecutablePlanErased) -> Result<Vec<Key>, InternalError>;
34
35/// Function pointer that executes a save query for a specific entity type.
36pub type SaveHandler = fn(SaveQuery) -> Result<Key, InternalError>;
37
38/// Function pointer that executes a delete query for a specific entity type.
39pub type DeleteHandler = fn(ExecutablePlanErased) -> Result<Vec<Key>, InternalError>;
40
41///
42/// EntityDispatch
43///
44/// Metadata and typed handlers for a single entity path.
45///
46/// Generated actor code exposes a `dispatch_entity(path)` function that returns this,
47/// letting you authorize per-entity before invoking the handlers. No canister
48/// endpoints are generated automatically.
49///
50
51#[derive(Clone, Copy)]
52pub struct EntityDispatch {
53    pub entity_name: &'static str,
54    pub path: &'static str,
55    pub load_keys: LoadHandler,
56    pub save_key: SaveHandler,
57    pub delete_keys: DeleteHandler,
58}