tx5_core/
lib.rs

1#![deny(missing_docs)]
2#![deny(unsafe_code)]
3#![doc = include_str!("README.tpl")]
4//! # tx5-core
5//!
6//! Holochain WebRTC p2p communication ecosystem core types.
7
8include!(concat!(env!("OUT_DIR"), "/readme.rs"));
9
10/// Re-exported dependencies.
11pub mod deps {
12    pub use base64;
13    pub use serde;
14    pub use serde_json;
15}
16
17mod error;
18pub use error::*;
19
20mod evt;
21pub use evt::*;
22
23#[cfg(feature = "file_check")]
24pub mod file_check;
25
26/// Pinned, boxed, future type alias.
27pub type BoxFut<'lt, T> =
28    std::pin::Pin<Box<dyn std::future::Future<Output = T> + 'lt + Send>>;
29
30/// Initial configuration. If you would like to change this from the
31/// default, please call [Tx5InitConfig::set_as_global_default]
32/// before creating any peer connections.
33#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)]
34#[serde(crate = "deps::serde", rename_all = "camelCase")]
35pub struct Tx5InitConfig {
36    /// If true, tracing logs from the backend webrtc library will be included.
37    /// Defaults to `false`.
38    pub tracing_enabled: bool,
39
40    /// The minimum ephemeral udp port to bind. Defaults to `1`.
41    pub ephemeral_udp_port_min: u16,
42
43    /// The maximum ephemeral udp port to bind. Defaults to `65535`.
44    pub ephemeral_udp_port_max: u16,
45}
46
47impl Default for Tx5InitConfig {
48    fn default() -> Self {
49        Self {
50            tracing_enabled: false,
51            ephemeral_udp_port_min: 1,
52            ephemeral_udp_port_max: 65535,
53        }
54    }
55}
56
57impl Tx5InitConfig {
58    /// Call this to set tx5_init defaults before creating any peer connections.
59    /// This will return an error if the settings have already been set.
60    pub fn set_as_global_default(&self) -> Result<()> {
61        TX5_INIT_CONFIG
62            .set(*self)
63            .map_err(|_| Error::id("Tx5InitAlreadySet"))
64    }
65
66    /// Get the currently set Tx5InitConfig. WARNING! If it hasn't been
67    /// explicitly set, this get will trigger the config to be set
68    /// to default values.
69    pub fn get() -> Self {
70        *TX5_INIT_CONFIG.get_or_init(Tx5InitConfig::default)
71    }
72}
73
74static TX5_INIT_CONFIG: once_cell::sync::OnceCell<Tx5InitConfig> =
75    once_cell::sync::OnceCell::new();