1use crate::Parse;
2
3use nom::{combinator::map_res, number::complete::le_u8};
4use thiserror::Error;
5
6#[derive(Debug, Error)]
7#[error("Invalid version reported: {value}")]
8pub struct VersionError {
9 value: u8,
10}
11
12#[derive(Debug, PartialEq)]
14pub enum Version {
15 V3,
16 V5,
17}
18
19impl TryFrom<u8> for Version {
20 type Error = VersionError;
22
23 fn try_from(i: u8) -> Result<Self, Self::Error> {
25 match i {
26 0x33 => Ok(Version::V3),
27 0x35 => Ok(Version::V5),
28 value => Err(VersionError { value }),
29 }
30 }
31}
32
33impl<'i> Parse<'i> for Version {
34 fn parse(input: &'i [u8]) -> nom::IResult<&'i [u8], Self> {
39 map_res(le_u8, Version::try_from)(input)
40 }
41}