ridl-backend-rust 0.2.0

Compiles a RIDL IR package to Rust source, plus the generated interaction face.
Documentation
---
source: crates/ridl-backend-rust/src/tests.rs
expression: source
---
/// Where a signal's current value came from (ridl §4.4, §4.5).
///
/// `Init` is the channel's seeded value, before the provider's first
/// publication; `Live` is a published value; `Invalid` marks a value
/// that violated the payload constraints — the malformed value is not
/// delivered, but its invalidity is, so no subscriber silently holds
/// stale last-good data.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Provenance {
    Init,
    Live,
    Invalid,
}
/// A signal channel: state that always holds a value (ridl §4.4).
pub trait SignalHandle<T> {
    /// The current value and where it came from. Never fails: the
    /// channel is seeded with the init value at creation.
    fn read(&self) -> (T, Provenance);
    /// Registers a subscriber. It is called immediately with the
    /// current value, then on every change.
    fn subscribe(&mut self, f: Box<dyn FnMut(&T, Provenance)>);
}
/// An event channel: occurrences, which are not state (ridl §5). There
/// is no `read` — an occurrence that has not happened has no value.
pub trait EventHandle<T> {
    fn subscribe(&mut self, f: Box<dyn FnMut(&T)>);
}
/// A finite or unbounded sequence of payloads (ridl §12).
///
/// Declared here rather than taken from a runtime crate so the
/// generated module stays dependency-free; it is shaped so an adapter
/// to `futures::Stream` is a blanket impl.
pub trait RidlStream {
    type Item;
    fn poll_next(
        self: core::pin::Pin<&mut Self>,
        cx: &mut core::task::Context<'_>,
    ) -> core::task::Poll<Option<Self::Item>>;
}
/// How a timing annotation constrains an interaction (ridl §9).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TimingMode {
    StrictPeriodic,
    Range,
}
/// One interaction's resolved timing, in exact microseconds.
///
/// `min_us` is the rate floor and `max_us` the staleness bound; a strict
/// period carries the same value in both. On a `command` or `query` the
/// same two bounds are the call throttle and the response bound
/// (ridl §9, ADR-0015 decision 3). `default_applied` records that
/// the contract was written without a `@` annotation and the configured
/// default was resolved in (ridl §9.1) — always `false` for an RPC,
/// whose bounds are never defaulted.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TimingConst {
    pub mode: TimingMode,
    pub min_us: Option<u64>,
    pub max_us: Option<u64>,
    pub default_applied: bool,
}
/// Which side of an interaction a contract clause constrains (ridl §13).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ContractKind {
    Require,
    Ensure,
}
/// One contract clause as data, for an observer to install.
///
/// `id` is the observer address the IR assigns; it is a stable identity
/// carried verbatim, not a Rust name. `uses_result` says whether the
/// clause reads the query's result, which an `ensure` observer must
/// know before it can be scheduled — it cannot run until the result
/// exists. The flag is carried rather than inferred: `source` is text,
/// so matching on it would misread a parameter named `resultCode` or a
/// field access `.result`, and an `ensure` clause does not always read
/// the result.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ContractStub {
    pub id: &'static str,
    pub kind: ContractKind,
    pub source: &'static str,
    pub signals: &'static [&'static str],
    pub params: &'static [&'static str],
    pub uses_result: bool,
}
/// Adaptive cruise
/// The consumer face of `CruiseControl` — what a component that uses this interface calls (ridl §10.1).
#[allow(async_fn_in_trait)]
pub trait CruiseControlConsumer {
    /// signal `engaged` — ordinal 1 (ridl §4).
    /// The channel is never empty: a read before the provider's first publication yields `0`, and every read carries its provenance (`Init`, `Live`, or `Invalid` — ridl §4.4, §4.5).
    fn engaged(&mut self) -> &mut dyn SignalHandle<Engagement>;
}
/// Adaptive cruise
/// The provider face of `CruiseControl` — what a component that implements this interface fulfils (ridl §10.1). A `fixed` has no entry here: it is provisioned externally and populated at binding initialization (ridl §8).
#[allow(async_fn_in_trait)]
pub trait CruiseControlProvider {
    /// Publishes `engaged` — signal ordinal 1 (ridl §4).
    fn publish_engaged(&mut self, value: Engagement);
}
/// Resolved timing for every timed interaction of `CruiseControl`, keyed by the source name, in exact microseconds (ridl §9): rate floor and staleness bound on a signal or event, call throttle and response bound on a command or query (ADR-0015 decision 3).
pub const CRUISE_CONTROL_TIMING: &[(&str, TimingConst)] = &[
    (
        "engaged",
        TimingConst {
            mode: TimingMode::Range,
            min_us: Some(100000),
            max_us: Some(1000000),
            default_applied: false,
        },
    ),
];
/// Every `require` and `ensure` clause of `CruiseControl` as data, in source order, for an observer to install (ridl §13).
pub const CRUISE_CONTROL_CONTRACTS: &[ContractStub] = &[];
/// Cabin climate
/// The consumer face of `ServiceVehHvacCabin` — what a component that uses this interface calls (ridl §10.1).
#[allow(async_fn_in_trait)]
pub trait ServiceVehHvacCabinConsumer {
    /// command `setTarget` — ordinal 1 (ridl §6).
    /// Always returns `()`. A delivery acknowledgment travels beneath the call — the receiving binding confirms received and accepted for execution — but it carries no functional payload, never reaches the contract surface, and is not application-visible as a return value (ridl §6.1). Observable results travel back as state.
    /// A transport failure is an infrastructure failure — detected, undeclared (gf §6.4): it is carried by runtime types, never by this signature.
    async fn set_target(&self, target: Temperature);
}
/// Cabin climate
/// The provider face of `ServiceVehHvacCabin` — what a component that implements this interface fulfils (ridl §10.1). A `fixed` has no entry here: it is provisioned externally and populated at binding initialization (ridl §8).
#[allow(async_fn_in_trait)]
pub trait ServiceVehHvacCabinProvider {
    /// Handles `setTarget` — command ordinal 1 (ridl §6). The binding has already validated the payload and the `require` clauses; a violating command never reaches this method (ridl §6.2).
    async fn on_set_target(&mut self, target: Temperature);
}
/// Resolved timing for every timed interaction of `ServiceVehHvacCabin`, keyed by the source name, in exact microseconds (ridl §9): rate floor and staleness bound on a signal or event, call throttle and response bound on a command or query (ADR-0015 decision 3).
pub const SERVICE_VEH_HVAC_CABIN_TIMING: &[(&str, TimingConst)] = &[];
/// Every `require` and `ensure` clause of `ServiceVehHvacCabin` as data, in source order, for an observer to install (ridl §13).
pub const SERVICE_VEH_HVAC_CABIN_CONTRACTS: &[ContractStub] = &[];
/// Every service this package declares: the deployment address and the
/// interface that answers at it (ridl §11) — one row per composed
/// interface, in slot order, so a service composing several interfaces
/// repeats its address (ridl §14.5).
///
/// The address is the contract's own dotted identity, carried verbatim;
/// the interface name is the generated Rust type prefix. For an inline
/// shape they differ by construction, and the address — not the type
/// name — is the identity the wire and the observers use.
pub const SERVICES: &[(&str, &str)] = &[
    ("veh.adas.cruise", "CruiseControl"),
    ("veh.hvac.cabin", "ServiceVehHvacCabin"),
];