Skip to main content

Session

Struct Session 

Source
pub struct Session<D: Document> { /* private fields */ }
Expand description

The state commands run on: the document Store, the file it came from, view state and the effects commands ask of the window. Commands are fn(&mut Session<D>, &Value).

Implementations§

Source§

impl<D: Document> Session<D>

Source

pub fn new(format: Arc<Format<D>>) -> Session<D>

A session with a new, empty document.

Source

pub fn is_windowed(&self) -> bool

Whether a window can show dialogs (false in CLI / MCP sessions).

Source

pub fn doc(&self) -> &D

The current document (inside a command: its working copy).

Source

pub fn snapshot(&self) -> Arc<D> ⓘ

The current document as an Arc (for a job or another thread).

Source

pub fn store(&self) -> &Store<D>

The store (history, revision, dirty state). Changing it directly from inside a command drops the command’s own edits; use Session::edit there.

Source

pub fn store_mut(&mut self) -> &mut Store<D>

Source

pub fn revision(&self) -> Revision

Source

pub fn is_dirty(&self) -> bool

Source

pub fn format(&self) -> &Format<D>

Source

pub fn edit<R>(&mut self, label: &str, f: impl FnOnce(&mut D) -> R) -> R

Change the document. Inside a command the change joins the command’s single undo step; outside (a view, a job’s apply) it is a step of its own called label.

Source

pub fn edit_merged<R>( &mut self, label: &str, merge_key: &str, f: impl FnOnce(&mut D) -> R, ) -> R

Session::edit that folds into the previous step with the same label and merge key (one undo step for a whole drag).

Source

pub fn try_edit<R, E>( &mut self, label: &str, f: impl FnOnce(&mut D) -> Result<R, E>, ) -> Result<R, E>

A change that may fail: on error nothing changes.

Source

pub fn can_undo(&self) -> bool

Source

pub fn can_redo(&self) -> bool

Source

pub fn undo(&mut self) -> Option<String>

Undo the last step; returns its label.

Source

pub fn redo(&mut self) -> Option<String>

Redo the next step; returns its label.

Source

pub fn path(&self) -> Option<&Path>

The file the document was opened from or saved to.

Source

pub fn display_name(&self) -> String

The document’s display name: the file name, or “Untitled”.

Source

pub fn migrated_from(&self) -> Option<u64>

The schema version the open file had before it was upgraded on load, if it was.

Source

pub fn file_generation(&self) -> u64

Changes when the file (path) changes; with the revision it keys caches of file state.

Source

pub fn new_document(&mut self)

Start a new, empty document.

Source

pub fn open(&mut self, path: &Path) -> Result<(), FormatError>

Open path (migrating older files). On error the current document stays.

Source

pub fn replace( &mut self, doc: D, path: Option<PathBuf>, migrated_from: Option<u64>, )

Replace the document with doc as if opened from path: clears the history, clean.

Source

pub fn replace_as_edit(&mut self, label: &str, doc: D)

Replace the document with doc as an undoable, unsaved change (recovered work, an import).

Source

pub fn save(&mut self) -> Result<PathBuf, FormatError>

Save to the current file. Errors when the document has never been saved.

Source

pub fn save_as(&mut self, path: &Path) -> Result<(), FormatError>

Save to path and make it the document’s file.

Source

pub fn view_state(&self) -> &Map<String, Value>

App-defined view state (ui.set merges into it; it is not part of the document).

Source

pub fn view<T: DeserializeOwned>(&self, key: &str) -> Option<T>

Source

pub fn view_or<T: DeserializeOwned>(&self, key: &str, default: T) -> T

Source

pub fn set_view<T: Serialize + ?Sized>(&mut self, key: &str, value: &T)

Source

pub fn set_view_value(&mut self, key: &str, value: Value)

Source

pub fn view_generation(&self) -> u64

Changes whenever the view state changes.

Source

pub fn request(&mut self, effect: Effect)

Ask the window for something (ignored headless).

Source

pub fn toast(&mut self, level: ToastLevel, text: impl Into<String>)

Show a message (headless: logged).

Trait Implementations§

Source§

impl<D: Document> Host for Session<D>

Source§

type Error = CommandError

The error type command bodies return. CommandError itself, or the app’s own error with From<AppError> for CommandError (leave the id empty: the chokepoint fills it in).
Source§

fn journal(&mut self) -> &mut Journal

The execution state: nesting depth and journal.
Source§

fn before( &mut self, _spec: &CommandSpec<Self>, _params: &mut Value, depth: usize, ) -> Result<(), CommandError>

Rewrite params before validation (resolve names to ids, settle an ongoing gesture…). An Err refuses the call: Host::rejected sees it converted, and Registry::execute_host hands it back unchanged as ExecError::Host.
Source§

fn after( &mut self, spec: &CommandSpec<Self>, _params: &Value, result: &Result<Value, CommandError>, depth: usize, )

Called after every run (ok, error or panic), at every nesting depth.
Source§

fn rejected(&mut self, id: &str, error: &CommandError, depth: usize)

Called when a call is refused before running: unknown command, bad params, disabled, too deeply nested, or an error from Host::before.
Source§

fn validate( &mut self, _spec: &CommandSpec<Self>, _params: &Value, _depth: usize, ) -> Result<(), CommandError>

Extra parameter checks after the registry’s Validation policy and before Host::enabled_for (an Err is refused and goes to Host::rejected). With Validation::Off this is where a host validates when it wants to, e.g. only at depth 0 or only for agent calls. Default: nothing.
Source§

fn enabled_for( &mut self, spec: &CommandSpec<Self>, _params: &Value, ) -> Result<(), String>

Enabled check for one call; override when explicit targets in params change availability (the state may be adjusted temporarily, e.g. targets swapped in for the selection).
Source§

fn scrub(&self, _id: &str, _params: &mut Value)

Remove secrets from params before they reach the journal.

Auto Trait Implementations§

§

impl<D> !RefUnwindSafe for Session<D>

§

impl<D> !UnwindSafe for Session<D>

§

impl<D> Freeze for Session<D>
where Store<D>: Freeze, Arc<Format<D>>: Freeze, Option<Work<D>>: Freeze,

§

impl<D> Send for Session<D>
where Store<D>: Send, Arc<Format<D>>: Send, Option<Work<D>>: Send,

§

impl<D> Sync for Session<D>
where Store<D>: Sync, Arc<Format<D>>: Sync, Option<Work<D>>: Sync,

§

impl<D> Unpin for Session<D>
where Store<D>: Unpin, Arc<Format<D>>: Unpin, Option<Work<D>>: Unpin,

§

impl<D> UnsafeUnpin for Session<D>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send> ⓘ

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(_simd: S, value: T) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,

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