nomux_extension/lib.rs
1//! The nomux extension SDK for the daemon part of an extension.
2//!
3//! The daemon part of an extension is a separate process on the Host. The
4//! nomux daemon (the host) starts the process when a client needs it and
5//! stops it when it is idle. The host and the extension exchange protobuf
6//! messages on the standard input and the standard output of the process.
7//!
8//! An extension does these steps:
9//!
10//! 1. It makes an [`Extension`] with the identifier and the version of its
11//! `extension.toml`.
12//! 2. It adds each method with [`Extension::method`]. The argument and the
13//! result of a method are protobuf messages.
14//! 3. It calls [`Extension::run`]. The function does the handshake, runs the
15//! methods that the host calls and returns when the host tells the
16//! extension to stop.
17//!
18//! In a method, [`Call::host`] gives the [`Host`]. With it, the extension
19//! emits events ([`Host::emit`]), calls host services ([`Host::call`]) and
20//! reads the permissions that the user approved ([`Host::permissions`]).
21//!
22//! # Example
23//!
24//! An extension with one method and one event. Add `prost` to the
25//! dependencies of the extension, with the same version as this crate uses:
26//! the `prost::Message` derive macro needs it.
27//!
28//! ```no_run
29//! use nomux_extension::{Call, CallError, Extension, RunError};
30//!
31//! /// The argument and the result of the `echo` method.
32//! #[derive(Clone, PartialEq, prost::Message)]
33//! struct Text {
34//! #[prost(string, tag = "1")]
35//! text: String,
36//! }
37//!
38//! /// The payload of the `echoed` event.
39//! #[derive(Clone, PartialEq, prost::Message)]
40//! struct Echoed {
41//! #[prost(uint64, tag = "1")]
42//! length: u64,
43//! }
44//!
45//! fn echo(call: &Call, input: Text) -> Result<Text, CallError> {
46//! if input.text.is_empty() {
47//! return Err(CallError::failed("the text is empty"));
48//! }
49//! let length = input.text.len() as u64;
50//! call.host().emit("echoed", &Echoed { length })?;
51//! Ok(input)
52//! }
53//!
54//! fn main() -> Result<(), RunError> {
55//! Extension::new("nomux.echo", env!("CARGO_PKG_VERSION"))
56//! .method("echo", echo)
57//! .run()
58//! }
59//! ```
60//!
61//! # Threads and the async runtime
62//!
63//! The crate has no async runtime. An extension starts when a client needs
64//! it and stops when it is idle, thus it must be small and start fast.
65//! [`Extension::run`] blocks the thread that calls it and runs the methods on
66//! that thread, one at a time. Thus a method does not need a lock for the
67//! state of the extension.
68//!
69//! One more thread reads the messages from the host. While a method runs,
70//! that thread answers health requests and records cancellations. A method
71//! that can run for a long time examines [`Call::is_cancelled`] between its
72//! steps.
73//!
74//! # Rules for an extension
75//!
76//! - The standard output carries the protocol. Do not print to it. Write
77//! logs to the standard error.
78//! - A panic in a method stops the process. The nomux daemon continues to
79//! run and reports the failure of the extension.
80//!
81//! # Other parts of the crate
82//!
83//! - [`wire::v1`]: the messages of the protocol. `prost` generates these
84//! types from the schemas of the `extension-api` repository.
85//! - [`frame`]: the framing. A host uses it to speak with an extension.
86//! - `manifest` (feature `manifest`): the types and the validator of
87//! `extension.toml`. An extension does not need it.
88//!
89//! Versions earlier than 1.0.0 are pre-releases: interfaces can change
90//! between minor versions.
91
92mod api_version;
93mod call;
94mod cancellation;
95mod error;
96mod extension;
97pub mod frame;
98mod handshake;
99mod host;
100#[cfg(feature = "manifest")]
101pub mod manifest;
102mod outbound;
103mod pending_calls;
104mod reader;
105
106pub use api_version::{ApiRequirement, ApiVersion, InvalidApiRequirement};
107pub use call::Call;
108pub use error::{CallError, RunError};
109pub use extension::Extension;
110pub use host::Host;
111pub use wire::v1::{ErrorCode, Permissions};
112
113/// Compiles the example in the crate README as a documentation test.
114#[cfg(doctest)]
115#[doc = include_str!("../README.md")]
116struct ReadmeDoctests;
117
118/// The messages of the nomux extension protocol.
119pub mod wire {
120 /// Package `nomux.extension.v1`.
121 ///
122 /// `buf generate` writes the included file. Do not edit the file. The
123 /// file is included with `include!`, thus `rustfmt` does not format it.
124 pub mod v1 {
125 include!("generated/nomux.extension.v1.rs");
126 }
127}