zus_rs/
lib.rs

1//! # ZUS-RS: ZUS RPC Framework for Rust
2//!
3//! `zus-rs` is a high-performance RPC framework with cross-language compatibility
4//! (Rust, Java, C++). It provides service discovery via ZooServer and supports
5//! QuickLZ/Snappy compression.
6//!
7//! ## Features
8//!
9//! - **Cross-language RPC**: Compatible with Java and C++ implementations
10//! - **Protocol Buffer serialization**: Efficient binary protocol
11//! - **Compression**: QuickLZ (default) and Snappy support
12//! - **Service Discovery**: Integration with ZooServer
13//! - **Async I/O**: Built on Tokio runtime
14//! - **Type-safe**: Full Rust type safety with protobuf definitions
15//!
16//! ## Quick Start
17//!
18//! ### Client Example
19//!
20//! ```toml
21//! [dependencies]
22//! zus-rs = { version = "1.1.4", features = ["client"] }
23//! tokio = { version = "1.35", features = ["full"] }
24//! ```
25//!
26//! ```rust,no_run
27//! # // Note: This is a documentation example showing the conceptual API
28//! # // The actual types may differ - check the API docs for details
29//! use zus_rs::prelude::*;
30//!
31//! # #[tokio::main]
32//! # async fn main() -> anyhow::Result<()> {
33//! #     // This is a conceptual example
34//! #     // See zus-examples/ for working code
35//! #     Ok(())
36//! # }
37//! ```
38//!
39//! ### Server Example
40//!
41//! ```toml
42//! [dependencies]
43//! zus-rs = { version = "1.1.4", features = ["server"] }
44//! tokio = { version = "1.35", features = ["full"] }
45//! ```
46//!
47//! ```rust,no_run
48//! # // Note: This is a documentation example showing the conceptual API
49//! # // See zus-examples/ for working server implementations
50//! use zus_rs::prelude::*;
51//!
52//! # #[tokio::main]
53//! # async fn main() -> anyhow::Result<()> {
54//! #     // This is a conceptual example
55//! #     // See zus-rpc-server documentation for actual API
56//! #     Ok(())
57//! # }
58//! ```
59//!
60//! ### Protocol Only (No RPC Runtime)
61//!
62//! ```toml
63//! [dependencies]
64//! zus-rs = { version = "1.1.4", features = ["protocol"] }
65//! ```
66//!
67//! ```rust
68//! use zus_rs::prelude::*;
69//!
70//! // Create protocol message
71//! let request = ZooRegisterClientRequest {};
72//!
73//! // Use compression types
74//! let _compressor = Compressor::default();
75//! let _comp_type = CompressionType::QuickLZ;
76//! ```
77//!
78//! ## Feature Flags
79//!
80//! Choose the features you need:
81//!
82//! | Feature | Description | Includes |
83//! |---------|-------------|----------|
84//! | `default` | Client + Server + Protocol | Everything except runtime |
85//! | `minimal` | Protocol definitions only | Just `zus-proto` |
86//! | `protocol` | Protocol + Codec + Compression | `zus-proto`, `zus-common` |
87//! | `client` | RPC client functionality | `protocol`, `zus-rpc-client`, `zus-discovery` |
88//! | `server` | RPC server functionality | `protocol`, `zus-rpc-server`, `zus-discovery`, `zus-macros` |
89//! | `full` | Everything | `client` + `server` |
90//!
91//! ## Examples
92//!
93//! ```toml
94//! # Just client
95//! [dependencies]
96//! zus-rs = { version = "1.1.4", features = ["client"] }
97//!
98//! # Just server
99//! [dependencies]
100//! zus-rs = { version = "1.1.4", features = ["server"] }
101//!
102//! # Protocol only (no RPC runtime)
103//! [dependencies]
104//! zus-rs = { version = "1.1.4", features = ["protocol"] }
105//!
106//! # Minimal (just message definitions)
107//! [dependencies]
108//! zus-rs = { version = "1.1.4", features = ["minimal"] }
109//!
110//! # Everything (default)
111//! [dependencies]
112//! zus-rs = "1.1.4"
113//! ```
114//!
115//! ## Architecture
116//!
117//! ```text
118//! ┌─────────────────────────────────────────┐
119//! │           zus-rs (convenience)          │
120//! └─────────────────────────────────────────┘
121//!                    │
122//!        ┌───────────┼───────────┐
123//!        │           │           │
124//!        ↓           ↓           ↓
125//!   ┌────────┐  ┌────────┐  ┌────────┐
126//!   │ client │  │ server │  │protocol│
127//!   └────────┘  └────────┘  └────────┘
128//!        │           │           │
129//!        ↓           ↓           ↓
130//!   ┌─────────────────────────────────┐
131//!   │  zus-rpc-{client,server}        │
132//!   │  zus-discovery                  │
133//!   │  zus-common (codec/compression) │
134//!   │  zus-proto (protobuf messages)  │
135//!   └─────────────────────────────────┘
136//! ```
137//!
138//! ## Cross-Language Compatibility
139//!
140//! ZUS-RS is wire-compatible with:
141//! - **Java**: ZUS Java client/server (tested with integration tests)
142//! - **C++**: ZUS C++ client/server (production-tested)
143//!
144//! All implementations share:
145//! - Same 40-byte RPC header format
146//! - Same Protocol Buffer definitions
147//! - Same compression algorithms (QuickLZ, Snappy)
148//! - Same CRC validation (CRC16 header, CRC32 data)
149//!
150//! See `tests/cross-language/` for comprehensive cross-language test suite.
151//!
152//! ## Documentation
153//!
154//! - [Getting Started Guide](../docs/GETTING_STARTED.md)
155//! - [Protocol Specification](../docs/protocol&wireformat.md)
156//! - [ZooServer Architecture](../docs/ZOOSERVER_ARCHITECTURE.md)
157//! - [Cross-Language Tests](../tests/cross-language/README.md)
158
159#![cfg_attr(docsrs, feature(doc_cfg))]
160
161// Re-export core protocol definitions (always available)
162pub use zus_proto as proto;
163
164// Re-export common codec and compression (available with protocol, client, or server features)
165#[cfg(feature = "protocol")]
166#[cfg_attr(docsrs, doc(cfg(feature = "protocol")))]
167pub use zus_common as common;
168
169// Re-export service discovery (available with client or server features)
170#[cfg(any(feature = "client", feature = "server"))]
171#[cfg_attr(docsrs, doc(cfg(any(feature = "client", feature = "server"))))]
172pub use zus_discovery as discovery;
173
174// Re-export RPC client (available with client feature)
175#[cfg(feature = "client")]
176#[cfg_attr(docsrs, doc(cfg(feature = "client")))]
177pub use zus_rpc_client as client;
178
179// Re-export RPC server (available with server feature)
180#[cfg(feature = "server")]
181#[cfg_attr(docsrs, doc(cfg(feature = "server")))]
182pub use zus_rpc_server as server;
183
184// Re-export proc macros (available with server feature)
185#[cfg(feature = "server")]
186#[cfg_attr(docsrs, doc(cfg(feature = "server")))]
187pub use zus_macros;
188
189// Re-export commonly used types for convenience
190#[cfg(any(feature = "client", feature = "server"))]
191#[cfg_attr(docsrs, doc(cfg(any(feature = "client", feature = "server"))))]
192pub use async_trait::async_trait;
193
194// Type aliases for more intuitive API
195/// Convenient type alias for `BaseRpcClient`
196///
197/// This is the main RPC client type for making service calls.
198///
199/// # Example
200/// ```no_run
201/// use zus_rs::RpcClient;
202///
203/// # #[tokio::main]
204/// # async fn main() -> anyhow::Result<()> {
205/// // Connect via TCP
206/// let client = RpcClient::new("tcp://localhost:9527").await?;
207///
208/// // Or via ZooServer discovery
209/// let client = RpcClient::new("zns://localhost:9528/zus/services/myservice/").await?;
210/// # Ok(())
211/// # }
212/// ```
213#[cfg(feature = "client")]
214#[cfg_attr(docsrs, doc(cfg(feature = "client")))]
215pub use zus_rpc_client::BaseRpcClient as RpcClient;
216
217/// Convenient type alias for `ZusServerManager`
218///
219/// This is the main RPC server type for hosting services.
220///
221/// # Example
222/// ```no_run
223/// use zus_rs::{RpcServer, prelude::*};
224/// use bytes::Bytes;
225/// use std::sync::Arc;
226///
227/// struct MyService;
228///
229/// #[async_trait]
230/// impl Service for MyService {
231///     fn service_name(&self) -> &str { "MyService" }
232///
233///     async fn do_work(
234///         &self,
235///         method: &str,
236///         params: Bytes,
237///         _ctx: zus_rpc_server::RequestContext,
238///     ) -> zus_common::Result<Bytes> {
239///         Ok(params)
240///     }
241/// }
242///
243/// # #[tokio::main]
244/// # async fn main() -> anyhow::Result<()> {
245/// let mut server = RpcServer::new("0.0.0.0".to_string(), 9527);
246/// server.register_service(Arc::new(MyService));
247/// server.start().await?;
248/// # Ok(())
249/// # }
250/// ```
251#[cfg(feature = "server")]
252#[cfg_attr(docsrs, doc(cfg(feature = "server")))]
253pub use zus_rpc_server::ZusServerManager as RpcServer;
254
255// Mobile protocol type aliases
256/// Mobile RPC Client (16-byte header protocol)
257///
258/// Use this for connecting to mobile-optimized servers.
259#[cfg(feature = "client")]
260#[cfg_attr(docsrs, doc(cfg(feature = "client")))]
261pub use zus_rpc_client::MobileRpcClient;
262
263/// Mobile RPC Server (16-byte header protocol)
264///
265/// Use this for mobile clients with bandwidth constraints.
266#[cfg(feature = "server")]
267#[cfg_attr(docsrs, doc(cfg(feature = "server")))]
268pub use zus_rpc_server::MobileRpcServer;
269
270// Prelude module with commonly used imports
271pub mod prelude {
272  //! Commonly used types and traits
273  //!
274  //! ```rust
275  //! use zus_rs::prelude::*;
276  //! ```
277
278  // Core protocol types
279  pub use crate::proto::{
280    ZooCreatePathRequest, ZooCreatePathResponse, ZooGetPathChildRequest, ZooGetPathChildResponse,
281    ZooRegisterClientRequest, ZooRegisterClientResponse, ZooSyncPathRequest, ZooSyncPathResponse,
282  };
283
284  // Common compression (Compressor, CompressType)
285  #[cfg(feature = "protocol")]
286  pub use crate::common::compression::{CompressionType, Compressor};
287
288  // Client types (re-export main client type and convenient alias)
289  #[cfg(feature = "client")]
290  pub use crate::client;
291
292  #[cfg(feature = "client")]
293  pub use crate::RpcClient;
294
295  // Server types (re-export server module and convenient alias)
296  #[cfg(feature = "server")]
297  pub use crate::server;
298
299  #[cfg(feature = "server")]
300  pub use crate::RpcServer;
301
302  #[cfg(feature = "server")]
303  pub use crate::server::Service;
304
305  // Mobile types
306  #[cfg(feature = "client")]
307  pub use crate::MobileRpcClient;
308
309  #[cfg(feature = "server")]
310  pub use crate::MobileRpcServer;
311
312  // Async trait
313  #[cfg(any(feature = "client", feature = "server"))]
314  pub use crate::async_trait;
315}
316
317#[cfg(test)]
318mod tests {
319  #[test]
320  fn test_proto_available() {
321    // Protocol should always be available
322    use crate::proto::ZooRegisterClientRequest;
323    let _req = ZooRegisterClientRequest {};
324  }
325
326  #[cfg(feature = "protocol")]
327  #[test]
328  fn test_common_available() {
329    use crate::common::compression::CompressionType;
330    let _compression = CompressionType::QuickLZ;
331  }
332
333  #[cfg(feature = "client")]
334  #[test]
335  fn test_client_module_available() {
336    // Just check that client module is accessible
337    #[allow(unused_imports)]
338    use crate::client;
339  }
340
341  #[cfg(feature = "server")]
342  #[test]
343  fn test_server_module_available() {
344    // Just check that server module is accessible
345    // Service is a trait, so we can't instantiate it
346    // Just verify the module exists
347    #[allow(unused_imports)]
348    use crate::server;
349  }
350}