Skip to main content

SceneStore

Struct SceneStore 

Source
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

Source

pub fn new() -> Self

Create a new store with a default session (no persistence).

The default session is created with an 800x600 viewport.

Source

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.

Source

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.

Source

pub fn get(&self, session_id: &str) -> Option<Scene>

Get a scene by session ID if it exists.

Source

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).

Source

pub fn update<F>(&self, session_id: &str, f: F) -> Result<(), StoreError>
where F: FnOnce(&mut Scene),

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.

Source

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.

Source

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.

Source

pub fn update_element<F>( &self, session_id: &str, id: ElementId, f: F, ) -> Result<(), StoreError>
where F: FnOnce(&mut Element),

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.

Source

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.

Source

pub fn session_ids(&self) -> Vec<String>

Get a list of all session IDs.

Source

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.

Source

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.

Source

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.

Source

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

Source§

fn clone(&self) -> SceneStore

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SceneStore

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SceneStore

Source§

fn default() -> SceneStore

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more