openrtc 2.8.0

OpenRTC: a Rust-first P2P runtime for device discovery, signaling, and iroh/QUIC networking.
Documentation
//! Canonical transport label taxonomy.
//!
//! Peer snapshots, carrier routing, and diagnostics exchange labels as strings
//! across Rust, WASM, TS, and IPC. Keep normalization
//! and classification in one target-neutral place so cross-platform behavior
//! cannot drift one call site at a time.

use crate::route_policy::{KnownRoute, RouteLocality};

pub(crate) const IROH: &str = KnownRoute::Iroh.as_str();
pub(crate) const IROH_QUIC: &str = KnownRoute::IrohQuic.as_str();
pub(crate) const IROH_RELAY: &str = KnownRoute::IrohRelay.as_str();
pub(crate) const IROH_LAN: &str = KnownRoute::IrohLan.as_str();
pub(crate) const BLE: &str = KnownRoute::Ble.as_str();
pub(crate) const WEBRTC: &str = KnownRoute::WebRtc.as_str();
pub(crate) const MOQ: &str = KnownRoute::Moq.as_str();

pub(crate) fn normalize(value: &str) -> Option<&'static str> {
    crate::route_policy::normalize_route(value).map(KnownRoute::as_str)
}

#[allow(dead_code)]
pub(crate) fn is_iroh_base(value: &str) -> bool {
    crate::route_policy::normalize_route(value)
        .map(|route| route.descriptor().base_protocol == IROH)
        .unwrap_or(false)
}

#[allow(dead_code)]
pub(crate) fn is_direct_iroh(value: &str) -> bool {
    crate::route_policy::normalize_route(value)
        .map(|route| {
            let descriptor = route.descriptor();
            descriptor.base_protocol == IROH
                && matches!(
                    descriptor.locality,
                    RouteLocality::Nearby | RouteLocality::DirectInternet
                )
        })
        .unwrap_or(false)
}

#[allow(dead_code)]
pub(crate) fn is_iroh_packet_carrier(value: &str) -> bool {
    matches!(
        crate::route_policy::normalize_route(value),
        Some(KnownRoute::WebRtc | KnownRoute::Moq)
    )
}

/// Routes whose proof and projection belong to one exact physical Iroh
/// generation. BLE does not use the WebRTC/MoQ packet-carrier implementation,
/// but it shares the same owner-level proof, commit, and generation fencing.
#[allow(dead_code)]
pub(crate) fn is_generation_bound_iroh_carrier(value: &str) -> bool {
    matches!(
        crate::route_policy::normalize_route(value),
        Some(KnownRoute::Ble | KnownRoute::WebRtc | KnownRoute::Moq)
    )
}

#[allow(dead_code)]
pub(crate) fn is_webrtc(value: &str) -> bool {
    crate::route_policy::normalize_route(value)
        .map(|route| route == KnownRoute::WebRtc)
        .unwrap_or(false)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(serde::Deserialize)]
    #[serde(rename_all = "camelCase")]
    struct TransportPolicyVectors {
        labels: Vec<LabelVector>,
    }

    #[derive(serde::Deserialize)]
    #[serde(rename_all = "camelCase")]
    struct LabelVector {
        input: String,
        normalized: Option<String>,
        iroh_base: bool,
        direct_iroh: bool,
        web_rtc: bool,
    }

    #[test]
    fn normalizes_official_labels_case_insensitively() {
        for (input, expected) in [
            ("iroh", IROH),
            (" IROH-QUIC ", IROH_QUIC),
            ("iroh-relay", IROH_RELAY),
            ("iroh-lan", IROH_LAN),
            ("ble", BLE),
            ("webrtc", WEBRTC),
            ("moq", MOQ),
        ] {
            assert_eq!(normalize(input), Some(expected));
        }
        assert_eq!(normalize("lan"), None);
        assert_eq!(normalize("webtransport"), None);
        assert_eq!(normalize("webrtc-lan"), None);
        assert_eq!(normalize("webrtc-turn"), None);
    }

    #[test]
    fn classifies_transport_families() {
        assert!(is_iroh_base(IROH));
        assert!(is_iroh_base(IROH_QUIC));
        assert!(is_iroh_base(IROH_LAN));
        assert!(is_iroh_base(IROH_RELAY));
        assert!(is_iroh_base(BLE));
        assert!(is_iroh_base(WEBRTC));
        assert!(is_iroh_base(MOQ));

        assert!(is_direct_iroh(IROH_QUIC));
        assert!(is_direct_iroh(IROH_LAN));
        assert!(is_direct_iroh(BLE));
        assert!(!is_direct_iroh(IROH_RELAY));

        assert!(is_iroh_packet_carrier(WEBRTC));
        assert!(is_iroh_packet_carrier(MOQ));
        assert!(!is_iroh_packet_carrier(BLE));
        assert!(!is_iroh_packet_carrier(IROH_RELAY));

        assert!(is_generation_bound_iroh_carrier(BLE));
        assert!(is_generation_bound_iroh_carrier(WEBRTC));
        assert!(is_generation_bound_iroh_carrier(MOQ));
        assert!(!is_generation_bound_iroh_carrier(IROH_RELAY));

        assert!(is_webrtc(WEBRTC));
        assert!(!is_webrtc(MOQ));
    }

    #[test]
    fn matches_shared_transport_policy_vectors() {
        let vectors: TransportPolicyVectors =
            serde_json::from_str(crate::generated::route_registry::TRANSPORT_REGISTRY_JSON)
                .expect("transport policy vectors");
        for vector in vectors.labels {
            assert_eq!(
                normalize(&vector.input),
                vector.normalized.as_deref(),
                "normalize({:?})",
                vector.input
            );
            assert_eq!(
                is_iroh_base(&vector.input),
                vector.iroh_base,
                "is_iroh_base({:?})",
                vector.input
            );
            assert_eq!(
                is_direct_iroh(&vector.input),
                vector.direct_iroh,
                "is_direct_iroh({:?})",
                vector.input
            );
            assert_eq!(
                is_webrtc(&vector.input),
                vector.web_rtc,
                "is_webrtc({:?})",
                vector.input
            );
        }
    }
}