use crate::*;
pub trait ProtoEnum: TryFrom<i32> + Copy + Default + Into<i32> + Send + Sync {
fn proto_name() -> &'static str;
#[inline]
fn as_int(self) -> i32 {
self.into()
}
#[inline]
fn is_unspecified(&self) -> bool {
self.as_int() == 0
}
#[inline]
#[must_use]
fn from_int_or_default(int: i32) -> Self {
int.try_into().unwrap_or_default()
}
}
pub trait ProtoEnumSchema: ProtoEnum {
fn proto_path() -> ProtoPath;
fn proto_schema() -> EnumSchema;
fn as_proto_name(&self) -> &'static str;
fn from_proto_name(name: &str) -> Option<Self>;
#[inline]
#[must_use]
fn is_known_variant(int: i32) -> bool {
Self::try_from(int).is_ok()
}
}
#[derive(Debug, Default, Clone, PartialEq, Builder)]
#[cfg_attr(feature = "std", derive(Template))]
#[cfg_attr(feature = "std", template(path = "enum.proto.j2"))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct EnumSchema {
pub short_name: FixedStr,
pub name: FixedStr,
pub package: FixedStr,
pub file: FixedStr,
pub variants: Vec<EnumVariant>,
pub reserved_numbers: Vec<Range<i32>>,
pub reserved_names: Vec<FixedStr>,
pub options: Vec<ProtoOption>,
pub rust_path: FixedStr,
}
impl EnumSchema {
#[cfg(feature = "std")]
#[inline(never)]
#[cold]
pub fn render_schema(&self) -> Result<String, askama::Error> {
self.render()
}
pub(crate) fn render_reserved_names(&self) -> Option<String> {
render_reserved_names(&self.reserved_names)
}
pub(crate) fn render_reserved_numbers(&self) -> Option<String> {
render_reserved_numbers(&self.reserved_numbers)
}
}
#[derive(Debug, Default, Clone, PartialEq, Builder)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct EnumVariant {
pub name: FixedStr,
pub tag: i32,
pub options: Vec<ProtoOption>,
}