prost/encoding/
wire_type.rs

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