Skip to main content

net_sdk/
lib.rs

1//! # Net SDK
2//!
3//! Ergonomic Rust SDK for the Net mesh network.
4//!
5//! The core `net` crate is the engine. This SDK is what developers actually import.
6//!
7//! # Example
8//!
9//! ```rust,no_run
10//! use net_sdk::{Net, Backpressure};
11//! use futures::StreamExt;
12//!
13//! # async fn example() -> net_sdk::error::Result<()> {
14//! let node = Net::builder()
15//!     .shards(4)
16//!     .backpressure(Backpressure::DropOldest)
17//!     .memory()
18//!     .build()
19//!     .await?;
20//!
21//! // Emit events
22//! node.emit(&serde_json::json!({"token": "hello"}))?;
23//! node.emit_raw(b"{\"token\": \"world\"}" as &[u8])?;
24//!
25//! // Subscribe to a stream
26//! let mut stream = node.subscribe(Default::default());
27//! while let Some(event) = stream.next().await {
28//!     let event = event?;
29//!     println!("{}", event.raw_str().unwrap_or("<non-utf8>"));
30//! }
31//!
32//! node.shutdown().await?;
33//! # Ok(())
34//! # }
35//! ```
36
37/// Crate version, sourced from `Cargo.toml` at build time. Re-
38/// exported so downstream binaries (the `net` CLI in particular)
39/// can report the embedded SDK version without hardcoding a
40/// literal that silently drifts on every workspace bump.
41pub const VERSION: &str = env!("CARGO_PKG_VERSION");
42
43#[cfg(feature = "compute")]
44pub mod compute;
45pub mod config;
46#[cfg(feature = "cortex")]
47pub mod cortex;
48#[cfg(feature = "dataforts")]
49pub mod dataforts;
50#[cfg(feature = "deck")]
51pub mod deck;
52pub mod error;
53#[cfg(feature = "groups")]
54pub mod groups;
55#[cfg(feature = "net")]
56pub mod mesh;
57#[cfg(all(feature = "net", feature = "cortex"))]
58pub mod mesh_rpc;
59#[cfg(all(feature = "net", feature = "cortex"))]
60pub mod mesh_rpc_resilience;
61#[cfg(feature = "meshdb")]
62pub mod meshdb;
63#[cfg(feature = "meshos")]
64pub mod meshos;
65mod net;
66#[cfg(feature = "redis")]
67pub mod redis_dedup;
68pub mod stream;
69#[cfg(feature = "testing")]
70pub mod testing;
71#[cfg(feature = "tool")]
72pub mod tool;
73// On-demand cross-peer movement primitives (blob + directory transfer
74// over the fairscheduler stream transport). Needs the networked node
75// (`net`) and the blob storage layer (`dataforts`); the transfer
76// surface lives here rather than in `dataforts` (which is storage +
77// operator read side).
78#[cfg(all(feature = "net", feature = "dataforts"))]
79pub mod transport;
80
81/// Procedural-macro re-exports gated by the `macros` feature.
82///
83/// Currently ships the `#[tool]` attribute macro — see the
84/// per-macro docs for the full attribute surface and an example.
85/// The macro generates a sibling `<fn>_descriptor()` /
86/// `<fn>_register(mesh)` pair atop `metadata_for::<Req, Resp>(name)
87/// .build()` + `mesh.serve_tool(...)`.
88///
89/// Always pulled in alongside `tool` because the macro's expansion
90/// references `net_sdk::tool::*`; users that don't want the
91/// proc-macro2 / syn / quote build cost simply omit the `macros`
92/// feature.
93#[cfg(feature = "macros")]
94pub mod macros {
95    pub use net_sdk_macros::tool;
96}
97
98#[cfg(feature = "redis")]
99pub use redis_dedup::RedisStreamDedup;
100
101// Security surface — identity (keypairs + tokens), capabilities
102// (declare + query), and subnets (visibility partitioning). All
103// three ride the `net` feature because they share a subprotocol
104// dispatch and operate as a single unit at runtime.
105#[cfg(feature = "net")]
106pub mod capabilities;
107#[cfg(feature = "net")]
108pub mod identity;
109#[cfg(feature = "net")]
110pub mod subnets;
111
112// Aggregator + lifecycle surfaces. Aggregator-daemon clients
113// (`RegistryClient`, `FoldQueryClient`) + the daemon-author
114// types (`AggregatorRegistry`, `LifecycleGroup`,
115// `HealthMonitor`). The `aggregator` feature is on by default and
116// transitively activates `net` (every surface in this module
117// routes through `Mesh::call` / `Mesh::serve_rpc`); the explicit
118// flag mirrors the per-binding `aggregator` Cargo feature so a
119// `--no-default-features` consumer can opt in by name.
120#[cfg(feature = "aggregator")]
121pub mod aggregator;
122
123// Re-export the main handle.
124pub use crate::net::{Net, PollRequest, PollResponse, Receipt, Stats};
125
126// Re-export config types.
127pub use crate::config::{Backpressure, NetBuilder};
128
129// Re-export stream types.
130pub use crate::stream::{EventStream, SubscribeOpts, TypedEventStream};
131
132// Re-export core types that users will need.
133pub use ::net::config::{BatchConfig, ScalingPolicy};
134pub use ::net::consumer::Ordering;
135pub use ::net::event::{Event, RawEvent, StoredEvent};
136pub use ::net::Filter;
137
138// Feature-gated re-exports.
139#[cfg(feature = "redis")]
140pub use ::net::config::RedisAdapterConfig;
141
142#[cfg(feature = "jetstream")]
143pub use ::net::config::JetStreamAdapterConfig;
144
145#[cfg(feature = "net")]
146pub use ::net::adapter::net::NetAdapterConfig;
147
148#[cfg(feature = "net")]
149pub use ::net::adapter::net::{
150    CloseBehavior, Reliability, Stream as MeshStream, StreamConfig, StreamStats,
151};
152
153// Channel (distributed pub/sub) types. Ship alongside `net` because
154// they live on the mesh transport — subscribing / publishing require
155// a live `Mesh`.
156#[cfg(feature = "net")]
157pub use ::net::adapter::net::{
158    AckReason, ChannelConfig, ChannelId, ChannelName, OnFailure, PublishConfig, PublishReport,
159    Visibility,
160};
161
162#[cfg(feature = "net")]
163pub use crate::mesh::{Mesh, MeshBuilder, SubscribeOptions};
164
165// Raw substrate handle. The SDK's `Mesh` is the ergonomic
166// front-door, but typed RPC clients (`RegistryClient`,
167// `FoldQueryClient`) and FFI bindings want the underlying
168// `Arc<MeshNode>` directly — `Mesh::node_arc()` hands one out.
169// Re-exporting here lets downstream consumers (the CLI's
170// remote-attach context, in particular) name the type without
171// reaching into `::net::adapter::net` themselves.
172#[cfg(feature = "net")]
173pub use ::net::adapter::net::MeshNode;
174
175// Compute surface — `MeshDaemon` trait + runtime. Gated by the
176// `compute` feature (which depends on `net`).
177#[cfg(feature = "compute")]
178pub use crate::compute::{
179    CausalEvent, CausalLink, DaemonError as ComputeDaemonError, DaemonHandle, DaemonHostConfig,
180    DaemonRuntime, DaemonStats, MeshDaemon, MigrationError, MigrationHandle, MigrationOpts,
181    MigrationPhase, StateSnapshot,
182};
183
184// Convenience re-exports for the common security types, so users can
185// `use net_sdk::{Identity, TokenScope};` without reaching for a
186// sub-module path.
187#[cfg(feature = "net")]
188pub use crate::capabilities::{CapabilityFilter, CapabilitySet};
189#[cfg(feature = "net")]
190pub use crate::identity::{Identity, PermissionToken, TokenError, TokenScope};
191#[cfg(feature = "net")]
192pub use crate::subnets::{SubnetId, SubnetPolicy};
193
194impl NetBuilder {
195    /// Build and start the node.
196    pub async fn build(self) -> error::Result<Net> {
197        Net::from_builder(self).await
198    }
199}