Skip to main content

prost_protovalidate_types/
lib.rs

1//! Generated Rust types for the [`buf.validate`](https://github.com/bufbuild/protovalidate)
2//! protobuf schema, built with `prost` (and optionally `prost-reflect`).
3//!
4//! This crate provides:
5//!
6//! - All message and enum types from `buf/validate/validate.proto`
7//!   (e.g. [`FieldRules`], [`MessageRules`], [`OneofRules`]).
8//! - [`rules_meta`] — rule ids and violation-message templates shared by the
9//!   runtime evaluator and the build-time code generator.
10//!
11//! With the **`reflect`** feature (default), it additionally provides:
12//!
13//! - A shared [`DESCRIPTOR_POOL`] containing the file descriptor set for
14//!   runtime reflection.
15//! - Extension traits for extracting constraint annotations from descriptors:
16//!   - [`FieldConstraintsExt`] — `buf.validate.field` rules on a
17//!     `FieldDescriptor`.
18//!   - [`MessageConstraintsExt`] — `buf.validate.message` rules on a
19//!     `MessageDescriptor`.
20//!   - [`OneofConstraintsExt`] — `buf.validate.oneof` rules on a
21//!     `OneofDescriptor`.
22//!   - [`PredefinedConstraintsExt`] — `buf.validate.predefined` rules.
23//!   - [`FieldConstraintsDynExt`] / [`MessageConstraintsDynExt`] — raw
24//!     `DynamicMessage` access for the runtime validator.
25//! - Typed helper functions for extension extraction with concrete error types:
26//!   [`field_constraints_typed`], [`message_constraints_typed`],
27//!   [`oneof_constraints_typed`], [`predefined_constraints_typed`].
28//!
29//! Disabling `reflect` yields a slim, reflection-free build — the generated
30//! prost types plus [`rules_meta`] — for consumers that only run build-time
31//! generated validators.
32//!
33//! # Usage
34//!
35//! Most users do not need this crate directly — the
36//! [`prost-protovalidate`](https://crates.io/crates/prost-protovalidate) crate re-exports
37//! everything required for validation via its `types` module. Use this crate when you only need the
38//! generated types or descriptor pool without the evaluation engine.
39
40#![warn(missing_docs)]
41
42#[allow(
43    missing_docs,
44    clippy::len_without_is_empty,
45    clippy::doc_lazy_continuation,
46    clippy::doc_markdown,
47    clippy::must_use_candidate
48)]
49mod proto;
50
51#[cfg(feature = "reflect")]
52mod constraints;
53
54pub mod rules_meta;
55
56pub use proto::*;
57
58#[cfg(feature = "reflect")]
59pub use constraints::*;
60
61/// Error returned while decoding `buf.validate` descriptor extensions.
62#[derive(Debug, thiserror::Error)]
63pub enum ConstraintDecodeError {
64    /// The generated descriptor pool could not be decoded.
65    #[error("descriptor pool initialization failed: {0}")]
66    DescriptorPoolInitialization(String),
67
68    /// The expected extension descriptor is missing from the pool.
69    #[error("missing extension descriptor `{0}`")]
70    MissingExtension(&'static str),
71
72    /// The extension payload could not be decoded into the typed rule.
73    #[error(transparent)]
74    Decode(#[from] prost::DecodeError),
75}
76
77/// Typed decode result for descriptor extension constraints.
78pub type ConstraintDecodeResult<T> = Result<Option<T>, ConstraintDecodeError>;