protify 0.1.4

A Rust-first protobuf framework to generate packages from rust code, with validation included
Documentation
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use core::{
	iter::{Copied, Zip},
	slice,
};

use proto_types::Status;

use super::*;

/// Offers information about the subject of the violation: the violation kind (string, int32, bytes, etc) and the field kind (map key, map value, etc).
#[derive(Clone, Debug, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct ViolationMeta {
	pub kind: ViolationKind,
	pub field_kind: FieldKind,
}

impl ViolationMeta {
	/// Creates a new instance, setting `field_kind` to [`FieldKind::Normal`].
	#[inline]
	#[must_use]
	pub const fn new(kind: ViolationKind) -> Self {
		Self {
			kind,
			field_kind: FieldKind::Normal,
		}
	}

	/// Changes the [`FieldKind`] of this instance.
	#[inline]
	#[must_use]
	pub const fn with_field_kind(mut self, field_kind: FieldKind) -> Self {
		self.field_kind = field_kind;
		self
	}
}

/// Holds the rich context concerning the validation errors that occur during validation.
///
/// Can be converted into [`Violations`] to be serialized into protobuf.
///
/// When the feature `tonic` is enabled, it can be converted directly into [`tonic::Status`] so that the `?` operator can be used directly inside [`tonic`] handlers.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ValidationErrors {
	metas: Vec<ViolationMeta>,
	violations: Vec<Violation>,
}

#[cfg(feature = "tonic")]
impl From<ValidationErrors> for tonic::Status {
	#[inline(never)]
	#[cold]
	fn from(value: ValidationErrors) -> Self {
		use ::prost::Message;

		let status_inner: Status = value.into();

		Self::with_details(
			tonic::Code::InvalidArgument,
			"Validation failure",
			Bytes::from(status_inner.encode_to_vec()),
		)
	}
}

/// The context of a specific violation.
///
/// It contains the [`Violation`] data, which is used for protobuf serialization, as well as the [`ViolationMeta`], which contains the extra information about the violation's kind and field kind.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ViolationCtx {
	pub meta: ViolationMeta,
	pub data: Violation,
}

impl From<ViolationCtx> for Violation {
	#[inline]
	fn from(value: ViolationCtx) -> Self {
		value.data
	}
}

impl ViolationCtx {
	/// Creates a new instance.
	#[inline]
	#[must_use]
	pub const fn new(meta: ViolationMeta, data: Violation) -> Self {
		Self { meta, data }
	}

	/// Converts into a [`Violation`].
	#[must_use]
	#[inline]
	pub fn into_violation(self) -> Violation {
		self.into()
	}
}

/// Immutable view for [`ViolationCtx`].
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct ViolationCtxRef<'a> {
	pub meta: ViolationMeta,
	pub data: &'a Violation,
}

impl ViolationCtxRef<'_> {
	/// Transforms this view into an owned instance of [`ViolationCtx`].
	#[must_use]
	#[inline(never)]
	#[cold]
	pub fn into_owned(self) -> ViolationCtx {
		ViolationCtx {
			meta: self.meta,
			data: self.data.clone(),
		}
	}
}

/// Mutable view for [`ViolationCtx`].
#[derive(Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ViolationCtxMut<'a> {
	pub meta: &'a mut ViolationMeta,
	pub data: &'a mut Violation,
}

impl ViolationCtxMut<'_> {
	/// Transforms this view into an owned instance of [`ViolationCtx`].
	#[must_use]
	#[inline(never)]
	#[cold]
	pub fn into_owned(&self) -> ViolationCtx {
		ViolationCtx {
			meta: *self.meta,
			data: self.data.clone(),
		}
	}
}

/// Owned iterator for [`ViolationCtx`].
#[derive(Clone, Debug)]
pub struct IntoIter {
	iter: core::iter::Zip<alloc::vec::IntoIter<ViolationMeta>, alloc::vec::IntoIter<Violation>>,
}

impl Iterator for IntoIter {
	type Item = ViolationCtx;

