pub struct SceneStore { /* private fields */ }Expand description
Thread-safe scene storage shared across MCP, WebSocket, and HTTP.
§Example
use canvas_core::store::SceneStore;
use canvas_core::{Element, ElementKind};
let store = SceneStore::new();
// Add an element to the default session
let element = Element::new(ElementKind::Text {
content: "Hello".to_string(),
font_size: 16.0,
color: "#000000".to_string(),
});
let id = store.add_element("default", element).unwrap();Implementations§
Source§impl SceneStore
impl SceneStore
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new store with a default session (no persistence).
The default session is created with an 800x600 viewport.
Sourcepub fn with_data_dir(data_dir: impl Into<PathBuf>) -> Result<Self, StoreError>
pub fn with_data_dir(data_dir: impl Into<PathBuf>) -> Result<Self, StoreError>
Create a store with filesystem persistence.
Sessions are saved as JSON files in data_dir. The directory is created
if it doesn’t exist.
§Errors
Returns StoreError::Io if the directory cannot be created.
Sourcepub fn get_or_create(&self, session_id: &str) -> Scene
pub fn get_or_create(&self, session_id: &str) -> Scene
Get or create a scene for the given session ID.
If the session does not exist, a new scene with default viewport is created.
Sourcepub fn replace(&self, session_id: &str, scene: Scene) -> Result<(), StoreError>
pub fn replace(&self, session_id: &str, scene: Scene) -> Result<(), StoreError>
Replace the entire scene for a session.
Creates the session if it does not exist.
§Errors
Returns StoreError::LockPoisoned if the lock is poisoned (currently
recovered from, so this variant is reserved for future stricter modes).
Sourcepub fn update<F>(&self, session_id: &str, f: F) -> Result<(), StoreError>
pub fn update<F>(&self, session_id: &str, f: F) -> Result<(), StoreError>
Update a scene using a closure.
The closure receives a mutable reference to the scene and can modify it.
§Errors
Returns StoreError::SessionNotFound if the session does not exist.
Sourcepub fn add_element(
&self,
session_id: &str,
element: Element,
) -> Result<ElementId, StoreError>
pub fn add_element( &self, session_id: &str, element: Element, ) -> Result<ElementId, StoreError>
Add an element to a session’s scene.
Creates the session if it does not exist.
§Errors
Currently infallible but returns Result for API consistency.
Sourcepub fn remove_element(
&self,
session_id: &str,
id: ElementId,
) -> Result<(), StoreError>
pub fn remove_element( &self, session_id: &str, id: ElementId, ) -> Result<(), StoreError>
Remove an element from a session’s scene.
§Errors
Returns StoreError::SessionNotFound if the session does not exist.
Returns StoreError::ElementNotFound if the element does not exist.
Sourcepub fn update_element<F>(
&self,
session_id: &str,
id: ElementId,
f: F,
) -> Result<(), StoreError>
pub fn update_element<F>( &self, session_id: &str, id: ElementId, f: F, ) -> Result<(), StoreError>
Update an element using a closure.
§Errors
Returns StoreError::SessionNotFound if the session does not exist.
Returns StoreError::ElementNotFound if the element does not exist.
Sourcepub fn scene_document(&self, session_id: &str) -> SceneDocument
pub fn scene_document(&self, session_id: &str) -> SceneDocument
Get the canonical document representation of a scene.
If the session does not exist, returns a document for an empty scene.
Sourcepub fn session_ids(&self) -> Vec<String>
pub fn session_ids(&self) -> Vec<String>
Get a list of all session IDs.
Sourcepub fn load_session_from_disk(&self, session_id: &str) -> Result<(), StoreError>
pub fn load_session_from_disk(&self, session_id: &str) -> Result<(), StoreError>
Load a single session from disk into memory.
§Errors
Returns an error if the file doesn’t exist or can’t be parsed.
Sourcepub fn load_all_sessions(&self) -> Result<Vec<String>, StoreError>
pub fn load_all_sessions(&self) -> Result<Vec<String>, StoreError>
Discover and load all persisted sessions from the data directory.
Returns a list of session IDs that were found on disk.
§Errors
Returns an error if the data directory can’t be read.
Sourcepub fn delete_session_file(&self, session_id: &str)
pub fn delete_session_file(&self, session_id: &str)
Remove a session’s persisted file from disk.
No-op if the store has no data directory or the file doesn’t exist.
Sourcepub fn clear(&self, session_id: &str) -> Result<(), StoreError>
pub fn clear(&self, session_id: &str) -> Result<(), StoreError>
Clear all elements from a session’s scene.
§Errors
Returns StoreError::SessionNotFound if the session does not exist.
Trait Implementations§
Source§impl Clone for SceneStore
impl Clone for SceneStore
Source§fn clone(&self) -> SceneStore
fn clone(&self) -> SceneStore
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more