Skip to main content

veilid_core/
lib.rs

1//! # The Veilid Framework
2//!
3//! This is the core library used to create a Veilid node and operate it as part of an application.
4//!
5//! `veilid-core` contains all of the core logic for Veilid and can be used in mobile applications as well as desktop
6//! and in-browser WebAssembly apps.
7//!
8//! ## Getting started
9//!
10//! - [Developer Book](https://veilid.gitlab.io/developer-book/)
11//! - [Examples](https://gitlab.com/veilid/veilid/-/tree/main/veilid-core/examples/)
12//! - [API Documentation](https://docs.rs/veilid-core)
13//!
14//! The public API is accessed by getting a [VeilidAPI] object via a call to [api_startup], [api_startup_json].
15//!
16//! From there, a [RoutingContext] object can get you access to public and private routed operations.
17//!
18//! ## Cargo features
19//!
20//! The default `veilid-core` configurations are:
21//!
22//! * `default` - Uses `tokio` as the async runtime.
23//!
24//! If you use `--no-default-features`, you can switch to other runtimes:
25//!
26//! * `default-async-std` - Uses `async-std` as the async runtime.
27//! * `default-wasm` - When building for the `wasm32` architecture, use this to enable `wasm-bindgen-futures` as the async runtime.
28//!
29
30#![recursion_limit = "256"]
31
32cfg_if::cfg_if! {
33    if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] {
34        #[cfg(any(feature = "rt-async-std", feature = "rt-tokio"))]
35        compile_error!("features \"rt-async-std\" and \"rt-tokio\" can not be specified for WASM");
36    } else {
37        #[cfg(all(feature = "rt-async-std", feature = "rt-tokio"))]
38        compile_error!(
39            "feature \"rt-async-std\" and feature \"rt-tokio\" cannot be enabled at the same time"
40        );
41        #[cfg(not(any(feature = "rt-async-std", feature = "rt-tokio")))]
42        compile_error!("exactly one of feature \"rt-async-std\" or feature \"rt-tokio\" must be specified");
43    }
44}
45
46#[macro_use]
47extern crate alloc;
48
49mod attachment_manager;
50#[cfg(feature = "unstable-blockstore")]
51mod block_store;
52mod component;
53mod core_context;
54mod crypto;
55mod logging;
56mod network_manager;
57mod protected_store;
58mod routing_table;
59mod rpc_processor;
60mod stats_accounting;
61mod storage_manager;
62mod table_store;
63mod veilid_api;
64mod veilid_config;
65
66////////////////////////////////////////////////////////////////////////////////////////////
67// Public API Exports
68
69pub use self::component::VeilidComponentGuard;
70pub use self::core_context::{api_startup, api_startup_json, UpdateCallback};
71pub use self::logging::{
72    ApiTracingLayer, FmtStripVeilidFields, VeilidLayerFilter, VeilidLayerFilterConfig,
73    VeilidLayerLogKeyFilter, VeilidLogDirective, VeilidLogKey, VeilidLogKeyFilterMode,
74    VEILID_LOG_KEY_FIELD,
75};
76#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
77pub use self::logging::{LogOutput, LogOutputKind, VeilidTracing};
78pub use self::veilid_api::*;
79pub use self::veilid_config::*;
80/// Re-exported from `async-trait` crate
81pub use async_trait;
82/// Re-exported from `tracing` crate
83pub use tracing;
84/// Re-exported from `tracing-subscriber` crate
85pub use tracing_subscriber;
86/// Re-exported from `veilid-tools` crate
87pub use veilid_tools as tools;
88
89#[cfg(target_os = "android")]
90pub use veilid_api::android::veilid_core_setup_android;
91
92#[cfg(any(test, feature = "test-util"))]
93#[doc(hidden)]
94pub mod tests;
95
96cfg_if::cfg_if! {
97    if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] {
98        pub use wasm_bindgen::prelude::*;
99        pub use tsify::*;
100    }
101}
102
103/// Return the cargo package version of veilid-core in string format.
104#[must_use]
105pub fn veilid_version_string() -> String {
106    env!("CARGO_PKG_VERSION").to_owned()
107}
108
109/// Return the cargo package version of veilid-core in tuple format.
110#[must_use]
111pub fn veilid_version() -> (u32, u32, u32) {
112    (
113        u32::from_str(env!("CARGO_PKG_VERSION_MAJOR")).unwrap_or_log(),
114        u32::from_str(env!("CARGO_PKG_VERSION_MINOR")).unwrap_or_log(),
115        u32::from_str(env!("CARGO_PKG_VERSION_PATCH")).unwrap_or_log(),
116    )
117}
118
119#[cfg(not(build_docs))]
120include!(env!("BOSION_PATH"));
121
122/// Return the features that were enabled when veilid-core was built.
123#[must_use]
124pub fn veilid_features() -> Vec<String> {
125    cfg_if! {
126        if #[cfg(build_docs)] {
127            vec!["default".to_string()]
128        } else {
129            let features = Bosion::CRATE_FEATURES.to_vec();
130            features.into_iter().map(String::from).collect()
131        }
132    }
133}
134
135////////////////////////////////////////////////////////////////////////////////////////////
136// Crate-only Exports
137
138mod veilid_capnp {
139    #![allow(
140        clippy::all,
141        clippy::must_use_candidate,
142        clippy::large_futures,
143        clippy::large_stack_arrays,
144        clippy::large_stack_frames,
145        clippy::large_types_passed_by_value,
146        clippy::unused_async,
147        clippy::ptr_cast_constness
148    )]
149    include!("../proto/veilid_capnp.rs");
150}
151
152use self::attachment_manager::TickEvent;
153use self::component::*;
154use self::core_context::RegisteredComponents;
155#[allow(unused_imports)]
156use self::logging::{
157    debug_duration, record_duration, record_duration_fut, MeasureDebugFuture, MeasureFuture,
158    VeilidComponentLogFacilities, VeilidComponentLogFacility, DEBUGWARN,
159};
160use self::stats_accounting::*;
161
162use async_trait::*;
163use cfg_if::*;
164use enumset::*;
165use eyre::{bail, eyre, Report as EyreReport, Result as EyreResult, WrapErr};
166use futures_util::stream::{FuturesOrdered, FuturesUnordered, StreamExt as _};
167use get_size::*;
168use indent::*;
169use parking_lot::*;
170use schemars::JsonSchema;
171use serde::*;
172use stop_token::{future::FutureExt as _, *};
173use thiserror::Error as ThisError;
174use tracing::*;
175use veilid_tools::*;
176
177/////////////////////////////////////////////////////////////////////////