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//! # Cargo features
41//!
42//! - **`std`** (default) — standard-library integration (`SystemTime`/`Duration`
43//!   conversions, `std::error::Error`). Without it the crate is `no_std` + `alloc`.
44//! - **`json`** — proto3 canonical JSON serde for the WKTs.
45//! - **`arbitrary`** — `arbitrary::Arbitrary` derives for fuzzing.
46//! - **`reflect`** — runtime reflection: the WKT view types implement
47//!   `buffa_descriptor::reflect::ReflectMessage`, so a message that has a WKT
48//!   field can reflect over it. This pulls a `buffa-descriptor` dependency and
49//!   requires `std` (the embedded descriptor pool uses `std::sync::OnceLock`).
50//!   If you reach for `&view as &dyn ReflectMessage` on a WKT view and the
51//!   compiler says `ReflectMessage` is not implemented, enable this feature.
52
53#![cfg_attr(not(feature = "std"), no_std)]
54#![deny(rustdoc::broken_intra_doc_links)]
55extern crate alloc;
56
57// Extension modules (ergonomic helpers — hand-written, not generated).
58mod any_ext;
59mod duration_ext;
60mod empty_ext;
61mod field_mask_ext;
62mod timestamp_ext;
63mod value_ext;
64#[cfg(feature = "json")]
65mod view_serde_ext;
66mod wrapper_ext;
67
68// Well-known type Rust structs — generated once by `gen_wkt_types`, checked
69// into src/generated/. These protos are Google-owned and frozen; regeneration
70// is only needed when buffa-codegen's output format changes. See the
71// `task gen-wkt-types` target and the `check-generated-code` CI job.
72//
73// The checked-in approach means consumers of buffa-types need only the
74// `buffa` runtime — NOT protoc, NOT buffa-build, NOT buffa-codegen.
75//
76// The allow attributes suppress lints that fire on generated code:
77//   derivable_impls      — enum Default impls are explicit rather than derived
78//   match_single_binding — empty messages generate a single-arm wildcard merge
79#[allow(
80    clippy::derivable_impls,
81    clippy::match_single_binding,
82    non_camel_case_types
83)]
84pub mod google {
85    pub mod protobuf {
86        include!("generated/google.protobuf.mod.rs");
87    }
88}
89
90// Convenience re-exports of the most commonly-used well-known types.
91// Full paths (`google::protobuf::*`) remain available for disambiguation.
92// Wrapper types (Int32Value, etc.) are NOT re-exported to avoid name
93// collisions with similarly-named types in user code.
94pub use google::protobuf::{
95    Any, Duration, Empty, FieldMask, ListValue, NullValue, Struct, Timestamp, Value,
96};
97
98// Re-export error types from extension modules (these are hand-written types
99// in private modules, so re-exporting is the only way to make them accessible).
100pub use duration_ext::DurationError;
101pub use timestamp_ext::TimestampError;
102
103// Re-export the WKT registry function for `Any` JSON + text support.
104pub use any_ext::register_wkt_types;
105
106#[cfg(test)]
107mod full_name_tests {
108    use super::google::protobuf::*;
109    use buffa::MessageName;
110
111    // Regression test: the WKT FQNs are baked into Any type-URLs, JSON
112    // serialization, and the type registry. Codegen must keep emitting them
113    // verbatim — these strings are observable on the wire.
114    #[test]
115    fn well_known_types_full_names_match_proto() {
116        assert_eq!(Timestamp::FULL_NAME, "google.protobuf.Timestamp");
117        assert_eq!(Duration::FULL_NAME, "google.protobuf.Duration");
118        assert_eq!(Any::FULL_NAME, "google.protobuf.Any");
119        assert_eq!(Empty::FULL_NAME, "google.protobuf.Empty");
120        assert_eq!(FieldMask::FULL_NAME, "google.protobuf.FieldMask");
121        assert_eq!(Struct::FULL_NAME, "google.protobuf.Struct");
122        assert_eq!(Value::FULL_NAME, "google.protobuf.Value");
123        assert_eq!(ListValue::FULL_NAME, "google.protobuf.ListValue");
124    }
125}