1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use crate::{shared::NomErrorReason, take_uint, IResult, NomErr, TryFromPrimitiveError};
use nom::{character::complete::char, sequence::tuple};
use num_enum::TryFromPrimitive;
use serde::{Deserialize, Serialize};
use serde_repr::*;
use std::convert::TryFrom;
use thiserror::Error;
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
#[serde(tag = "tag", content = "content")]
pub enum Skip {
Horizontal(SkipHorizontal),
Vertical(SkipVertical),
Enclosure(SkipEnclosure),
Solid(SkipSolid),
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SkipHorizontal {
pub left: u8,
pub right: u8,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SkipVertical {
pub top: u8,
pub bottom: u8,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SkipEnclosure {
pub exterior: u8,
pub interior: u8,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SkipSolid {
pub total_stroke_count: u8,
pub solid_subpattern: SolidSubpattern,
}
#[derive(
TryFromPrimitive,
Debug,
Copy,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Serialize_repr,
Deserialize_repr,
)]
#[repr(u8)]
pub enum SolidSubpattern {
TopLine = 1,
BottomLine,
ThroughLine,
Other,
}
#[derive(Error, Debug, PartialEq, Eq)]
pub enum SkipParseError {
#[error("(Skip) Invalid solid pattern: {0}")]
InvalidSolidPattern(#[from] TryFromPrimitiveError<SolidSubpattern>),
#[error("(Skip) Format: {0}")]
Format(NomErrorReason),
#[error("(Skip) Digit indicating the pattern was not valid")]
SkipKind,
}
impl<'a> From<NomErr<'a>> for SkipParseError {
fn from(err: NomErr<'a>) -> Self {
Self::Format(err.into())
}
}
impl TryFrom<&str> for Skip {
type Error = SkipParseError;
fn try_from(text: &str) -> Result<Self, Self::Error> {
let (_i, (pattern_kind, _, first, _, second)) = parts(text)?;
match pattern_kind {
1 => Ok(Self::Horizontal(SkipHorizontal {
left: first,
right: second,
})),
2 => Ok(Self::Vertical(SkipVertical {
top: first,
bottom: second,
})),
3 => Ok(Self::Enclosure(SkipEnclosure {
exterior: first,
interior: second,
})),
4 => {
let solid_subpattern = SolidSubpattern::try_from(second)?;
Ok(Self::Solid(SkipSolid {
total_stroke_count: first,
solid_subpattern,
}))
}
_ => Err(SkipParseError::SkipKind),
}
}
}
fn parts(s: &str) -> IResult<(u8, char, u8, char, u8)> {
tuple((take_uint, char('-'), take_uint, char('-'), take_uint))(s)
}