-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
style: Move wire type to separate module
While the `encoding` module is undocumented and not stable, there are known users. Leave a alias in place to prevent breaking them.
- Loading branch information
1 parent
8634d3f
commit 997de1d
Showing
5 changed files
with
58 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use crate::DecodeError; | ||
use alloc::format; | ||
|
||
/// Represent the wire type for protobuf encoding. | ||
/// | ||
/// The integer value is equvilant with the encoded value. | ||
#[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
#[repr(u8)] | ||
pub enum WireType { | ||
Varint = 0, | ||
SixtyFourBit = 1, | ||
LengthDelimited = 2, | ||
StartGroup = 3, | ||
EndGroup = 4, | ||
ThirtyTwoBit = 5, | ||
} | ||
|
||
impl TryFrom<u64> for WireType { | ||
type Error = DecodeError; | ||
|
||
#[inline] | ||
fn try_from(value: u64) -> Result<Self, Self::Error> { | ||
match value { | ||
0 => Ok(WireType::Varint), | ||
1 => Ok(WireType::SixtyFourBit), | ||
2 => Ok(WireType::LengthDelimited), | ||
3 => Ok(WireType::StartGroup), | ||
4 => Ok(WireType::EndGroup), | ||
5 => Ok(WireType::ThirtyTwoBit), | ||
_ => Err(DecodeError::new(format!( | ||
"invalid wire type value: {}", | ||
value | ||
))), | ||
} | ||
} | ||
} | ||
|
||
/// Checks that the expected wire type matches the actual wire type, | ||
/// or returns an error result. | ||
#[inline] | ||
pub fn check_wire_type(expected: WireType, actual: WireType) -> Result<(), DecodeError> { | ||
if expected != actual { | ||
return Err(DecodeError::new(format!( | ||
"invalid wire type: {:?} (expected {:?})", | ||
actual, expected | ||
))); | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters