proto_types/protovalidate/
mod.rs1#![allow(clippy::len_without_is_empty)]
2include!("./buf.validate.rs");
3
4use crate::protovalidate::field_path_element::Subscript;
5
6#[cfg(feature = "protocheck")]
7mod length_rules;
8
9#[cfg(feature = "protocheck")]
10pub use length_rules::*;
11
12#[cfg(feature = "protocheck")]
13mod substring_rules;
14
15#[cfg(feature = "protocheck")]
16pub use substring_rules::*;
17
18#[cfg(feature = "protocheck")]
19mod comparable_rules;
20
21#[cfg(feature = "protocheck")]
22mod containing_rules;
23
24#[cfg(feature = "protocheck")]
25mod into_comparable;
26
27#[cfg(feature = "protocheck")]
28mod numeric_rules;
29
30#[cfg(feature = "protocheck")]
31mod rule_matching;
32
33#[cfg(feature = "protocheck")]
34mod const_rules;
35
36#[cfg(feature = "protocheck")]
37pub use const_rules::*;
38
39mod violations;
40
41use std::fmt::{self, Display};
42
43#[cfg(feature = "protocheck")]
44pub use comparable_rules::*;
45#[cfg(feature = "protocheck")]
46pub use containing_rules::{ContainingRules, ItemList};
47#[cfg(feature = "protocheck")]
48pub use numeric_rules::NumericRules;
49
50impl Display for Subscript {
51 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52 match self {
53 Subscript::Index(val) => write!(f, "{}", val),
54 Subscript::BoolKey(val) => write!(f, "{}", val),
55 Subscript::IntKey(val) => write!(f, "{}", val),
56 Subscript::UintKey(val) => write!(f, "{}", val),
57 Subscript::StringKey(val) => write!(f, "{}", val),
58 }
59 }
60}
61
62#[cfg(feature = "totokens")]
63mod totokens {
64 use proc_macro2::TokenStream;
65 use quote::{quote, ToTokens};
66
67 use crate::protovalidate::{field_path_element::Subscript, FieldPathElement, Ignore};
68
69 impl ToTokens for Ignore {
70 fn to_tokens(&self, tokens: &mut TokenStream) {
71 let path = quote! { ::protocheck::types::protovalidate::Ignore };
72
73 match self {
74 Ignore::Unspecified => tokens.extend(quote! { #path::Unspecified }),
75 Ignore::IfZeroValue => tokens.extend(quote! { #path::IfZeroValue }),
76 Ignore::Always => tokens.extend(quote! { #path::Always }),
77 }
78 }
79 }
80
81 impl ToTokens for Subscript {
82 fn to_tokens(&self, tokens: &mut TokenStream) {
83 match self {
84 Subscript::Index(value) => {
85 tokens.extend(quote! {
86 ::protocheck::types::protovalidate::Subscript::Index(#value)
87 });
88 }
89 Subscript::BoolKey(value) => {
90 tokens.extend(quote! {
91 ::protocheck::types::protovalidate::Subscript::BoolKey(#value)
92 });
93 }
94 Subscript::IntKey(value) => {
95 tokens.extend(quote! {
96 ::protocheck::types::protovalidate::Subscript::IntKey(#value)
97 });
98 }
99 Subscript::UintKey(value) => {
100 tokens.extend(quote! {
101 ::protocheck::types::protovalidate::Subscript::UintKey(#value)
102 });
103 }
104 Subscript::StringKey(value) => {
105 tokens.extend(quote! {
106 ::protocheck::types::protovalidate::Subscript::StringKey(#value)
107 });
108 }
109 }
110 }
111 }
112
113 impl ToTokens for FieldPathElement {
114 fn to_tokens(&self, tokens: &mut TokenStream) {
115 let field_number = &self.field_number;
116 let field_name = &self.field_name;
117 let field_type = &self.field_type;
118 let key_type = &self.key_type;
119 let value_type = &self.value_type;
120 let subscript = &self.subscript;
121
122 let field_name_expr = match field_name {
123 Some(name_str) => quote! { Some(#name_str.clone()) },
124 None => quote! { None },
125 };
126
127 tokens.extend(quote! {
128 ::protocheck::types::protovalidate::FieldPathElement {
129 field_number: #field_number,
130 field_name: #field_name_expr,
131 field_type: #field_type,
132 key_type: #key_type,
133 value_type: #value_type,
134 subscript: #subscript,
135 }
136 });
137 }
138 }
139}