Skip to main content

icedwright_protocol/
lib.rs

1//! Wire types for icedwright.
2//!
3//! - [`raw`]: the raw tree a live app records, which `icedwright_semantics::infer` turns into a
4//!   semantic tree. The harness and the remote endpoint produce the same values.
5//! - The typed remote protocol: one type per request, each tied to its response type and
6//!   method name through [`Request`], the handshake ([`Hello`], [`Welcome`], [`PROTOCOL`] and
7//!   [`accepts`]), [`Notification`]s, [`WireEvent`] and the framing in [`WireFrame`].
8//!
9//! Geometry and roles come from `icedwright-types`. Nothing here depends on a JSON library:
10//! both ends encode with `serde_json`, and values only the app can rebuild travel as JSON text.
11
12#![forbid(unsafe_code)]
13#![deny(missing_docs)]
14#![warn(clippy::must_use_candidate, clippy::return_self_not_must_use)]
15
16mod error;
17mod event;
18mod frame;
19mod ids;
20pub mod raw;
21mod request;
22mod version;
23
24pub use error::{ErrorBody, ErrorCode, ProtocolError};
25pub use event::{
26    WireButton, WireEvent, WireKey, WireLocation, WireModifiers, WirePhysicalKey, WireScrollDelta,
27    WireStatus,
28};
29pub use frame::{Attachment, FrameKind, HEADER_LEN, MAX_FRAME_BYTES, WireFrame};
30pub use ids::{
31    FrameCount, Generation, RequestId, WindowOrdinal, WireDuration, WireRuleId, WireTaskId,
32};
33pub use raw::{
34    AnnotationSource, RawAnnotation, RawCall, RawKey, RawNode, RawTree, RawValue, RawWindow,
35    RawWindowInfo, WidgetId, WindowId,
36};
37pub use request::{
38    AnyRequest, AnyResponse, Bye, Call, CursorReading, Delivered, Done, Drive, Focus, Guard, Hello,
39    HoldTask, Input, Inspect, METHODS, NextTask, Notification, Record, Release, ReleaseTask,
40    RemoveRule, Reply, ReplyBody, Request, ResolveTask, Screenshot, ScreenshotHeader, Snapshot,
41    SnapshotReply, TaskChange, TaskOutput, WaitIdle, Welcome, WindowList, Windows,
42};
43pub use version::{PROTOCOL, VersionMismatch, accepts};
44
45#[doc(hidden)]
46pub mod __private {
47    /// Whether every name in `methods` is distinct. Run in a `const` assertion over the
48    /// request list, so two requests with one method name fail to compile.
49    #[must_use]
50    pub const fn methods_unique(methods: &[&str]) -> bool {
51        let mut i = 0;
52        while i < methods.len() {
53            let mut j = i + 1;
54            while j < methods.len() {
55                if str_eq(methods[i], methods[j]) {
56                    return false;
57                }
58                j += 1;
59            }
60            i += 1;
61        }
62        true
63    }
64
65    const fn str_eq(a: &str, b: &str) -> bool {
66        let (a, b) = (a.as_bytes(), b.as_bytes());
67        if a.len() != b.len() {
68            return false;
69        }
70        let mut i = 0;
71        while i < a.len() {
72            if a[i] != b[i] {
73                return false;
74            }
75            i += 1;
76        }
77        true
78    }
79}