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
//! The nomux extension SDK for the daemon part of an extension.
//!
//! The daemon part of an extension is a separate process on the Host. The
//! nomux daemon (the host) starts the process when a client needs it and
//! stops it when it is idle. The host and the extension exchange protobuf
//! messages on the standard input and the standard output of the process.
//!
//! An extension does these steps:
//!
//! 1. It makes an [`Extension`] with the identifier and the version of its
//! `extension.toml`.
//! 2. It adds each method with [`Extension::method`]. The argument and the
//! result of a method are protobuf messages.
//! 3. It calls [`Extension::run`]. The function does the handshake, runs the
//! methods that the host calls and returns when the host tells the
//! extension to stop.
//!
//! In a method, [`Call::host`] gives the [`Host`]. With it, the extension
//! emits events ([`Host::emit`]), calls host services ([`Host::call`]) and
//! reads the permissions that the user approved ([`Host::permissions`]).
//!
//! # Example
//!
//! An extension with one method and one event. Add `prost` to the
//! dependencies of the extension, with the same version as this crate uses:
//! the `prost::Message` derive macro needs it.
//!
//! ```no_run
//! use nomux_extension::{Call, CallError, Extension, RunError};
//!
//! /// The argument and the result of the `echo` method.
//! #[derive(Clone, PartialEq, prost::Message)]
//! struct Text {
//! #[prost(string, tag = "1")]
//! text: String,
//! }
//!
//! /// The payload of the `echoed` event.
//! #[derive(Clone, PartialEq, prost::Message)]
//! struct Echoed {
//! #[prost(uint64, tag = "1")]
//! length: u64,
//! }
//!
//! fn echo(call: &Call, input: Text) -> Result<Text, CallError> {
//! if input.text.is_empty() {
//! return Err(CallError::failed("the text is empty"));
//! }
//! let length = input.text.len() as u64;
//! call.host().emit("echoed", &Echoed { length })?;
//! Ok(input)
//! }
//!
//! fn main() -> Result<(), RunError> {
//! Extension::new("nomux.echo", env!("CARGO_PKG_VERSION"))
//! .method("echo", echo)
//! .run()
//! }
//! ```
//!
//! # Threads and the async runtime
//!
//! The crate has no async runtime. An extension starts when a client needs
//! it and stops when it is idle, thus it must be small and start fast.
//! [`Extension::run`] blocks the thread that calls it and runs the methods on
//! that thread, one at a time. Thus a method does not need a lock for the
//! state of the extension.
//!
//! One more thread reads the messages from the host. While a method runs,
//! that thread answers health requests and records cancellations. A method
//! that can run for a long time examines [`Call::is_cancelled`] between its
//! steps.
//!
//! # Rules for an extension
//!
//! - The standard output carries the protocol. Do not print to it. Write
//! logs to the standard error.
//! - A panic in a method stops the process. The nomux daemon continues to
//! run and reports the failure of the extension.
//!
//! # Other parts of the crate
//!
//! - [`wire::v1`]: the messages of the protocol. `prost` generates these
//! types from the schemas of the `extension-api` repository.
//! - [`frame`]: the framing. A host uses it to speak with an extension.
//! - `manifest` (feature `manifest`): the types and the validator of
//! `extension.toml`. An extension does not need it.
//!
//! Versions earlier than 1.0.0 are pre-releases: interfaces can change
//! between minor versions.
pub use ;
pub use Call;
pub use ;
pub use Extension;
pub use Host;
pub use ;
/// Compiles the example in the crate README as a documentation test.
;
/// The messages of the nomux extension protocol.