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;
51#[cfg(feature = "json")]
52mod view_serde_ext;
53mod wrapper_ext;
54
55// Well-known type Rust structs — generated once by `gen_wkt_types`, checked
56// into src/generated/. These protos are Google-owned and frozen; regeneration
57// is only needed when buffa-codegen's output format changes. See the
58// `task gen-wkt-types` target and the `check-generated-code` CI job.
59//
60// The checked-in approach means consumers of buffa-types need only the
61// `buffa` runtime — NOT protoc, NOT buffa-build, NOT buffa-codegen.
62//
63// The allow attributes suppress lints that fire on generated code:
64// derivable_impls — enum Default impls are explicit rather than derived
65// match_single_binding — empty messages generate a single-arm wildcard merge
66#[allow(
67 clippy::derivable_impls,
68 clippy::match_single_binding,
69 non_camel_case_types
70)]
71pub mod google {
72 pub mod protobuf {
73 include!("generated/google.protobuf.mod.rs");
74 }
75}
76
77// Convenience re-exports of the most commonly-used well-known types.
78// Full paths (`google::protobuf::*`) remain available for disambiguation.
79// Wrapper types (Int32Value, etc.) are NOT re-exported to avoid name
80// collisions with similarly-named types in user code.
81pub use google::protobuf::{
82 Any, Duration, Empty, FieldMask, ListValue, NullValue, Struct, Timestamp, Value,
83};
84
85// Re-export error types from extension modules (these are hand-written types
86// in private modules, so re-exporting is the only way to make them accessible).
87pub use duration_ext::DurationError;
88pub use timestamp_ext::TimestampError;
89
90// Re-export the WKT registry function for `Any` JSON + text support.
91pub use any_ext::register_wkt_types;
92
93#[cfg(test)]
94mod full_name_tests {
95 use super::google::protobuf::*;
96 use buffa::MessageName;
97
98 // Regression test: the WKT FQNs are baked into Any type-URLs, JSON
99 // serialization, and the type registry. Codegen must keep emitting them
100 // verbatim — these strings are observable on the wire.
101 #[test]
102 fn well_known_types_full_names_match_proto() {
103 assert_eq!(Timestamp::FULL_NAME, "google.protobuf.Timestamp");
104 assert_eq!(Duration::FULL_NAME, "google.protobuf.Duration");
105 assert_eq!(Any::FULL_NAME, "google.protobuf.Any");
106 assert_eq!(Empty::FULL_NAME, "google.protobuf.Empty");
107 assert_eq!(FieldMask::FULL_NAME, "google.protobuf.FieldMask");
108 assert_eq!(Struct::FULL_NAME, "google.protobuf.Struct");
109 assert_eq!(Value::FULL_NAME, "google.protobuf.Value");
110 assert_eq!(ListValue::FULL_NAME, "google.protobuf.ListValue");
111 }
112}