protovalidate_buffa/
error.rs1use std::borrow::Cow;
2use std::fmt;
3
4#[derive(Debug, Clone, Default)]
5pub struct ValidationError {
6 pub violations: Vec<Violation>,
7}
8
9#[derive(Debug, Clone)]
10pub struct Violation {
11 pub field: FieldPath,
12 pub rule: FieldPath,
13 pub rule_id: Cow<'static, str>,
14 pub message: Cow<'static, str>,
15 pub for_key: bool,
16}
17
18#[derive(Debug, Clone, Default)]
19pub struct FieldPath {
20 pub elements: Vec<FieldPathElement>,
21}
22
23#[derive(Debug, Clone)]
24pub struct FieldPathElement {
25 pub field_number: Option<i32>,
26 pub field_name: Option<Cow<'static, str>>,
27 pub field_type: Option<FieldType>,
28 pub subscript: Option<Subscript>,
29}
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub enum FieldType {
33 Double,
34 Float,
35 Int64,
36 Uint64,
37 Int32,
38 Fixed64,
39 Fixed32,
40 Bool,
41 String,
42 Group,
43 Message,
44 Bytes,
45 Uint32,
46 Enum,
47 Sfixed32,
48 Sfixed64,
49 Sint32,
50 Sint64,
51}
52
53#[derive(Debug, Clone)]
54pub enum Subscript {
55 Index(u64),
56 BoolKey(bool),
57 IntKey(i64),
58 UintKey(u64),
59 StringKey(Cow<'static, str>),
60}
61
62impl fmt::Display for FieldPath {
63 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64 let mut first = true;
65 for el in &self.elements {
66 if let Some(name) = &el.field_name {
67 if !first {
68 f.write_str(".")?;
69 }
70 f.write_str(name)?;
71 first = false;
72 }
73 if let Some(sub) = &el.subscript {
74 match sub {
75 Subscript::Index(i) => write!(f, "[{i}]")?,
76 Subscript::BoolKey(b) => write!(f, "[{b}]")?,
77 Subscript::IntKey(i) => write!(f, "[{i}]")?,
78 Subscript::UintKey(u) => write!(f, "[{u}]")?,
79 Subscript::StringKey(s) => write!(f, "[\"{}\"]", s.escape_default())?,
80 }
81 }
82 }
83 Ok(())
84 }
85}
86
87impl fmt::Display for ValidationError {
88 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89 let mut first = true;
90 for v in &self.violations {
91 if !first {
92 f.write_str("; ")?;
93 }
94 write!(f, "{}: {} [{}]", v.field, v.message, v.rule_id)?;
95 first = false;
96 }
97 Ok(())
98 }
99}
100
101impl std::error::Error for ValidationError {}