	#[inline]
	fn next(&mut self) -> Option<Self::Item> {
		self.iter
			.next()
			.map(|(meta, data)| ViolationCtx { meta, data })
	}

	#[inline]
	fn size_hint(&self) -> (usize, Option<usize>) {
		self.iter.size_hint()
	}
}

impl ExactSizeIterator for IntoIter {
	#[inline]
	fn len(&self) -> usize {
		self.iter.len()
	}
}

/// Ref iterator for [`ViolationCtx`].
#[derive(Clone, Debug)]
pub struct Iter<'a> {
	iter: Zip<Copied<slice::Iter<'a, ViolationMeta>>, slice::Iter<'a, Violation>>,
}

impl<'a> Iterator for Iter<'a> {
	type Item = ViolationCtxRef<'a>;

	#[inline]
	fn next(&mut self) -> Option<Self::Item> {
		self.iter
			.next()
			.map(|(meta, data)| ViolationCtxRef { meta, data })
	}

	#[inline]
	fn size_hint(&self) -> (usize, Option<usize>) {
		self.iter.size_hint()
	}
}

impl ExactSizeIterator for Iter<'_> {
	#[inline]
	fn len(&self) -> usize {
		self.iter.len()
	}
}

/// Mutable ref iterator for [`ViolationCtx`].
#[derive(Debug)]
pub struct IterMut<'a> {
	iter: Zip<core::slice::IterMut<'a, ViolationMeta>, core::slice::IterMut<'a, Violation>>,
}

impl<'a> Iterator for IterMut<'a> {
	type Item = ViolationCtxMut<'a>;

	#[inline]
	fn next(&mut self) -> Option<Self::Item> {
		self.iter
			.next()
			.map(|(meta, data)| ViolationCtxMut { meta, data })
	}

	#[inline]
	fn size_hint(&self) -> (usize, Option<usize>) {
		self.iter.size_hint()
	}
}

impl ExactSizeIterator for IterMut<'_> {
	#[inline]
	fn len(&self) -> usize {
		self.iter.len()
	}
}

impl From<ValidationErrors> for Violations {
	#[inline]
	fn from(value: ValidationErrors) -> Self {
		Self {
			violations: value.violations,
		}
	}
}

impl From<ValidationErrors> for Vec<Violation> {
	#[inline]
	fn from(value: ValidationErrors) -> Self {
		value.violations
	}
}

impl From<ValidationErrors> for Status {
	#[inline]
	#[cold]
	fn from(value: ValidationErrors) -> Self {
		value.into_violations().into()
	}
}

impl IntoIterator for ValidationErrors {
	type IntoIter = IntoIter;
	type Item = ViolationCtx;

	#[inline]
	fn into_iter(self) -> Self::IntoIter {
		IntoIter {
			iter: self.metas.into_iter().zip(self.violations),
		}
	}
}

impl<'a> IntoIterator for &'a ValidationErrors {
	type Item = ViolationCtxRef<'a>;

	type IntoIter = Iter<'a>;

	#[inline]
	fn into_iter(self) -> Self::IntoIter {
		Iter {
			iter: self
				.metas
				.iter()
				.copied()
				.zip(self.violations.iter()),
		}
	}
}

impl<'a> IntoIterator for &'a mut ValidationErrors {
	type Item = ViolationCtxMut<'a>;

	type IntoIter = IterMut<'a>;

	#[inline]
	fn into_iter(self) -> Self::IntoIter {
		IterMut {
			iter: self
				.metas
				.iter_mut()
				.zip(self.violations.iter_mut()),
		}
	}
}

impl Extend<ViolationCtx> for ValidationErrors {
	fn extend<T: IntoIterator<Item = ViolationCtx>>(&mut self, iter: T) {
		let iter = iter.into_iter();

		let (lower_bound, _) = iter.size_hint();
		if lower_bound > 0 {
			self.metas.reserve(lower_bound);
			self.violations.reserve(lower_bound);
		}

		for v in iter {
			self.metas.push(v.meta);
			self.violations.push(v.data);
		}
	}
}

