Skip to main content

Registry

Struct Registry 

Source
pub struct Registry { /* private fields */ }
Expand description

Holds plugins and device instances, routes input to devices, records everything in each device’s event log and publishes BusEvents.

The registry is sans-IO: it reads time only from the injected Clock and never blocks. Methods that feed a device return the device’s DeviceEvents so the caller can deliver ToHost bytes, wedge scans and connection changes to the app.

use hardless_core::{FakeClock, Registry};
let registry = Registry::new(FakeClock::new());
assert!(registry.list().is_empty());

Implementations§

Source§

impl Registry

Source

pub fn new(clock: impl Clock + 'static) -> Self

A registry with default limits.

Source

pub fn with_config(clock: impl Clock + 'static, config: RegistryConfig) -> Self

A registry with custom limits.

Source

pub fn config(&self) -> RegistryConfig

The registry’s limits.

Source

pub fn now(&mut self) -> Millis

The current time from the injected clock, clamped so it never goes backwards.

Source

pub fn register( &mut self, plugin: impl DevicePlugin, ) -> Result<(), RegistryError>

Registers a device plugin. Each kind can be registered once.

Source

pub fn register_boxed( &mut self, plugin: Box<dyn DevicePlugin>, ) -> Result<(), RegistryError>

Registers a boxed device plugin.

Source

pub fn plugin(&self, kind: &str) -> Option<&dyn DevicePlugin>

The plugin for a kind.

Source

pub fn plugin_kinds(&self) -> impl Iterator<Item = &'static str> + '_

Registered kinds, sorted.

Source

pub fn create( &mut self, kind: &str, id: Option<DeviceId>, config: Value, ) -> Result<DeviceId, RegistryError>

Creates a device. Without an id, one is generated as <kind>-<n>. Emits device:added.

Source

pub fn remove(&mut self, id: &DeviceId) -> Result<(), RegistryError>

Removes a device. Emits device:removed.

Source

pub fn contains(&self, id: &DeviceId) -> bool

Whether a device exists.

Source

pub fn get(&self, id: &DeviceId) -> Option<&dyn Device>

A device by ID.

Source

pub fn summary(&self, id: &DeviceId) -> Option<DeviceSummary>

A device’s summary: kind, state and active faults.

Source

pub fn list(&self) -> Vec<DeviceSummary>

Every device, sorted by ID.

Source

pub fn host_bytes( &mut self, id: &DeviceId, data: &[u8], ) -> Result<Vec<DeviceEvent>, RegistryError>

Delivers host -> device bytes and returns the device’s events.

Source

pub fn host_signals( &mut self, id: &DeviceId, signals: SerialSignals, ) -> Result<(), RegistryError>

Records serial output signals set by the app (SerialPort.setSignals()) in the device’s event log. Devices do not react to them yet.

Source

pub fn control( &mut self, id: &DeviceId, command: ControlCommand, ) -> Result<Vec<DeviceEvent>, RegistryError>

Sends a control command and returns the device’s events.

Source

pub fn log( &mut self, id: &DeviceId, entry: LogEntry, ) -> Result<(), RegistryError>

Records a diagnostic in a device’s event log on behalf of a transport, e.g. a part of an HTTP print request the device cannot reproduce.

Source

pub fn set_fault( &mut self, id: &DeviceId, fault: FaultId, active: bool, options: FaultOptions, ) -> Result<Vec<DeviceEvent>, RegistryError>

Turns a fault on or off, returns the device’s events (e.g. Disconnected for offline) and emits device:state, since the active faults are part of the summary.

A fault the device does not declare in Device::faults is rejected with RegistryError::UnsupportedFault, whether it is being turned on or off, and nothing is changed or recorded.

Source

pub fn reset( &mut self, id: &DeviceId, ) -> Result<Vec<DeviceEvent>, RegistryError>

Resets a device: clears its state, jobs and faults (its event log is kept), returns the device’s events and emits device:state.

Source

pub fn tick(&mut self) -> Vec<(DeviceId, DeviceEvent)>

Runs on_tick for every device whose deadline has passed and returns their events.

Source

pub fn next_deadline(&self) -> Option<Millis>

The earliest deadline of any device: when the caller should next call Registry::tick.

Source

pub fn events( &self, id: &DeviceId, since: Option<u64>, ) -> Result<Vec<&EventRecord>, RegistryError>

A device’s event log, oldest first, optionally only records with seq > since.

Source

pub fn jobs( &self, id: &DeviceId, ) -> Result<impl Iterator<Item = &Job> + '_, RegistryError>

A device’s completed jobs, newest first.

Source

pub fn last_job(&self, id: &DeviceId) -> Result<Option<&Job>, RegistryError>

A device’s latest job.

Source

pub fn drain_bus(&mut self) -> Vec<BusEvent>

Takes every event published on the bus since the last drain, oldest first.

Trait Implementations§

Source§

impl Debug for Registry

Source§

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

Formats the value using the given formatter. 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.

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