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