feophantlib/engine/io/page_formats/
page_id.rs

1//! A struct to uniquely identify a page in all operations. This replaces adding additional arguments everywhere.
2
3use nom::{
4    bytes::complete::tag_no_case,
5    error::{convert_error, make_error, ContextError, ErrorKind, ParseError, VerboseError},
6    Finish, IResult,
7};
8use std::{
9    fmt::{self, Display, Formatter},
10    str::FromStr,
11};
12use thiserror::Error;
13use uuid::Uuid;
14
15use crate::engine::io::block_layer::ResourceFormatter;
16
17#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
18pub struct PageId {
19    pub resource_key: Uuid,
20    pub page_type: PageType,
21}
22
23impl fmt::Display for PageId {
24    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
25        writeln!(f, "{}", ResourceFormatter::format_uuid(&self.resource_key))?;
26        writeln!(f, "{}", self.page_type)
27    }
28}
29
30#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
31pub enum PageType {
32    Data,
33    FreeSpaceMap,
34    //VisibilityMap
35}
36
37impl PageType {
38    pub fn parse_type<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
39        input: &'a str,
40    ) -> IResult<&'a str, PageType, E> {
41        let (input, matched) = tag_no_case("data")(input)?;
42
43        let page_type = match matched {
44            "data" => PageType::Data,
45            _ => {
46                return Err(nom::Err::Failure(make_error(input, ErrorKind::Fix)));
47            }
48        };
49        Ok((input, page_type))
50    }
51}
52
53impl Display for PageType {
54    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
55        match self {
56            PageType::Data => write!(f, "data"),
57            PageType::FreeSpaceMap => write!(f, "fs"),
58        }
59    }
60}
61
62impl FromStr for PageType {
63    type Err = PageTypeError;
64    fn from_str(s: &str) -> Result<Self, Self::Err> {
65        match Self::parse_type::<VerboseError<&str>>(s).finish() {
66            Ok((_, page_type)) => Ok(page_type),
67            Err(e) => Err(PageTypeError::ParseError(convert_error(s, e))),
68        }
69    }
70}
71
72#[derive(Debug, Error)]
73pub enum PageTypeError {
74    #[error("Page Type Parse Error {0}")]
75    ParseError(String),
76}