-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority.rs
More file actions
58 lines (53 loc) · 1.48 KB
/
priority.rs
File metadata and controls
58 lines (53 loc) · 1.48 KB
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
// use nom::character::complete::char;
// use nom::multi::{many0, many1};
// use nom::sequence::pair;
// use nom::IResult;
// use nom::Parser;
use crate::prelude::*;
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum PriorityComponent {
Padding(usize),
Exclaimation(usize),
}
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Priority((PriorityComponent, PriorityComponent));
impl std::default::Default for Priority {
fn default() -> Self {
Priority((
PriorityComponent::Exclaimation(0usize),
PriorityComponent::Padding(0usize),
))
}
}
pub fn priority<'a>() -> impl FnMut(&'a str) -> IResult<&'a str, Priority, VerboseError<&str>> {
let de = pair(
map(many1(char('.')), |v| PriorityComponent::Padding(v.len())),
map(many0(char('!')), |v| {
PriorityComponent::Exclaimation(v.len())
}),
);
let ed = pair(
map(many1(char('!')), |v| {
PriorityComponent::Exclaimation(v.len())
}),
map(many0(char('.')), |v| PriorityComponent::Padding(v.len())),
);
map(alt((de, ed)), |(com1, com2)| (Priority((com1, com2))))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_basic() {
assert_eq!(
priority()("!..."),
Ok((
"",
Priority((
PriorityComponent::Exclaimation(1),
PriorityComponent::Padding(3)
))
))
);
}
}