proto-types 0.2.2

⚙️ Implementations for various common protobuf types.
Documentation
use crate::field_descriptor_proto::Type as ProtoType;

/// 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.
#[derive(Clone, Debug, PartialEq, Eq, Copy)]
#[non_exhaustive]
pub enum FieldType {
	Double,
	Float,
	Int64,
	Uint64,
	Int32,
	Fixed64,
	Fixed32,
	Bool,
	String,
	Bytes,
	Uint32,
	Enum,
	Sfixed32,
	Sfixed64,
	Sint32,
	Sint64,
	Group,
	Message,
	Duration,
	Timestamp,
	Any,
	FieldMask,
}

impl FieldType {
	#[must_use]
	#[inline]
	pub const fn is_scalar(&self) -> bool {
		matches!(
			self,
			Self::Double
				| Self::Float
				| Self::Int64
				| Self::Uint64
				| Self::Int32
				| Self::Fixed32
				| Self::Fixed64
				| Self::Bool | Self::String
				| Self::Bytes
				| Self::Uint32
				| Self::Sfixed32
				| Self::Sfixed64
				| Self::Sint32
				| Self::Sint64
		)
	}

	/// Returns the short name for the field type. For message types such as `Timestamp`, it returns just the short name without the package path.
	#[must_use]
	#[inline]
	pub const fn name(&self) -> &'static str {
		match self {
			Self::Double => "double",
			Self::Float => "float",
			Self::Int64 => "int64",
			Self::Uint64 => "uint64",
			Self::Int32 => "int32",
			Self::Fixed64 => "fixed64",
			Self::Fixed32 => "fixed32",
			Self::Bool => "bool",
			Self::String => "string",
			Self::Bytes => "bytes",
			Self::Uint32 => "uint32",
			Self::Enum => "enum",
			Self::Sfixed32 => "sfixed32",
			Self::Sfixed64 => "sfixed64",
			Self::Sint32 => "sint32",
			Self::Sint64 => "sint64",
			Self::Group => "group",
			Self::Message => "message",
			Self::Duration => "Duration",
			Self::Timestamp => "Timestamp",
			Self::Any => "Any",
			Self::FieldMask => "FieldMask",
		}
	}

	/// Returns the full name for the field type, using the
	/// fully-qualified name for Google's well-known types.
	#[must_use]
	#[inline]
	pub const fn full_name(&self) -> &'static str {
		match self {
			// Special cases for well-known types
			Self::Duration => "google.protobuf.Duration",
			Self::Timestamp => "google.protobuf.Timestamp",
			Self::Any => "google.protobuf.Any",
			Self::FieldMask => "google.protobuf.FieldMask",

			// For all other types, it falls back to the short name.
			// The `_` arm catches all variants not matched above.
			_ => self.name(),
		}
	}
}

impl From<FieldType> for ProtoType {
	#[inline]
	fn from(value: FieldType) -> Self {
		match value {
			FieldType::Double => Self::Double,
			FieldType::Float => Self::Float,
			FieldType::Int32 => Self::Int32,
			FieldType::Int64 => Self::Int64,
			FieldType::Uint32 => Self::Uint32,
			FieldType::Uint64 => Self::Uint64,
			FieldType::Sint32 => Self::Sint32,
			FieldType::Sint64 => Self::Sint64,
			FieldType::Fixed32 => Self::Fixed32,
			FieldType::Fixed64 => Self::Fixed64,
			FieldType::Sfixed32 => Self::Sfixed32,
			FieldType::Sfixed64 => Self::Sfixed64,
			FieldType::Bool => Self::Bool,
			FieldType::String => Self::String,
			FieldType::Bytes => Self::Bytes,
			FieldType::Message
			| FieldType::Duration
			| FieldType::Timestamp
			| FieldType::Any
			| FieldType::FieldMask => Self::Message,
			FieldType::Enum => Self::Enum,
			FieldType::Group => Self::Group,
		}
	}
}

impl From<ProtoType> for FieldType {
	#[inline]
	fn from(value: ProtoType) -> Self {
		match value {
			ProtoType::Double => Self::Double,
			ProtoType::Float => Self::Float,
			ProtoType::Int32 => Self::Int32,
			ProtoType::Int64 => Self::Int64,
			ProtoType::Uint32 => Self::Uint32,
			ProtoType::Uint64 => Self::Uint64,
			ProtoType::Sint32 => Self::Sint32,
			ProtoType::Sint64 => Self::Sint64,
			ProtoType::Fixed32 => Self::Fixed32,
			ProtoType::Fixed64 => Self::Fixed64,
			ProtoType::Sfixed32 => Self::Sfixed32,
			ProtoType::Sfixed64 => Self::Sfixed64,
			ProtoType::Bool => Self::Bool,
			ProtoType::String => Self::String,
			ProtoType::Bytes => Self::Bytes,
			ProtoType::Message => Self::Message,
			ProtoType::Enum => Self::Enum,
			ProtoType::Group => Self::Group,
		}
	}
}