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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#![macro_use]
use std::fmt::{Debug, Display, Formatter, Result as DisplayResult};
use std::str::{FromStr, SplitN};
#[macro_export]
macro_rules! tab_separated {
($($x:expr),*) => ({
let vec: Vec<String> = vec![$($x.to_string()),*];
vec.join("\t")
})
}
pub trait ToTabSeparatedString {
fn to_tab_separated_string(&self) -> String;
}
pub trait FromTabSeparatedStr {
fn from_tab_separated_str(s: &str) -> Result<Self, ParseError> where Self: Sized;
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ParseError {
ParseError(String),
MissingField(usize)
}
impl Display for ParseError {
fn fmt(&self, f: &mut Formatter) -> DisplayResult {
match *self {
ParseError::ParseError(ref description) => write!(f, "{}", description),
ParseError::MissingField(index) => write!(f, "missing field at index {}", index)
}
}
}
pub struct TabSeparatedParser<'a> {
index: usize,
parts: SplitN<'a, &'a str>
}
impl<'a> TabSeparatedParser<'a> {
pub fn new(n: usize, s: &'a str) -> TabSeparatedParser<'a> {
TabSeparatedParser {
index: 0,
parts: s.splitn(n, "\t")
}
}
pub fn parse_next<T>(&mut self) -> Result<T, ParseError> where T: FromStr, <T as FromStr>::Err: Display + Debug {
match self.parts.next().map(|x| x.parse()) {
Some(Ok(value)) => {
self.index += 1;
Ok(value)
},
Some(Err(err)) => Err(ParseError::ParseError(format!("{}", err))),
None => Err(ParseError::MissingField(self.index))
}
}
}
#[cfg(test)]
mod tests {
use super::super::*;
#[test]
fn test_tab_separated_macro() {
let tab_separated_value = tab_separated!("hello", "world", "!");
assert_eq!(tab_separated_value, "hello\tworld\t!");
let tab_separated_value = tab_separated!(1, 2);
assert_eq!(tab_separated_value, "1\t2");
}
#[test]
fn test_tab_separated_parser() {
let tab_separated_value = tab_separated!("hello", "world", "!");
let mut parser = TabSeparatedParser::new(3, &tab_separated_value);
let hello: String = parser.parse_next().expect("Unable to parse value");
let world: String = parser.parse_next().expect("Unable to parse value");
let exclamation_mark: String = parser.parse_next().expect("Unable to parse value");
assert_eq!(hello, "hello".to_owned());
assert_eq!(world, "world".to_owned());
assert_eq!(exclamation_mark, "!".to_owned());
let tab_separated_value = tab_separated!(1, 2);
let mut parser = TabSeparatedParser::new(2, &tab_separated_value);
let one: u8 = parser.parse_next().expect("Unable to parse value");
let two: u8 = parser.parse_next().expect("Unable to parse value");
assert_eq!(one, 1);
assert_eq!(two, 2);
}
#[test]
fn test_parse_error() {
let tab_separated_value = tab_separated!("hello", "world");
let mut parser = TabSeparatedParser::new(2, &tab_separated_value);
assert_eq!(parser.parse_next::<u8>(), Err(ParseError::ParseError("invalid digit found in string".to_owned())));
}
#[test]
fn test_missing_field_error() {
let tab_separated_value = tab_separated!("hello", "world");
let mut parser = TabSeparatedParser::new(2, &tab_separated_value);
let hello: String = parser.parse_next().expect("Unable to parse value");
let world: String = parser.parse_next().expect("Unable to parse value");
assert_eq!(hello, "hello".to_owned());
assert_eq!(world, "world".to_owned());
assert_eq!(parser.parse_next::<String>(), Err(ParseError::MissingField(2)));
}
}