protify 0.1.4

A Rust-first protobuf framework to generate packages from rust code, with validation included
Documentation
#[doc(hidden)]
pub mod state;
use crate::validators::*;
pub(crate) use state::*;

/// Builder for [`IntValidator`].
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct IntValidatorBuilder<Num, S = Empty>
where
	S: State,
	Num: IntWrapper,
{
	_state: PhantomData<S>,
	_wrapper: PhantomData<Num>,

	data: IntValidator<Num>,
}

impl<Num, S> Default for IntValidatorBuilder<Num, S>
where
	S: State,
	Num: IntWrapper,
{
	#[inline]
	fn default() -> Self {
		Self {
			_state: PhantomData,
			_wrapper: PhantomData,
			data: IntValidator::default(),
		}
	}
}

impl<S, N> From<IntValidatorBuilder<N, S>> for ProtoOption
where
	S: State,
	N: IntWrapper,
{
	#[inline(never)]
	#[cold]
	fn from(value: IntValidatorBuilder<N, S>) -> Self {
		value.build().into()
	}
}

impl<Num, S> IntValidatorBuilder<Num, S>
where
	S: State,
	Num: IntWrapper,
{
	/// Adds a map with custom error messages to the underlying validator.
	///
	/// If a violation has no custom error message attached to it, it uses the default error message.
	#[inline]
	pub fn with_error_messages(
		mut self,
		error_messages: impl IntoIterator<Item = (Num::ViolationEnum, impl Into<FixedStr>)>,
	) -> IntValidatorBuilder<Num, SetErrorMessages<S>>
	where
		S::ErrorMessages: IsUnset,
	{
		self.data.error_messages = Some(collect_error_messages(error_messages));

		IntValidatorBuilder {
			_state: PhantomData,
			_wrapper: self._wrapper,
			data: self.data,
		}
	}

	/// Specifies that this validator should always be ignored.
	#[inline]
	pub fn ignore_always(mut self) -> IntValidatorBuilder<Num, SetIgnore<S>>
	where
		S::Ignore: IsUnset,
	{
		self.data.ignore = Ignore::Always;

		IntValidatorBuilder {
			_state: PhantomData,
			_wrapper: self._wrapper,
			data: self.data,
		}
	}

	/// Specifies that this validator should be ignored if the value is either unset or equal to its protobuf zero value.
	#[inline]
	pub fn ignore_if_zero_value(mut self) -> IntValidatorBuilder<Num, SetIgnore<S>>
	where
		S::Ignore: IsUnset,
	{
		self.data.ignore = Ignore::IfZeroValue;

		IntValidatorBuilder {
			_state: PhantomData,
			_wrapper: self._wrapper,
			data: self.data,
		}
	}

	/// Adds a [`CelProgram`] to this validator.
	#[inline]
	#[allow(clippy::use_self, clippy::return_self_not_must_use)]
	pub fn cel(mut self, program: CelProgram) -> IntValidatorBuilder<Num, S> {
		self.data.cel.push(program);

		IntValidatorBuilder {
			_state: PhantomData,
			_wrapper: self._wrapper,
			data: self.data,
		}
	}

	/// Specifies that the field must be set (if optional) or not equal to its zero value (if not optional) in order to be valid.
	#[inline]
	pub fn required(mut self) -> IntValidatorBuilder<Num, SetRequired<S>>
	where
		S::Required: IsUnset,
	{
		self.data.required = true;

		IntValidatorBuilder {
			_state: PhantomData,
			_wrapper: self._wrapper,
			data: self.data,
		}
	}

	/// Specifies that only this specific value will be considered valid for this field.
	#[inline]
	pub fn const_(mut self, val: Num::RustType) -> IntValidatorBuilder<Num, SetConst<S>>
	where
		S::Const: IsUnset,
	{
		self.data.const_ = Some(val);

		IntValidatorBuilder {
			_state: PhantomData,
			_wrapper: self._wrapper,
			data: self.data,
		}
	}

	/// Specifies that this field's value will be valid only if it is smaller than the specified amount
	#[inline]
	pub fn lt(mut self, val: Num::RustType) -> IntValidatorBuilder<Num, SetLt<S>>
	where
		S::Lt: IsUnset,
	{
		self.data.lt = Some(val);

		IntValidatorBuilder {
			_state: PhantomData,
			_wrapper: self._wrapper,
			data: self.data,
		}
	}

	/// Specifies that this field's value will be valid only if it is smaller than, or equal to, the specified amount
	#[inline]
	pub fn lte(mut self, val: Num::RustType) -> IntValidatorBuilder<Num, SetLte<S>>
	where
		S::Lte: IsUnset,
	{
		self.data.lte = Some(val);

		IntValidatorBuilder {
			_state: PhantomData,
			_wrapper: self._wrapper,
			data: self.data,
		}
	}

	/// Specifies that this field's value will be valid only if it is greater than the specified amount
	#[inline]
	pub fn gt(mut self, val: Num::RustType) -> IntValidatorBuilder<Num, SetGt<S>>
	where
		S::Gt: IsUnset,
	{
		self.data.gt = Some(val);

		IntValidatorBuilder {
			_state: PhantomData,
			_wrapper: self._wrapper,
			data: self.data,
		}
	}

	/// Specifies that this field's value will be valid only if it is smaller than, or equal to, the specified amount
	#[inline]
	pub fn gte(mut self, val: Num::RustType) -> IntValidatorBuilder<Num, SetGte<S>>
	where
		S::Gte: IsUnset,
	{
		self.data.gte = Some(val);

		IntValidatorBuilder {
			_state: PhantomData,
			_wrapper: self._wrapper,
			data: self.data,
		}
	}

	/// Specifies that the values in this list will be considered NOT valid for this field.
	#[inline]
	pub fn not_in(
		mut self,
		list: impl IntoSortedList<Num::RustType>,
	) -> IntValidatorBuilder<Num, SetNotIn<S>>
	where
		S::NotIn: IsUnset,
	{
		self.data.not_in = Some(list.into_sorted_list());

		IntValidatorBuilder {
			_state: PhantomData,
			_wrapper: self._wrapper,
			data: self.data,
		}
	}

	/// Specifies that only the values in this list will be considered valid for this field.
	#[inline]
	pub fn in_(
		mut self,
		list: impl IntoSortedList<Num::RustType>,
	) -> IntValidatorBuilder<Num, SetIn<S>>
	where
		S::In: IsUnset,
	{
		self.data.in_ = Some(list.into_sorted_list());

		IntValidatorBuilder {
			_state: PhantomData,
			_wrapper: self._wrapper,
			data: self.data,
		}
	}

	/// Builds the validator.
	#[inline]
	pub fn build(self) -> IntValidator<Num> {
		self.data
	}
}