1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#![doc = include_str!("../README.md")]
#![deny(warnings)]
#![warn(unused_extern_crates)]
#![deny(clippy::todo)]
#![deny(clippy::unimplemented)]
#![warn(clippy::unwrap_used)]
#![deny(clippy::expect_used)]
#![deny(clippy::panic)]
#![deny(clippy::unreachable)]
#![deny(clippy::await_holding_lock)]
#![deny(clippy::needless_pass_by_value)]
#![deny(clippy::trivially_copy_pass_by_ref)]

use crate::registry::REGISTRY;
use prost_reflect::ReflectMessage;

mod any;
mod bool;
mod bytes;
mod duration;
mod r#enum;
mod field;
mod list;
mod map;
mod message;
mod number;
mod registry;
mod string;
mod timestamp;
mod utils;
mod validate;

#[allow(clippy::trivially_copy_pass_by_ref)]
#[allow(clippy::enum_variant_names)]
mod validate_proto {
    use once_cell::sync::Lazy;
    use prost_reflect::DescriptorPool;

    #[allow(clippy::unwrap_used)]
    pub(crate) static DESCRIPTOR_POOL: Lazy<DescriptorPool> = Lazy::new(|| {
        DescriptorPool::decode(
            include_bytes!(concat!(env!("OUT_DIR"), "/file_descriptor_set.bin")).as_ref(),
        )
        .unwrap()
    });
    include!(concat!(env!("OUT_DIR"), "/validate.rs"));
}

/// Extension trait for validating messages using `prost-reflect`.
/// 
/// The implementation is provided for the `prost_reflect::ReflectMessage` trait:
/// ```rust
///  use prost_reflect_validate::ValidatorExt;
///  use crate::proto::ExampleMessage;
///  
///  match ExampleMessage::default().validate() {
///  Ok(_) => println!("Validation passed"),
///  Err(e) => eprintln!("Validation failed: {}", e),
///  }
///  let msg = ExampleMessage{content: "Hello, world!".to_string()};
///  match msg.validate() {
///  Ok(_) => println!("Validation passed"),
///  Err(e) => eprintln!("Validation failed: {}", e),
///  }
/// ```
pub trait ValidatorExt {
    fn validate(&self) -> anyhow::Result<()>;
}

impl<T: ReflectMessage> ValidatorExt for T {
    fn validate(&self) -> anyhow::Result<()> {
        let msg = self.transcode_to_dynamic();
        REGISTRY.validate(&msg)
    }
}