oxiproto 0.1.0

Pure Rust protobuf toolkit (no protoc)
Documentation
#![forbid(unsafe_code)]

//! `oxiproto` — Pure Rust protobuf toolkit, no `protoc` required.
//!
//! This crate is the top-level facade for the OxiProto stack.  It re-exports
//! the core types from [`oxiproto_core`] and conditionally exposes helpers
//! from the sub-crates depending on which features are enabled.
//!
//! ## Quick start
//!
//! ```rust
//! # use oxiproto::prelude::*;
//! // Use OxiMessage for native wire-format encode/decode (no prost macros needed).
//! // Use Message (prost) for prost-derive generated types.
//! ```
//!
//! ## Trait overview
//!
//! | Trait          | Source               | Description |
//! |----------------|----------------------|-------------|
//! | [`OxiMessage`] | `oxiproto-core`      | Native wire-format encode/decode trait. Generated by codegen when `emit_oxi_message_impl=true`. |
//! | [`OxiName`]    | `oxiproto-core`      | Provides `NAME`, `PACKAGE`, `full_name()`, `type_url()` for a message type. |
//! | [`OxiOneof`]   | `oxiproto-core`      | Marker trait for native oneof enum types. |
//! | [`Extensions`] | `oxiproto-core`      | Trait for proto extension fields. |
//! | `Message`      | `prost`              | The prost derive-based message trait (re-exported for compat). |
//! | `Name`         | `prost`              | The prost name trait (re-exported for compat). |
//!
//! ## Features
//!
//! | Feature      | Default | Description |
//! |--------------|---------|-------------|
//! | `build`      | off     | Enables `oxiproto::build` with `compile_protos` and `Builder`. |
//! | `reflect`    | off     | Enables `oxiproto::reflect` with runtime `DescriptorPool` and `DynamicMessage`. |
//! | `wkt`        | off     | Enables `oxiproto::wkt` with Well-Known Type extension traits. |
//! | `wkt-chrono` | off     | Enables `wkt` + the `chrono` interop methods on `TimestampExt` / `DurationExt`. |
//! | `codegen`    | off     | Enables `oxiproto::codegen` for generating plain Rust from a `FileDescriptorSet`. |

pub use oxiproto_core::prost_types;
pub use oxiproto_core::{
    Extensions, Message, Name, OxiMessage, OxiName, OxiOneof, OxiProtoError, OxiProtoResult,
};

/// Native wire format primitives (varint, zigzag, tags, fixed, buffers).
///
/// Re-exported from [`oxiproto_core::wire`] and always available regardless
/// of which features are enabled.
pub use oxiproto_core::wire;

/// Returns the version string of the `oxiproto` crate.
///
/// # Example
///
/// ```
/// let v = oxiproto::version();
/// assert!(!v.is_empty());
/// ```
pub fn version() -> &'static str {
    env!("CARGO_PKG_VERSION")
}

/// Encode a message implementing [`OxiMessage`] to a `Vec<u8>`.
///
/// This is a convenience wrapper around [`OxiMessage::encode_to_vec`].
///
/// # Example
///
/// ```ignore
/// let bytes = oxiproto::encode(&my_message);
/// ```
pub fn encode<T: OxiMessage>(msg: &T) -> Vec<u8> {
    msg.encode_to_vec()
}

/// Decode a message from a byte slice.
///
/// This is a convenience wrapper around [`OxiMessage::decode`].
///
/// # Errors
///
/// Returns [`OxiProtoError`] if the bytes cannot be decoded into `T`.
///
/// # Example
///
/// ```ignore
/// let msg = oxiproto::decode::<MyMessage>(&bytes)?;
/// ```
pub fn decode<T: OxiMessage>(bytes: &[u8]) -> OxiProtoResult<T> {
    T::decode(bytes)
}

/// Top-level re-export of [`oxiproto_build::compile_protos`] for ergonomic use
/// in downstream `build.rs` files.
///
/// Enabled with the `build` feature.
#[cfg(feature = "build")]
pub use oxiproto_build::compile_protos;

/// Glob-importable prelude of the most commonly used OxiProto types.
///
/// ```
/// use oxiproto::prelude::*;
/// ```
pub mod prelude {
    pub use oxiproto_core::wire::{DecodeBuffer, EncodeBuffer, WireType};
    pub use oxiproto_core::{
        Extensions, Message, Name, OxiMessage, OxiName, OxiOneof, OxiProtoError, OxiProtoResult,
    };
}

/// Build-time helpers for compiling `.proto` files to Rust.
///
/// Enabled with the `build` feature.
#[cfg(feature = "build")]
pub mod build {
    pub use oxiproto_build::*;
}

/// Runtime protobuf reflection via `prost-reflect`.
///
/// Enabled with the `reflect` feature.
#[cfg(feature = "reflect")]
pub mod reflect {
    pub use oxiproto_reflect::*;
}

/// Well-Known Types interop (Timestamp, Duration, Any, etc.).
///
/// Enabled with the `wkt` feature. Use the `wkt-chrono` feature to also
/// enable `chrono` conversion methods.
#[cfg(feature = "wkt")]
pub mod wkt {
    pub use oxiproto_wkt::*;
}

/// Code generation from `FileDescriptorSet` to plain Rust structs/enums.
///
/// Enabled with the `codegen` feature.
#[cfg(feature = "codegen")]
pub mod codegen {
    pub use oxiproto_codegen::*;
}

/// Canonical Protobuf-JSON mapping.
///
/// Enabled with the `json` feature. Provides [`json::to_json`],
/// [`json::from_json`], and [`json::JsonCodec`].
#[cfg(feature = "json")]
pub mod json {
    pub use oxiproto_json::*;
}