use crate::{
common::{PosHoriz, PosVert},
parse::{FromUtf8, ParseUtf8},
ErrorKind,
};
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum SheetStyle {
#[default]
A4 = 0,
A3 = 1,
A2 = 2,
A1 = 3,
A0 = 4,
A = 5,
B = 6,
C = 7,
D = 8,
E = 9,
Letter = 10,
Legal = 11,
Tabloid = 12,
OrCadA = 13,
OrCadB = 14,
OrCadC = 15,
OrCadD = 16,
OrCadE = 17,
}
impl TryFrom<u8> for SheetStyle {
type Error = ErrorKind;
fn try_from(value: u8) -> Result<Self, Self::Error> {
let res = match value {
x if x == Self::A4 as u8 => Self::A4,
x if x == Self::A3 as u8 => Self::A3,
x if x == Self::A2 as u8 => Self::A2,
x if x == Self::A1 as u8 => Self::A1,
x if x == Self::A0 as u8 => Self::A0,
x if x == Self::A as u8 => Self::A,
x if x == Self::B as u8 => Self::B,
x if x == Self::C as u8 => Self::C,
x if x == Self::D as u8 => Self::D,
x if x == Self::E as u8 => Self::E,
x if x == Self::Letter as u8 => Self::Letter,
x if x == Self::Legal as u8 => Self::Legal,
x if x == Self::Tabloid as u8 => Self::Tabloid,
x if x == Self::OrCadA as u8 => Self::OrCadA,
x if x == Self::OrCadB as u8 => Self::OrCadB,
x if x == Self::OrCadC as u8 => Self::OrCadC,
x if x == Self::OrCadD as u8 => Self::OrCadD,
x if x == Self::OrCadE as u8 => Self::OrCadE,
_ => return Err(ErrorKind::SheetStyle(value)),
};
Ok(res)
}
}
impl FromUtf8<'_> for SheetStyle {
fn from_utf8(buf: &[u8]) -> Result<Self, ErrorKind> {
let num: u8 = buf.parse_as_utf8()?;
num.try_into()
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum Justification {
#[default]
BottomLeft = 0,
BottomCenter = 1,
BottomRight = 2,
CenterLeft = 3,
CenterCenter = 4,
CenterRight = 5,
TopLeft = 6,
TopCenter = 7,
TopRight = 8,
}
impl TryFrom<u8> for Justification {
type Error = ErrorKind;
fn try_from(value: u8) -> Result<Self, Self::Error> {
let res = match value {
x if x == Self::BottomLeft as u8 => Self::BottomLeft,
x if x == Self::BottomCenter as u8 => Self::BottomCenter,
x if x == Self::BottomRight as u8 => Self::BottomRight,
x if x == Self::CenterLeft as u8 => Self::CenterLeft,
x if x == Self::CenterCenter as u8 => Self::CenterCenter,
x if x == Self::CenterRight as u8 => Self::CenterRight,
x if x == Self::TopLeft as u8 => Self::TopLeft,
x if x == Self::TopCenter as u8 => Self::TopCenter,
x if x == Self::TopRight as u8 => Self::TopRight,
_ => return Err(ErrorKind::Justification(value)),
};
Ok(res)
}
}
impl FromUtf8<'_> for Justification {
fn from_utf8(buf: &[u8]) -> Result<Self, ErrorKind> {
let num: u8 = buf.parse_as_utf8()?;
num.try_into()
}
}
impl From<Justification> for (PosHoriz, PosVert) {
fn from(value: Justification) -> Self {
match value {
Justification::BottomLeft => (PosHoriz::Left, PosVert::Bottom),
Justification::BottomCenter => (PosHoriz::Center, PosVert::Bottom),
Justification::BottomRight => (PosHoriz::Right, PosVert::Bottom),
Justification::CenterLeft => (PosHoriz::Left, PosVert::Middle),
Justification::CenterCenter => (PosHoriz::Center, PosVert::Middle),
Justification::CenterRight => (PosHoriz::Right, PosVert::Middle),
Justification::TopLeft => (PosHoriz::Left, PosVert::Top),
Justification::TopCenter => (PosHoriz::Center, PosVert::Top),
Justification::TopRight => (PosHoriz::Right, PosVert::Top),
}
}
}