openrtc 1.0.2

OpenRTC: a Rust-first P2P runtime for device discovery, signaling, and iroh/QUIC networking.
Documentation
//! Native MoQ route-promotion policy.
//!
//! This module is intentionally side-effect free. The native transport upgrade
//! implementation owns relay sessions and async I/O; this file owns the small
//! decision table that keeps MoQ from becoming a peer lifecycle authority.

#[cfg(all(not(target_arch = "wasm32"), feature = "transport-moq"))]
use crate::transport::NativeMoQState;

#[cfg(all(not(target_arch = "wasm32"), feature = "transport-moq"))]
pub(crate) const NATIVE_MOQ_TRANSPORT_LABEL: &str = "moq";

#[cfg(all(not(target_arch = "wasm32"), feature = "transport-moq"))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct NativeMoQStatusReport<'a> {
    pub(crate) active_transport: &'a str,
    pub(crate) parallel_transport: Option<&'a str>,
}

#[cfg(all(not(target_arch = "wasm32"), feature = "transport-moq"))]
pub(crate) fn setup_status_report(base_transport: &str) -> NativeMoQStatusReport<'_> {
    NativeMoQStatusReport {
        active_transport: base_transport,
        parallel_transport: Some(NATIVE_MOQ_TRANSPORT_LABEL),
    }
}

#[cfg(all(not(target_arch = "wasm32"), feature = "transport-moq"))]
pub(crate) fn successful_send_status_report(parallel_transport: &str) -> NativeMoQStatusReport<'_> {
    NativeMoQStatusReport {
        active_transport: NATIVE_MOQ_TRANSPORT_LABEL,
        parallel_transport: Some(parallel_transport),
    }
}

#[cfg(all(not(target_arch = "wasm32"), feature = "transport-moq"))]
pub(crate) fn should_preserve_active_moq_on_iroh_send(
    active_transport: &str,
    moq_data_ready: bool,
) -> bool {
    moq_data_ready
        && crate::transport_label::normalize(active_transport) == Some(crate::transport_label::MOQ)
}

#[cfg(all(not(target_arch = "wasm32"), feature = "transport-moq"))]
pub(crate) fn is_data_ready(state: NativeMoQState, peer_data_subscribed: bool) -> bool {
    state == NativeMoQState::Connected && peer_data_subscribed
}

#[cfg(all(not(target_arch = "wasm32"), feature = "transport-moq"))]
pub(crate) fn is_repairable_state(state: NativeMoQState) -> bool {
    matches!(
        state,
        NativeMoQState::Idle | NativeMoQState::Failed | NativeMoQState::Closed
    )
}

#[cfg(all(not(target_arch = "wasm32"), feature = "transport-moq"))]
pub(crate) fn should_close_before_restart(state: NativeMoQState) -> bool {
    matches!(state, NativeMoQState::Failed | NativeMoQState::Closed)
}

#[cfg(all(not(target_arch = "wasm32"), feature = "transport-moq"))]
pub(crate) fn should_reuse_existing_setup(state: NativeMoQState) -> bool {
    !should_close_before_restart(state)
}

#[cfg(test)]
#[cfg(all(not(target_arch = "wasm32"), feature = "transport-moq"))]
mod tests {
    use super::*;

    #[test]
    fn setup_reports_moq_as_parallel_only() {
        let report = setup_status_report("iroh-relay");

        assert_eq!(report.active_transport, "iroh-relay");
        assert_eq!(report.parallel_transport, Some("moq"));
    }

    #[test]
    fn successful_send_is_the_promotion_boundary() {
        let report = successful_send_status_report("iroh-relay");

        assert_eq!(report.active_transport, "moq");
        assert_eq!(report.parallel_transport, Some("iroh-relay"));
    }

    #[test]
    fn direct_iroh_send_preserves_only_data_ready_active_moq() {
        assert!(should_preserve_active_moq_on_iroh_send("moq", true));
        assert!(should_preserve_active_moq_on_iroh_send(" MOQ ", true));
        assert!(!should_preserve_active_moq_on_iroh_send("moq", false));
        assert!(!should_preserve_active_moq_on_iroh_send("iroh-lan", true));
        assert!(!should_preserve_active_moq_on_iroh_send("webrtc", true));
    }

    #[test]
    fn data_ready_requires_connected_session_and_peer_subscription() {
        assert!(is_data_ready(NativeMoQState::Connected, true));
        assert!(!is_data_ready(NativeMoQState::Connected, false));
        assert!(!is_data_ready(NativeMoQState::Connecting, true));
        assert!(!is_data_ready(NativeMoQState::Failed, true));
    }

    #[test]
    fn repair_and_reuse_states_are_explicit() {
        assert!(is_repairable_state(NativeMoQState::Idle));
        assert!(is_repairable_state(NativeMoQState::Failed));
        assert!(is_repairable_state(NativeMoQState::Closed));
        assert!(!is_repairable_state(NativeMoQState::Connecting));
        assert!(!is_repairable_state(NativeMoQState::Connected));

        assert!(should_close_before_restart(NativeMoQState::Failed));
        assert!(should_close_before_restart(NativeMoQState::Closed));
        assert!(!should_close_before_restart(NativeMoQState::Idle));

        assert!(should_reuse_existing_setup(NativeMoQState::Idle));
        assert!(should_reuse_existing_setup(NativeMoQState::Connecting));
        assert!(should_reuse_existing_setup(NativeMoQState::Connected));
        assert!(!should_reuse_existing_setup(NativeMoQState::Failed));
        assert!(!should_reuse_existing_setup(NativeMoQState::Closed));
    }
}