-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmark_file.rs
80 lines (70 loc) · 1.99 KB
/
mark_file.rs
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
use std::{
io::{BufReader, Read, Seek},
num::ParseIntError,
str::FromStr,
};
use nom::{
bytes::complete::tag,
character::complete::{alphanumeric1, digit1, multispace1},
combinator::map_res,
sequence::{delimited, terminated},
Finish, IResult,
};
use rev_lines::RevLines;
use crate::{Error, Mark};
pub(crate) fn get_last_mark<R>(reader: R) -> Result<Option<Mark>, Error>
where
R: Read + Seek,
{
if let Some(line) = RevLines::new(BufReader::new(reader))?
.into_iter()
.find(|line| !line.is_empty())
{
Ok(Some(
Finish::finish(mark_line(&line))
.map_err(|e| Error::MarkParsingError(e.code))?
.1,
))
} else {
Ok(None)
}
}
fn mark_line(input: &str) -> IResult<&str, Mark> {
map_res(
terminated(delimited(tag(":"), digit1, multispace1), alphanumeric1),
|raw| -> Result<Mark, ParseIntError> { Mark::from_str(raw) },
)(input)
}
#[cfg(test)]
mod tests {
use std::io::Cursor;
use super::*;
macro_rules! assert_get_last_mark_error {
($input:expr) => {
assert!(get_last_mark(Cursor::new($input)).is_err());
};
}
macro_rules! assert_get_last_mark_ok {
($input:expr, $want:expr) => {
assert_eq!(get_last_mark(Cursor::new($input)).unwrap(), $want);
};
}
#[test]
fn test_get_last_mark() {
assert_get_last_mark_ok!(b"", None);
assert_get_last_mark_ok!(b"\n", None);
assert_get_last_mark_ok!(
b":25 0123456789012345678901234567890123456789",
Some(Mark(25))
);
assert_get_last_mark_ok!(
b":25 0123456789012345678901234567890123456789\n\n",
Some(Mark(25))
);
assert_get_last_mark_error!(b"not a mark");
assert_get_last_mark_error!(b":xx xx");
assert_get_last_mark_error!(b":25");
assert_get_last_mark_error!(b":25 \n");
assert_get_last_mark_error!(b"25 xx");
}
}