Skip to main content

proto_types/
field_type.rs

1use crate::field_descriptor_proto::Type as ProtoType;
2
3/// This is an enhanced enum for protobuf types, which allows precise identification for well known types such as Any, Timestamp or Duration. It is non-exhaustive because other well known types can be added in the future, but it is safe to assume that any non matching variant will be of the generic `Message` type.
4#[derive(Clone, Debug, PartialEq, Eq, Copy)]
5#[non_exhaustive]
6pub enum FieldType {
7	Double,
8	Float,
9	Int64,
10	Uint64,
11	Int32,
12	Fixed64,
13	Fixed32,
14	Bool,
15	String,
16	Bytes,
17	Uint32,
18	Enum,
19	Sfixed32,
20	Sfixed64,
21	Sint32,
22	Sint64,
23	Group,
24	Message,
25	Duration,
26	Timestamp,
27	Any,
28	FieldMask,
29}
30
31impl FieldType {
32	#[must_use]
33	#[inline]
34	pub const fn is_scalar(&self) -> bool {
35		matches!(
36			self,
37			Self::Double
38				| Self::Float
39				| Self::Int64
40				| Self::Uint64
41				| Self::Int32
42				| Self::Fixed32
43				| Self::Fixed64
44				| Self::Bool | Self::String
45				| Self::Bytes
46				| Self::Uint32
47				| Self::Sfixed32
48				| Self::Sfixed64
49				| Self::Sint32
50				| Self::Sint64
51		)
52	}
53
54	/// Returns the short name for the field type. For message types such as `Timestamp`, it returns just the short name without the package path.
55	#[must_use]
56	#[inline]
57	pub const fn name(&self) -> &'static str {
58		match self {
59			Self::Double => "double",
60			Self::Float => "float",
61			Self::Int64 => "int64",
62			Self::Uint64 => "uint64",
63			Self::Int32 => "int32",
64			Self::Fixed64 => "fixed64",
65			Self::Fixed32 => "fixed32",
66			Self::Bool => "bool",
67			Self::String => "string",
68			Self::Bytes => "bytes",
69			Self::Uint32 => "uint32",
70			Self::Enum => "enum",
71			Self::Sfixed32 => "sfixed32",
72			Self::Sfixed64 => "sfixed64",
73			Self::Sint32 => "sint32",
74			Self::Sint64 => "sint64",
75			Self::Group => "group",
76			Self::Message => "message",
77			Self::Duration => "Duration",
78			Self::Timestamp => "Timestamp",
79			Self::Any => "Any",
80			Self::FieldMask => "FieldMask",
81		}
82	}
83
84	/// Returns the full name for the field type, using the
85	/// fully-qualified name for Google's well-known types.
86	#[must_use]
87	#[inline]
88	pub const fn full_name(&self) -> &'static str {
89		match self {
90			// Special cases for well-known types
91			Self::Duration => "google.protobuf.Duration",
92			Self::Timestamp => "google.protobuf.Timestamp",
93			Self::Any => "google.protobuf.Any",
94			Self::FieldMask => "google.protobuf.FieldMask",
95
96			// For all other types, it falls back to the short name.
97			// The `_` arm catches all variants not matched above.
98			_ => self.name(),
99		}
100	}
101}
102
103impl From<FieldType> for ProtoType {
104	#[inline]
105	fn from(value: FieldType) -> Self {
106		match value {
107			FieldType::Double => Self::Double,
108			FieldType::Float => Self::Float,
109			FieldType::Int32 => Self::Int32,
110			FieldType::Int64 => Self::Int64,
111			FieldType::Uint32 => Self::Uint32,
112			FieldType::Uint64 => Self::Uint64,
113			FieldType::Sint32 => Self::Sint32,
114			FieldType::Sint64 => Self::Sint64,
115			FieldType::Fixed32 => Self::Fixed32,
116			FieldType::Fixed64 => Self::Fixed64,
117			FieldType::Sfixed32 => Self::Sfixed32,
118			FieldType::Sfixed64 => Self::Sfixed64,
119			FieldType::Bool => Self::Bool,
120			FieldType::String => Self::String,
121			FieldType::Bytes => Self::Bytes,
122			FieldType::Message
123			| FieldType::Duration
124			| FieldType::Timestamp
125			| FieldType::Any
126			| FieldType::FieldMask => Self::Message,
127			FieldType::Enum => Self::Enum,
128			FieldType::Group => Self::Group,
129		}
130	}
131}
132
133impl From<ProtoType> for FieldType {
134	#[inline]
135	fn from(value: ProtoType) -> Self {
136		match value {
137			ProtoType::Double => Self::Double,
138			ProtoType::Float => Self::Float,
139			ProtoType::Int32 => Self::Int32,
140			ProtoType::Int64 => Self::Int64,
141			ProtoType::Uint32 => Self::Uint32,
142			ProtoType::Uint64 => Self::Uint64,
143			ProtoType::Sint32 => Self::Sint32,
144			ProtoType::Sint64 => Self::Sint64,
145			ProtoType::Fixed32 => Self::Fixed32,
146			ProtoType::Fixed64 => Self::Fixed64,
147			ProtoType::Sfixed32 => Self::Sfixed32,
148			ProtoType::Sfixed64 => Self::Sfixed64,
149			ProtoType::Bool => Self::Bool,
150			ProtoType::String => Self::String,
151			ProtoType::Bytes => Self::Bytes,
152			ProtoType::Message => Self::Message,
153			ProtoType::Enum => Self::Enum,
154			ProtoType::Group => Self::Group,
155		}
156	}
157}