icedwright-protocol 0.1.0

Wire types for icedwright: the raw semantic tree a live app records and the typed remote protocol
Documentation
//! Wire types for icedwright.
//!
//! - [`raw`]: the raw tree a live app records, which `icedwright_semantics::infer` turns into a
//!   semantic tree. The harness and the remote endpoint produce the same values.
//! - The typed remote protocol: one type per request, each tied to its response type and
//!   method name through [`Request`], the handshake ([`Hello`], [`Welcome`], [`PROTOCOL`] and
//!   [`accepts`]), [`Notification`]s, [`WireEvent`] and the framing in [`WireFrame`].
//!
//! Geometry and roles come from `icedwright-types`. Nothing here depends on a JSON library:
//! both ends encode with `serde_json`, and values only the app can rebuild travel as JSON text.

#![forbid(unsafe_code)]
#![deny(missing_docs)]
#![warn(clippy::must_use_candidate, clippy::return_self_not_must_use)]

mod error;
mod event;
mod frame;
mod ids;
pub mod raw;
mod request;
mod version;

pub use error::{ErrorBody, ErrorCode, ProtocolError};
pub use event::{
    WireButton, WireEvent, WireKey, WireLocation, WireModifiers, WirePhysicalKey, WireScrollDelta,
    WireStatus,
};
pub use frame::{Attachment, FrameKind, HEADER_LEN, MAX_FRAME_BYTES, WireFrame};
pub use ids::{
    FrameCount, Generation, RequestId, WindowOrdinal, WireDuration, WireRuleId, WireTaskId,
};
pub use raw::{
    AnnotationSource, RawAnnotation, RawCall, RawKey, RawNode, RawTree, RawValue, RawWindow,
    RawWindowInfo, WidgetId, WindowId,
};
pub use request::{
    AnyRequest, AnyResponse, Bye, Call, CursorReading, Delivered, Done, Drive, Focus, Guard, Hello,
    HoldTask, Input, Inspect, METHODS, NextTask, Notification, Record, Release, ReleaseTask,
    RemoveRule, Reply, ReplyBody, Request, ResolveTask, Screenshot, ScreenshotHeader, Snapshot,
    SnapshotReply, TaskChange, TaskOutput, WaitIdle, Welcome, WindowList, Windows,
};
pub use version::{PROTOCOL, VersionMismatch, accepts};

#[doc(hidden)]
pub mod __private {
    /// Whether every name in `methods` is distinct. Run in a `const` assertion over the
    /// request list, so two requests with one method name fail to compile.
    #[must_use]
    pub const fn methods_unique(methods: &[&str]) -> bool {
        let mut i = 0;
        while i < methods.len() {
            let mut j = i + 1;
            while j < methods.len() {
                if str_eq(methods[i], methods[j]) {
                    return false;
                }
                j += 1;
            }
            i += 1;
        }
        true
    }

    const fn str_eq(a: &str, b: &str) -> bool {
        let (a, b) = (a.as_bytes(), b.as_bytes());
        if a.len() != b.len() {
            return false;
        }
        let mut i = 0;
        while i < a.len() {
            if a[i] != b[i] {
                return false;
            }
            i += 1;
        }
        true
    }
}