-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
213 lines (190 loc) · 6.3 KB
/
lib.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#![warn(missing_docs)]
//! Provides a single macro `parseln!` as a counterpart to `println!`, parsing text with a similar syntax used for printing.
/// Internal function to capture parts of a string according to a pattern.
#[doc(hidden)]
pub fn split_line<'a>(string: &'a str, pattern: &str) -> Result<Vec<&'a str>, &'static str> {
let mut results = vec![];
let mut last_idx = 0;
let mut first = true;
let splits: Vec<&str> = pattern.split("{}").collect();
for i in 0..splits.len() {
if splits[i].chars().count() == 0 {
if i + 1 == splits.len() {
results.push(&string[last_idx..]);
continue;
} else if i != 0 {
return Err("Invalid pattern, found consecutive captures {}{}.");
}
}
match string.get(last_idx..).and_then(|s| s.find(splits[i])) {
Some(idx) => {
let idx = last_idx + idx;
if !first {
results.push(&string[last_idx..idx]);
}
last_idx = idx + splits[i].chars().count();
first = false;
}
None => return Err("Could not match pattern to string."),
}
}
Ok(results)
}
/// A println! counterpart for simple parsing problems.
///
/// It uses a pattern where {} captures something, and is parsed to the type of the supplied variable.
/// If two consecutive captures are included in the patter the call will panic.
/// If too few variables are supplied the remaining captures are dropped, if too many are supplied the call will panic.
///
/// ## Examples
/// It can be used either with already defined variables as
/// ```rust
/// # use parseline::parseln;
/// let month: String;
/// let day: isize;
/// parseln!("Date: apr 13", "Date: {} {}", month, day);
/// assert_eq!((month, day), (String::from("apr"), 13))
/// ```
/// or by generating new binding, though then we need to supply the type to be parsed
/// ```rust
/// # use parseline::parseln;
/// parseln!("Date: apr 13", "Date: {} {}", month: String, day: i32);
/// assert_eq!((month, day), (String::from("apr"), 13))
/// ```
#[macro_export]
macro_rules! parseln {
($line:expr, $pattern:expr) => {
$crate::split_line($line, $pattern).expect("Failed to parse")
};
($line:expr, $pattern:expr, $($var:ident),+) => {
{
let result = match $crate::split_line($line, $pattern) {
Ok(x) => x,
Err(e) => panic!("Parsing error: {}", e),
};
let mut result_iter = result.iter();
$(
$var = result_iter.next().expect("Too many variables").parse().expect("Incorrect type for captured variable");
)+
}
};
($line:expr, $pattern:expr, $($var:ident:$type:ty),+) => {
$(let $var: $type);+;
parseln!($line, $pattern, $($var),+);
/*
let ($($var),+) = { // why does result not get overwritten even without this?
let result = match $crate::split_line($line, $pattern) {
Ok(x) => x,
Err(e) => panic!("Parsing error: {}", e),
};
let mut result_iter = result.iter();
$(
let $var: $type = result_iter.next().expect("Too many variables").parse().expect("Incorrect type for captured variable");
)+
($($var),+)
};*/
};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_split_line_empty_corners() {
let result = split_line("@23x", "{}@{}x{}").unwrap();
assert_eq!(result, vec!["", "23", ""]);
}
#[test]
fn test_split_line_with_corners() {
let result = split_line("a@23x*", "{}@{}x{}").unwrap();
assert_eq!(result, vec!["a", "23", "*"]);
}
#[test]
fn test_macro_vec() {
let result = parseln!("#1 @ c,1.44: firstxtrue", "#{} @ {},{}: {}x{}");
assert_eq!(result, vec!["1", "c", "1.44", "first", "true"]);
}
#[test]
fn test_macro_external_vars() {
let a: u32;
let b: char;
let c: f32;
let d: String;
let e: bool;
parseln!(
"#1 @ c,1.44: firstxtrue",
"#{} @ {},{}: {}x{}",
a,
b,
c,
d,
e
);
assert_eq!(a, 1);
assert_eq!(b, 'c');
assert_eq!(c, 1.44);
assert_eq!(d, "first");
assert!(e);
}
#[test]
fn test_macro_inside_vars() {
parseln!(
"#1 @ c,1.44: firstxtrue",
"#{} @ {},{}: {}x{}",
a: i32,
b: char,
c: f32,
d: String,
e: bool
);
assert_eq!(a, 1);
assert_eq!(b, 'c');
assert_eq!(c, 1.44);
assert_eq!(d, "first");
assert!(e);
}
#[test]
fn test_macro_text_var() {
let text = "1@a^";
parseln!(text, "{}@{}^{}", a: i32, b: char, c: String);
assert_eq!(a, 1);
assert_eq!(b, 'a');
assert_eq!(c, "");
}
#[test]
fn test_macro_not_overwrite() {
let result = "hello";
parseln!("1@a^", "{}@{}^{}", _a: u32, _b: char, _c: String);
assert_eq!(result, "hello");
}
/*/
#[test]
#[ignore = "not yet implemented"]
fn test_macro_underscore() {
parseln!("#1 @ c,1.44: firstxtrue", "#{} @ {},{}: {}x{}", a: u32, _, c: f32, d: String, e: bool);
assert_eq!(a, 1);
assert_eq!(c, 1.44);
assert_eq!(d, "first");
assert_eq!(e, true);
}
*/
#[test]
#[should_panic(expected = "Invalid pattern, found consecutive captures {}{}")]
fn test_macro_double_capture() {
parseln!("1@a^", "{}@{}{}^{}", _a: u32, _b: char, _c: String);
}
#[test]
#[should_panic(expected = "Too many variables")]
fn test_macro_panic_too_many_variables() {
parseln!("1@a^", "{}@{}^{}", _a: u32, _b: char, _c: String, _d: bool);
}
#[test]
#[should_panic(expected = "Incorrect type for captured variable")]
fn test_macro_panic_incorrect_variable_type() {
parseln!("1@a^", "{}@{}^{}", _a: u32, _b: bool, _c: String);
}
#[test]
#[should_panic(expected = "Could not match pattern to string")]
fn test_macro_panic_parse() {
parseln!("1@a^", "{}#{}^{}", _a: u32, _b: bool, _c: String);
}
}