rm_lines/
version.rs

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/// Data representation of the file format version used for a reMarkable document
13#[derive(Debug, PartialEq)]
14pub enum Version {
15    V3,
16    V5,
17}
18
19impl TryFrom<u8> for Version {
20    /// Used to represent a [u8] that does not map to a `Version`
21    type Error = VersionError;
22
23    /// Attempts to map a [u8] value to a known and supported `Version`
24    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    /// Attempts to parse a `Version` from a byte sequence
35    ///
36    /// A version is represented by a value-constrained, little-endian, 8-bit integer,
37    /// contained within a larger, 44-byte ASCII-encoded header.
38    fn parse(input: &'i [u8]) -> nom::IResult<&'i [u8], Self> {
39        map_res(le_u8, Version::try_from)(input)
40    }
41}