Skip to main content

protify/
extension.rs

1use crate::*;
2
3/// A struct representing a protobuf Extension.
4#[derive(Debug, PartialEq, Builder)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6#[non_exhaustive]
7pub struct Extension {
8	pub target: ExtensionTarget,
9	pub fields: Vec<Field>,
10}
11
12/// Implemented by the [`proto_extension`] macro.
13pub trait ProtoExtension {
14	fn proto_schema() -> Extension;
15}
16
17/// Valid extension targets for a Proto3 file.
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19#[allow(clippy::enum_variant_names)]
20#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
21pub enum ExtensionTarget {
22	FileOptions,
23	MessageOptions,
24	FieldOptions,
25	OneofOptions,
26	EnumOptions,
27	EnumValueOptions,
28	ServiceOptions,
29	MethodOptions,
30}
31
32impl ExtensionTarget {
33	/// Returns the string representation.
34	#[must_use]
35	pub const fn as_str(&self) -> &'static str {
36		match self {
37			Self::FileOptions => "google.protobuf.FileOptions",
38			Self::MessageOptions => "google.protobuf.MessageOptions",
39			Self::FieldOptions => "google.protobuf.FieldOptions",
40			Self::OneofOptions => "google.protobuf.OneofOptions",
41			Self::EnumOptions => "google.protobuf.EnumOptions",
42			Self::EnumValueOptions => "google.protobuf.EnumValueOptions",
43			Self::ServiceOptions => "google.protobuf.ServiceOptions",
44			Self::MethodOptions => "google.protobuf.MethodOptions",
45		}
46	}
47}
48
49impl Display for ExtensionTarget {
50	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
51		write!(f, "{}", self.as_str())
52	}
53}