nautilus_serialization/capnp/
mod.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Cap'n Proto serialization for Nautilus types.
17//!
18//! This module provides Cap'n Proto serialization support for Nautilus domain types.
19//! The generated schema modules are available at the crate root for proper cross-referencing.
20//!
21//! # Generated modules
22//!
23//! The following modules are generated from Cap'n Proto schemas:
24//! - `crate::base_capnp` - Base types (UUID4, UnixNanos, StringMap)
25//! - `crate::identifiers_capnp` - Identifier types
26//! - `crate::types_capnp` - Value types (Price, Quantity, Money, etc.)
27//! - `crate::enums_capnp` - Enumerations
28//! - `crate::trading_capnp` - Trading commands
29//! - `crate::data_capnp` - Data commands and responses
30//! - `crate::order_capnp` - Order events
31//! - `crate::position_capnp` - Position events
32//! - `crate::account_capnp` - Account events
33//! - `crate::market_capnp` - Market data types
34
35#![cfg(feature = "capnp")]
36
37pub mod conversions;
38
39// Re-export generated modules for convenience.
40// Re-export conversion functions for use by other crates
41pub use conversions::order_side_to_capnp;
42
43pub use crate::{
44    account_capnp, base_capnp, data_capnp, enums_capnp, identifiers_capnp, market_capnp,
45    order_capnp, position_capnp, trading_capnp, types_capnp,
46};
47
48/// Trait for converting Rust types to Cap'n Proto builders.
49pub trait ToCapnp<'a> {
50    /// The Cap'n Proto builder type for this Rust type.
51    type Builder;
52
53    /// Convert this Rust value to a Cap'n Proto builder.
54    fn to_capnp(&self, builder: Self::Builder);
55}
56
57/// Trait for converting Cap'n Proto readers to Rust types.
58pub trait FromCapnp<'a> {
59    /// The Cap'n Proto reader type for this Rust type.
60    type Reader;
61
62    /// Convert a Cap'n Proto reader to this Rust type.
63    ///
64    /// # Errors
65    ///
66    /// Returns an error if the Cap'n Proto data is invalid or cannot be converted.
67    fn from_capnp(reader: Self::Reader) -> Result<Self, Box<dyn std::error::Error>>
68    where
69        Self: Sized;
70}
71
72#[cfg(test)]
73mod tests {
74    use rstest::rstest;
75
76    #[rstest]
77    fn test_capnp_feature_enabled() {
78        // This test ensures the capnp feature is properly configured
79        assert!(cfg!(feature = "capnp"));
80    }
81}