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
use std::fmt;
use crate::records::extension::Extendable;
use crate::util::{ParseError, Time};
#[derive(Debug, PartialEq, Eq)]
pub struct KRecord<'a> {
pub time: Time,
extension_string: &'a str,
}
impl<'a> KRecord<'a> {
pub fn parse(line: &'a str) -> Result<Self, ParseError> {
assert_eq!(line.as_bytes()[0], b'K');
if line.len() <= 7 {
return Err(ParseError::SyntaxError);
}
if !line.is_ascii() {
return Err(ParseError::NonASCIICharacters);
}
let time = line[1..7].parse()?;
let extension_string = &line[7..];
Ok(Self {
time,
extension_string,
})
}
}
impl<'a> Extendable for KRecord<'a> {
const BASE_LENGTH: usize = 7;
fn extension_string(&self) -> &str {
self.extension_string
}
}
impl<'a> fmt::Display for KRecord<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "K{}{}", self.time, self.extension_string)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::records::extension::Extension;
#[test]
fn krecord_parse() {
let sample_string = "K095214FooTheBar";
let parsed = KRecord::parse(sample_string).unwrap();
let expected = KRecord {
time: Time::from_hms(9, 52, 14),
extension_string: "FooTheBar",
};
assert_eq!(parsed, expected);
}
#[test]
fn parse_with_invalid_char_boundary() {
assert!(KRecord::parse("Kኲበ᧞").is_err());
}
#[test]
fn krecord_format() {
let expected_string = "K095214FooTheBar";
let record = KRecord {
time: Time::from_hms(9, 52, 14),
extension_string: "FooTheBar",
};
assert_eq!(format!("{}", record), expected_string);
}
#[test]
fn krecord_extensions() {
let record = KRecord {
time: Time::from_hms(9, 52, 14),
extension_string: "FooTheBar",
};
let ext1 = Extension {
start_byte: 8,
end_byte: 10,
mnemonic: "One",
};
let ext2 = Extension {
start_byte: 11,
end_byte: 13,
mnemonic: "Two",
};
let ext3 = Extension {
start_byte: 14,
end_byte: 16,
mnemonic: "Th3",
};
assert_eq!(record.get_extension(&ext1).unwrap(), "Foo");
assert_eq!(record.get_extension(&ext2).unwrap(), "The");
assert_eq!(record.get_extension(&ext3).unwrap(), "Bar");
}
proptest! {
#[test]
#[allow(unused_must_use)]
fn parse_doesnt_crash(s in "K\\PC*") {
KRecord::parse(&s);
}
}
}