#[cfg(feature = "alloc")]
extern crate alloc;
pub mod reader;
#[cfg(feature = "alloc")]
pub mod descriptor;
#[cfg(feature = "alloc")]
pub mod mapper;
#[cfg(feature = "alloc")]
pub mod registry;
use zerodds_types::dynamic::TypeKind;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum ProtoFieldType {
Double = 1,
Float = 2,
Int64 = 3,
Uint64 = 4,
Int32 = 5,
Fixed64 = 6,
Fixed32 = 7,
Bool = 8,
String = 9,
Group = 10,
Message = 11,
Bytes = 12,
Uint32 = 13,
Enum = 14,
Sfixed32 = 15,
Sfixed64 = 16,
Sint32 = 17,
Sint64 = 18,
}
impl ProtoFieldType {
#[must_use]
pub fn from_wire(v: i32) -> Option<Self> {
Some(match v {
1 => Self::Double,
2 => Self::Float,
3 => Self::Int64,
4 => Self::Uint64,
5 => Self::Int32,
6 => Self::Fixed64,
7 => Self::Fixed32,
8 => Self::Bool,
9 => Self::String,
10 => Self::Group,
11 => Self::Message,
12 => Self::Bytes,
13 => Self::Uint32,
14 => Self::Enum,
15 => Self::Sfixed32,
16 => Self::Sfixed64,
17 => Self::Sint32,
18 => Self::Sint64,
_ => return None,
})
}
#[must_use]
pub fn scalar_type_kind(self) -> Option<TypeKind> {
Some(match self {
Self::Double => TypeKind::Float64,
Self::Float => TypeKind::Float32,
Self::Int64 | Self::Sfixed64 | Self::Sint64 => TypeKind::Int64,
Self::Uint64 | Self::Fixed64 => TypeKind::UInt64,
Self::Int32 | Self::Sfixed32 | Self::Sint32 => TypeKind::Int32,
Self::Uint32 | Self::Fixed32 => TypeKind::UInt32,
Self::Bool => TypeKind::Boolean,
Self::String => TypeKind::String8,
Self::Bytes | Self::Message | Self::Group | Self::Enum => return None,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn wire_values_round_trip_the_descriptor_enum() {
assert_eq!(ProtoFieldType::from_wire(1), Some(ProtoFieldType::Double));
assert_eq!(ProtoFieldType::from_wire(11), Some(ProtoFieldType::Message));
assert_eq!(ProtoFieldType::from_wire(18), Some(ProtoFieldType::Sint64));
assert_eq!(ProtoFieldType::from_wire(0), None);
assert_eq!(ProtoFieldType::from_wire(19), None);
}
#[test]
fn scalars_map_to_the_expected_type_kind() {
assert_eq!(
ProtoFieldType::Double.scalar_type_kind(),
Some(TypeKind::Float64)
);
assert_eq!(
ProtoFieldType::Float.scalar_type_kind(),
Some(TypeKind::Float32)
);
for t in [
ProtoFieldType::Int64,
ProtoFieldType::Sfixed64,
ProtoFieldType::Sint64,
] {
assert_eq!(t.scalar_type_kind(), Some(TypeKind::Int64));
}
for t in [
ProtoFieldType::Int32,
ProtoFieldType::Sfixed32,
ProtoFieldType::Sint32,
] {
assert_eq!(t.scalar_type_kind(), Some(TypeKind::Int32));
}
assert_eq!(
ProtoFieldType::Uint64.scalar_type_kind(),
Some(TypeKind::UInt64)
);
assert_eq!(
ProtoFieldType::Fixed32.scalar_type_kind(),
Some(TypeKind::UInt32)
);
assert_eq!(
ProtoFieldType::Bool.scalar_type_kind(),
Some(TypeKind::Boolean)
);
assert_eq!(
ProtoFieldType::String.scalar_type_kind(),
Some(TypeKind::String8)
);
}
#[test]
fn non_scalar_field_types_have_no_direct_kind() {
for t in [
ProtoFieldType::Bytes,
ProtoFieldType::Message,
ProtoFieldType::Group,
ProtoFieldType::Enum,
] {
assert_eq!(t.scalar_type_kind(), None);
}
}
}