Skip to main content

pax_runtime_api/
store.rs

1//! Typed stores owned by mounted nodes and discovered through `NodeContext`.
2
3/// Marker trait for values published with `NodeContext::provide_store`.
4///
5/// The concrete Rust type is the lookup key. Each mounted owner can provide one
6/// value per type; a nearer provider of that type shadows an ancestor's value.
7/// Prefer a named struct or newtype for each purpose. A type alias does not
8/// create a distinct key.
9///
10/// A store is ordinary Rust data and does not need `#[pax]`. Use `Property`
11/// handles inside it for reactive state; plain fields do not become reactive.
12/// The owner clears its providers at final unmount. Explicitly cloned handles
13/// can outlive that registration, but do not reconnect to a later mount.
14pub trait Store: 'static {}
15
16/// Failure to register or borrow a store through a `NodeContext`.
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub enum StoreError {
19    /// No provider of the requested type exists on this node or its logical ancestors.
20    Missing { type_name: &'static str },
21    /// The context is not mounted, its mount has ended, or its provider ancestry has expired.
22    ExpiredScope,
23    /// The selected provider is already borrowed by a `with_store` closure.
24    /// Lookup does not fall through to an outer provider in this case.
25    BorrowConflict { type_name: &'static str },
26}
27
28impl std::fmt::Display for StoreError {
29    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30        match self {
31            Self::Missing { type_name } => write!(f, "no provider for store {type_name}"),
32            Self::ExpiredScope => f.write_str("store scope is not mounted or has expired"),
33            Self::BorrowConflict { type_name } => {
34                write!(f, "store {type_name} is already borrowed")
35            }
36        }
37    }
38}
39
40impl std::error::Error for StoreError {}