Skip to main content

buffa_types/
lib.rs

1//! Protobuf well-known types for buffa.
2//!
3//! This crate provides Rust types for Google's well-known `.proto` types:
4//!
5//! - [`google::protobuf::Timestamp`] — Unix timestamp with nanosecond precision
6//! - [`google::protobuf::Duration`] — Signed duration with nanosecond precision
7//! - [`google::protobuf::Any`] — Any value with an attached type URL
8//! - [`google::protobuf::Struct`] / [`google::protobuf::Value`] / [`google::protobuf::ListValue`]
9//!   — JSON-like dynamic values
10//! - [`google::protobuf::FieldMask`] — Specifies a subset of fields referenced in a message
11//! - [`google::protobuf::Empty`] — A generic empty message
12//! - Wrapper types: [`google::protobuf::BoolValue`], [`google::protobuf::Int32Value`],
13//!   [`google::protobuf::Int64Value`], [`google::protobuf::UInt32Value`],
14//!   [`google::protobuf::UInt64Value`], [`google::protobuf::FloatValue`],
15//!   [`google::protobuf::DoubleValue`], [`google::protobuf::StringValue`],
16//!   [`google::protobuf::BytesValue`]
17//!
18//! # Usage
19//!
20//! ```rust,no_run
21//! use buffa_types::google::protobuf::Timestamp;
22//! use buffa::Message;
23//!
24//! let ts = Timestamp { seconds: 1_000_000_000, nanos: 0, ..Default::default() };
25//! let bytes = ts.encode_to_vec();
26//! let decoded = Timestamp::decode_from_slice(&bytes).unwrap();
27//! assert_eq!(ts, decoded);
28//! ```
29//!
30//! # Ergonomic helpers
31//!
32//! Common Rust type conversions are provided as trait impls:
33//!
34//! - `Timestamp` ↔ [`std::time::SystemTime`] (requires `std` feature)
35//! - `Duration` ↔ [`std::time::Duration`] (requires `std` feature)
36//! - `Any::pack` / `Any::unpack` helpers
37//! - `Value` constructors: [`Value::null`](google::protobuf::Value::null), `From<f64>`, `From<String>`, `From<bool>`, etc.
38//! - Wrapper type `From`/`Into` impls
39
40#![cfg_attr(not(feature = "std"), no_std)]
41#![deny(rustdoc::broken_intra_doc_links)]
42extern crate alloc;
43
44// Extension modules (ergonomic helpers — hand-written, not generated).
45mod any_ext;
46mod duration_ext;
47mod empty_ext;
48mod field_mask_ext;
49mod timestamp_ext;
50mod value_ext;
51mod wrapper_ext;
52
53// Well-known type Rust structs — generated once by `gen_wkt_types`, checked
54// into src/generated/. These protos are Google-owned and frozen; regeneration
55// is only needed when buffa-codegen's output format changes. See the
56// `task gen-wkt-types` target and the `check-generated-code` CI job.
57//
58// The checked-in approach means consumers of buffa-types need only the
59// `buffa` runtime — NOT protoc, NOT buffa-build, NOT buffa-codegen.
60//
61// The allow attributes suppress lints that fire on generated code:
62//   derivable_impls      — enum Default impls are explicit rather than derived
63//   match_single_binding — empty messages generate a single-arm wildcard merge
64#[allow(
65    clippy::derivable_impls,
66    clippy::match_single_binding,
67    non_camel_case_types
68)]
69pub mod google {
70    pub mod protobuf {
71        include!("generated/google.protobuf.any.rs");
72        include!("generated/google.protobuf.duration.rs");
73        include!("generated/google.protobuf.empty.rs");
74        include!("generated/google.protobuf.field_mask.rs");
75        include!("generated/google.protobuf.struct.rs");
76        include!("generated/google.protobuf.timestamp.rs");
77        include!("generated/google.protobuf.wrappers.rs");
78    }
79}
80
81// Convenience re-exports of the most commonly-used well-known types.
82// Full paths (`google::protobuf::*`) remain available for disambiguation.
83// Wrapper types (Int32Value, etc.) are NOT re-exported to avoid name
84// collisions with similarly-named types in user code.
85pub use google::protobuf::{
86    Any, Duration, Empty, FieldMask, ListValue, NullValue, Struct, Timestamp, Value,
87};
88
89// Re-export error types from extension modules (these are hand-written types
90// in private modules, so re-exporting is the only way to make them accessible).
91pub use duration_ext::DurationError;
92pub use timestamp_ext::TimestampError;
93
94// Re-export the WKT registry function for `Any` JSON support.
95#[cfg(feature = "json")]
96pub use any_ext::register_wkt_types;