prost/encoding/
wire_type.rs

1use crate::DecodeError;
2use alloc::format;
3
4/// Represent the wire type for protobuf encoding.
5///
6/// The integer value is equvilant with the encoded value.
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8#[repr(u8)]
9pub enum WireType {
10    Varint = 0,
11    SixtyFourBit = 1,
12    LengthDelimited = 2,
13    StartGroup = 3,
14    EndGroup = 4,
15    ThirtyTwoBit = 5,
16}
17
18impl TryFrom<u64> for WireType {
19    type Error = DecodeError;
20
21    #[inline]
22    fn try_from(value: u64) -> Result<Self, Self::Error> {
23        match value {
24            0 => Ok(WireType::Varint),
25            1 => Ok(WireType::SixtyFourBit),
26            2 => Ok(WireType::LengthDelimited),
27            3 => Ok(WireType::StartGroup),
28            4 => Ok(WireType::EndGroup),
29            5 => Ok(WireType::ThirtyTwoBit),
30            _ => Err(DecodeError::new(format!(
31                "invalid wire type value: {}",
32                value
33            ))),
34        }
35    }
36}
37
38/// Checks that the expected wire type matches the actual wire type,
39/// or returns an error result.
40#[inline]
41pub fn check_wire_type(expected: WireType, actual: WireType) -> Result<(), DecodeError> {
42    if expected != actual {
43        return Err(DecodeError::new(format!(
44            "invalid wire type: {:?} (expected {:?})",
45            actual, expected
46        )));
47    }
48    Ok(())
49}