Struct AppState

Source
pub struct AppState {
    pub watches: RwLock<HashMap<String, WatchContext>>,
}
Expand description

§AppState

§Description

The AppState structure represents the in-memory runtime state of the orchestrator.
It manages all active WatchContext instances in a concurrent-safe way using an RwLock<HashMap<String, WatchContext>>.

This state is central to the daemon’s operation: it allows concurrent tasks (e.g., socket requests, watchers) to safely read and modify the currently tracked projects.


§Fields

  • watches: RwLock<HashMap<String, WatchContext>>
    A thread-safe map of project identifiers (id) to their associated WatchContext.

§Associated Methods

§load_from_disk() -> Result<Self>

Loads the state from the persisted watches.json file.

  • Reconstructs all WatchContext objects from disk.
  • Re-initializes their loggers.
  • Returns a fully built AppState.

Example:

let state = AppState::load_from_disk().await?;

§save_to_disk(&self) -> Result<()>

Serializes the current state into the watches.json file.
Ensures that all current contexts (WatchContext) are persisted.

Example:

state.save_to_disk().await?;

§init_watch_file() -> Result<()>

Initializes the watches.json file if it does not exist.

  • Ensures the parent directory exists.
  • Creates an empty WatchRegistry if no registry file is found.

Example:

AppState::init_watch_file().await?;

§load_watches() -> Result<WatchRegistry>

Loads the contents of watches.json and deserializes it into a WatchRegistry.
This is a low-level helper used internally by other methods.

Example:

let registry = AppState::load_watches().await?;

§add_watch(ctx: &WatchContext) -> Result<()>

Adds or updates a WatchContext inside the watches.json file.

  • If a project with the same project_dir already exists, it is updated.
  • Otherwise, the new watch is appended.

Example:

let ctx = WatchContextBuilder::new(...).build().await?;
AppState::add_watch(&ctx).await?;

§remove_watch_by_id(id: &str) -> Result<()>

Removes a WatchContext from watches.json by its identifier.

Example:

AppState::remove_watch_by_id("project-123").await?;

§Usage Example

AppState is typically wrapped in an Arc and shared across async tasks.
For instance, in a socket listener:

pub async fn start_socket_listener(state: Arc<AppState>) -> anyhow::Result<()> {
    // state is cloned into each request handler
    let state = Arc::clone(&state);
    // requests are processed concurrently
}

This makes AppState the central shared runtime state for the orchestrator daemon.


§Notes

  • AppState bridges persistent storage (watches.json) with in-memory runtime state.
  • It guarantees safe concurrent access to watches via RwLock.
  • It is designed to be long-lived and shared across tasks via Arc<AppState>.

Fields§

§watches: RwLock<HashMap<String, WatchContext>>

Implementations§

Source§

impl AppState

Source

pub async fn load_from_disk() -> Result<Self, Error>

Source

pub async fn save_to_disk(&self) -> Result<()>

Source

pub async fn init_watch_file() -> Result<()>

Source

pub async fn load_watches() -> Result<WatchRegistry>

load watches.json file and Serialize it into WatchRegistry

Source

pub async fn add_watch(ctx: &WatchContext) -> Result<()>

adds a view to the watches.json file

Source

pub async fn remove_watch_by_id(id: &str) -> Result<()>

deletes a WatchContext in the watches.json file

Trait Implementations§

Source§

impl Default for AppState

Source§

fn default() -> AppState

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

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

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
§

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. 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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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

impl<T> ErasedDestructor for T
where T: 'static,