rtci-host 0.2.0

The host I/O around the sans-I/O rtci WebRTC engine: sockets, timers, STUN/TURN clients, ICE-TCP, mDNS and media, behind a permission gate and a socket factory
Documentation
// Copyright 2026 Brainwires. MIT OR Apache-2.0.

//! The crate's error: a W3C / Web IDL exception kind and its message. Each
//! front end maps [`ErrorKind`] to its own error classes (a `JsError` in the
//! built-in extension, integer codes over FFI).

use std::fmt;

/// Which exception a failure is, as the W3C WebRTC specs name them.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorKind {
  /// A `TypeError`.
  Type,
  /// A `DOMException` named `SyntaxError`.
  Syntax,
  /// A `DOMException` named `InvalidAccessError`.
  InvalidAccess,
  /// A `DOMException` named `InvalidStateError`.
  InvalidState,
  /// A `DOMException` named `OperationError`.
  Operation,
  /// A `DOMException` named `InvalidModificationError`.
  InvalidModification,
  /// A `DOMException` named `NotSupportedError`.
  NotSupported,
  /// The [`NetGate`](crate::NetGate) refused an address (a permission
  /// denial: Deno's `NotCapable`).
  NotCapable,
  /// A failure the front end should report as an internal error (no W3C
  /// class fits).
  Internal,
}

impl ErrorKind {
  /// A stable name for the kind (`"TypeError"`, `"InvalidStateError"`, ...),
  /// the `DOMException` name where there is one.
  pub fn name(self) -> &'static str {
    match self {
      ErrorKind::Type => "TypeError",
      ErrorKind::Syntax => "SyntaxError",
      ErrorKind::InvalidAccess => "InvalidAccessError",
      ErrorKind::InvalidState => "InvalidStateError",
      ErrorKind::Operation => "OperationError",
      ErrorKind::InvalidModification => "InvalidModificationError",
      ErrorKind::NotSupported => "NotSupportedError",
      ErrorKind::NotCapable => "NotCapable",
      ErrorKind::Internal => "Error",
    }
  }
}

/// A failure of an `rtci-host` call: its kind and message.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Error {
  pub kind: ErrorKind,
  pub message: String,
}

impl Error {
  pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
    Self {
      kind,
      message: message.into(),
    }
  }

  /// A [`ErrorKind::NotCapable`] error: the gate refused (the message is the
  /// gate's).
  pub fn not_capable(message: impl Into<String>) -> Self {
    Self::new(ErrorKind::NotCapable, message)
  }

  pub fn kind(&self) -> ErrorKind {
    self.kind
  }

  pub fn message(&self) -> &str {
    &self.message
  }
}

impl fmt::Display for Error {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.write_str(&self.message)
  }
}

impl std::error::Error for Error {}

/// `{"kind": <ErrorKind::name>, "message": ...}` (the `error` of an
/// `opdone` event).
impl serde::Serialize for Error {
  fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
    use serde::ser::SerializeStruct;
    let mut st = s.serialize_struct("Error", 2)?;
    st.serialize_field("kind", self.kind.name())?;
    st.serialize_field("message", &self.message)?;
    st.end()
  }
}

/// `Result` with this crate's [`Error`].
pub type Result<T, E = Error> = std::result::Result<T, E>;