impl ValidationErrors {
	/// Appends the contents of another [`ValidationErrors`] to the original instance.
	#[inline]
	pub fn merge(&mut self, other: &mut Self) {
		self.metas.append(&mut other.metas);
		self.violations.append(&mut other.violations);
	}

	/// Returns a ref iterator.
	#[inline]
	#[must_use]
	pub fn iter(&self) -> Iter<'_> {
		self.into_iter()
	}

	/// Returns a mutable ref iterator.
	#[inline]
	pub fn iter_mut(&mut self) -> IterMut<'_> {
		self.into_iter()
	}

	/// Removes the violations that match the given predicate.
	pub fn retain<F>(&mut self, mut f: F)
	where
		F: FnMut(ViolationCtxRef) -> bool,
	{
		let len = self.violations.len();
		let mut keep_count = 0;

		for i in 0..len {
			let should_keep = f(ViolationCtxRef {
				meta: self.metas[i],
				data: &self.violations[i],
			});

			if should_keep {
				if keep_count != i {
					self.metas.swap(keep_count, i);
					self.violations.swap(keep_count, i);
				}
				keep_count += 1;
			}
		}

		self.metas.truncate(keep_count);
		self.violations.truncate(keep_count);
	}

	/// Creates a new instance.
	#[must_use]
	#[inline]
	pub const fn new() -> Self {
		Self {
			metas: vec![],
			violations: vec![],
		}
	}

	/// Turns into [`Status`], the equivalent of the `google.rpc.Status` message.
	#[inline]
	#[must_use]
	pub fn into_status(self) -> Status {
		self.into()
	}

	/// Converts into [`Violations`].
	#[inline]
	#[must_use]
	pub fn into_violations(self) -> Violations {
		Violations {
			violations: self.violations,
		}
	}

	/// Adds a new violation to the list.
	#[inline]
	pub fn push(&mut self, v: ViolationCtx) {
		self.metas.push(v.meta);
		self.violations.push(v.data);
	}

	/// Checks if this instance contains any violations.
	#[inline]
	#[must_use]
	pub const fn is_empty(&self) -> bool {
		self.violations.is_empty()
	}

	/// Returns the amount of violations contained.
	#[inline]
	#[must_use]
	pub const fn len(&self) -> usize {
		self.violations.len()
	}
}

impl Default for ValidationErrors {
	#[inline]
	fn default() -> Self {
		Self::new()
	}
}

#[inline(never)]
#[cold]
pub(crate) fn create_violation_core(
	custom_rule_id: Option<String>,
	field_context: Option<&FieldContext>,
	parent_elements: &[FieldPathElement],
	violation_data: ViolationData,
	error_message: String,
) -> Violation {
	let mut field_elements: Option<Vec<FieldPathElement>> = None;
	let mut rule_elements: Vec<FieldPathElement> = Vec::new();
	let mut is_for_key = false;

	// In case of a top level validator there would be no field path
	if let Some(field_context) = field_context {
		let elements = field_elements.get_or_insert_default();

		elements.extend(parent_elements.iter().cloned());

		let current_elem = field_context.as_path_element();

		elements.push(current_elem);

		match &field_context.field_kind {
			FieldKind::MapKey => {
				is_for_key = true;
				rule_elements.extend(MAP_KEYS_VIOLATION.elements_iter());
			}
			FieldKind::MapValue => rule_elements.extend(MAP_VALUES_VIOLATION.elements_iter()),
			FieldKind::RepeatedItem => {
				rule_elements.extend(REPEATED_ITEMS_VIOLATION.elements_iter())
			}
			_ => {}
		};
	}

	rule_elements.extend(violation_data.elements_iter());

	Violation {
		rule_id: Some(custom_rule_id.unwrap_or_else(|| violation_data.name.to_string())),
		message: Some(error_message),
		for_key: Some(is_for_key),
		field: field_elements.map(|elements| FieldPath { elements }),
		rule: Some(FieldPath {
			elements: rule_elements,
		}),
	}
}