1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//! # phoxal
//!
//! A production-oriented framework for autonomous robots.
//!
//! Phoxal gives a robot a small, strongly-typed core: a contract bus over
//! [Zenoh](https://zenoh.io), current production `v1` plus evolving preview `v2` contracts,
//! and a
//! participant authoring model where one struct plus a couple of attribute
//! macros is a complete service, driver, tool, or simulator. The framework owns the
//! awkward parts - argument parsing, bus connection, scheduling, query serving,
//! shutdown, and health - so the code you write is the robot's behavior, not its
//! plumbing.
//!
//! Three ideas hold it together:
//!
//! - **A typed contract bus.** Every message is a plain serde body bound to one
//! version-qualified contract name. Handles are body-typed
//! ([`Publisher<T>`](bus::Publisher), [`Subscriber<T>`](bus::Subscriber),
//! [`Latest<T>`](bus::Latest), [`Querier<Req, Resp>`](bus::Querier)), so the
//! compiler - not a late check - rejects sending the wrong type on a topic.
//! - **No per-participant API version ceiling.** API versions are conventional vN modules
//! (`phoxal_api::v1`, …), not semver crates. A participant's `Api` handle
//! struct may mix bodies from different versions freely across its fields -
//! compatibility is per-contract name identity, realized on the wire by the
//! version-qualified key (D1); there is no `schema_id`.
//! - **Participants are authored, not wired.** You write a `Config` struct, an
//! `Api` handle struct, a state struct, and an `impl`;
//! [`#[derive(Config)]`](derive@Config) / [`#[derive(Api)]`](derive@Api) plus
//! [`#[phoxal::service|driver|simulator|tool]`](macro@service) and
//! [`#[phoxal::behavior]`](macro@behavior) derive the static metadata, and
//! [`run`] turns the type into a binary. Use `service` for ordinary robot
//! participants, `driver` for a participant launched once per
//! `robot.components` entry, `tool` for host-side utilities, and `simulator`
//! for simulation-only participants.
//!
//! ## Author a participant
//!
//! A participant is a `Config` struct, an `Api` struct of typed bus handles, a
//! state struct, and one annotated inherent `impl`. This is the whole
//! getting-started surface:
//!
//! ```ignore
//! use phoxal_api::v1;
//! use phoxal::prelude::*;
//!
//! #[derive(serde::Deserialize, phoxal::Config)]
//! struct Config {}
//!
//! #[derive(phoxal::Api)]
//! struct Api {
//! state: Latest<v1::drive::State>, // keep-last view of the drive state
//! target: Publisher<v1::drive::Target>, // commanded drive target
//! }
//!
//! #[phoxal::service(id = "avoid-obstacles")]
//! struct AvoidObstacles;
//!
//! #[phoxal::behavior]
//! impl AvoidObstacles {
//! #[setup]
//! async fn setup(ctx: &mut SetupContext<Self>, _config: Self::Config) -> Result<(Self, Self::Api)> {
//! Ok((Self, Self::Api {
//! state: ctx.latest(v1::topic::new().drive().state()).await?,
//! target: ctx.publisher(v1::topic::new().drive().target()).await?,
//! }))
//! }
//!
//! #[step(hz = 50)]
//! async fn step(&mut self, api: &mut Self::Api, step: StepContext) -> Result<()> {
//! let now = step.time();
//! api.target.publish_at(now, v1::drive::Target {
//! linear_x_mps: 0.2,
//! angular_z_radps: 0.0,
//! curvature_limit_radpm: None,
//! }).await?;
//! Ok(())
//! }
//! }
//!
//! fn main() -> phoxal::Result<()> { phoxal::run::<AvoidObstacles>() }
//! ```
//!
//! What each piece does:
//!
//! - `use phoxal_api::v1;` brings the versioned API module into scope;
//! `Api` struct fields name version-qualified bodies (`v1::drive::Target`)
//! directly, so a participant may mix versions across fields with no
//! version-ceiling attribute to keep in sync.
//! - `#[derive(phoxal::Api)]` derives the bus-facing contract surface from the
//! `Api` struct's handle fields ([`Publisher<T>`](bus::Publisher),
//! [`Latest<T>`](bus::Latest), [`Subscriber<T>`](bus::Subscriber),
//! [`Querier<Req, Resp>`](bus::Querier), `Server<Req, Resp>`).
//! - `#[phoxal::service(id = "…")]` links the participant state struct to its
//! `Config`/`Api` types and records its identity.
//! - All handles are built in `#[setup]` from api-local topic builders
//! (`v1::topic::new().drive().state()`) and returned as the `Api` value
//! alongside the participant state.
//! - `#[step(hz = ...)]` is the scheduled control loop; the runner owns timing and
//! delivers logical time via [`StepContext`](participant::StepContext), and
//! `&mut Self::Api` alongside `&mut self`. Query servers use `#[server]` /
//! `#[server_snapshot]`, and `#[shutdown]` runs graceful cleanup before the bus
//! closes.
//! - `fn main() -> phoxal::Result<()> { phoxal::run::<R>() }` is the default
//! blocking entrypoint. For a custom Tokio main, call
//! [`phoxal::tokio::run::<R>().await`](tokio::run).
//!
//! The four authoring kinds share the same metadata path but describe different
//! runtime roles:
//!
//! - [`macro@service`] is the ordinary typed participant surface.
//! - [`macro@driver`] is launched once per `robot.components` entry. Only a
//! driver can call
//! [`SetupContextDriverExt::component`](participant::SetupContextDriverExt::component)
//! to read the bound component instance.
//! - [`macro@tool`] is for host-side utilities that inspect the robot model
//! through
//! [`SetupContextApiExt::robot`](participant::SetupContextApiExt::robot).
//! Privileged raw-bus access lives under [`raw`] so it is never part of the
//! default checked participant surface.
//! - [`macro@simulator`] is a normal participant for simulation-only processes.
//! It carries a distinct kind and marker for simulation clock ownership.
//!
//! Worked examples live in `phoxal/examples/`.
//!
//! ## Where to look next
//!
//! - The `phoxal-api` crate (`phoxal_api::v1`, …) - the versioned API
//! modules: version-local wire bodies, the [`ApiVersion`](bus::ApiVersion) /
//! [`ContractBody`](bus::ContractBody) traits, and the api-local topic builders,
//! all generated by [`phoxal_api_tree!`](macro@phoxal_macros::phoxal_api_tree).
//! A participant imports it directly with `use phoxal_api::v1 as api;`.
//! The runner also links it for framework-owned out-of-band infrastructure
//! contracts such as bus logs.
//! - [`prelude`] - everything a participant author imports with
//! `use phoxal::prelude::*;`: the handle types, [`SetupContext`](participant::SetupContext) /
//! [`StepContext`](participant::StepContext), and [`Result`].
//! - [`mod@participant`] - the authoring surface behind the macros: the static metadata
//! traits, the contexts, the clock and scheduler, and the runner
//! ([`run`] / [`tokio::run`]).
//! - [`bus`] - the typed contract vocabulary normal participants need: the
//! key scheme, MessagePack codec, [`BusMetadata`](bus::BusMetadata) attachment,
//! body-typed handles, and side-branded [`Topic`](bus::Topic) values.
//! - [`raw`] - the explicit privileged/tooling surface for opening a raw bus,
//! accessing the underlying session, or embedding runtimes on a caller-owned
//! bus.
//! - [`model`] - the authored manifest schemas (`robot.yaml`, `structure.urdf`,
//! `component.yaml`, …) that participants and the CLI parse.
//! - The **official service set** ships alongside this crate in the workspace
//! `service/` tree (`drive`, `localize`, `map`, `safety`, …): full platform
//! participants authored on exactly this surface, useful as reference reading.
// Generated macro output refers to the framework as `::phoxal::…`; make that path
// resolve to this crate so the engine's participant derives and
// `#[phoxal::behavior]` macro work when invoked inside the engine (e.g. the
// crate's own tests), the
// same as in downstream service crates. Only the in-crate test build units expand
// macros to `::phoxal::…`, so the alias is needed only under `cfg(test)`; gating
// it there keeps the non-test build free of an unused `extern crate` (no need for
// an `allow(unused_extern_crates)`).
extern crate self as phoxal;
/// Typed contract and handle vocabulary for normal participant authoring.
///
/// This module deliberately excludes the raw session-owning bus types
/// (`Bus`, `BusConfig`, `BusHealth`, `IncomingQuery`, `ServerQueryable`). Checked
/// participants build IO through [`participant::SetupContext`] and the api-local
/// topic builders; privileged tools, bridges, and framework tests that need raw
/// access use [`raw`] instead.
/// Explicit raw/permissive bus surface for privileged participants, tooling,
/// bridges, and framework tests.
///
/// Importing this module is the conscious opt-in. The ordinary
/// `phoxal::prelude::*` and [`bus`] module do not expose raw session/open APIs.
/// `Tool` participants are emitted as `participant_class = "privileged"`; the
/// graph checker still includes their contracts, but never lets their raw
/// access satisfy checked topology.
/// The framework result type (`anyhow`-backed). Authoring code uses bare
/// `Result<T>` via the [`prelude`].
pub use Result;
/// The bare `#[phoxal::behavior]` attribute for a participant's inherent impl.
pub use behavior;
/// Derive the bus-facing contract surface from an `Api` handle struct's
/// fields. See `phoxal::participant::api`.
pub use Api;
/// Derive participant config identity from a `Config` struct (schema
/// materialization is a later slice - see
/// `phoxal::participant::api::ParticipantConfig`).
pub use Config;
/// Link a participant state struct to its `Config`/`Api` types as a checked
/// service.
pub use service;
/// Link a participant state struct to its `Config`/`Api` types as a
/// component driver.
pub use driver;
/// Link a participant state struct to its `Config`/`Api` types as a
/// simulation participant.
pub use simulator;
/// Link a participant state struct to its `Config` as a raw-bus tool (`Api`
/// defaults to `()` - tools stay raw-bus only).
pub use tool;
pub use phoxal_api_tree;
/// Run a participant (`#[phoxal::service|driver|simulator|tool]` +
/// `#[phoxal::behavior]`) to completion on a framework-owned blocking Tokio
/// runtime.
///
/// This is the default binary entrypoint:
/// `fn main() -> phoxal::Result<()> { phoxal::run::<Participant>() }`.
pub use run;
/// Async host runner entrypoint for custom Tokio mains
/// (`phoxal::tokio::run::<Participant>().await`).
/// Everything a participant author imports with `use phoxal::prelude::*;`.