v1.3
This commit is contained in:
parent
66309d81db
commit
09cff01e0a
5 changed files with 41 additions and 24 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -4,4 +4,4 @@ version = 3
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "eyes"
|
name = "eyes"
|
||||||
version = "1.3.1"
|
version = "1.3.0"
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
[package]
|
[package]
|
||||||
name = "eyes"
|
name = "eyes"
|
||||||
version = "1.3.1"
|
version = "1.3.0"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
description = "A simpler way to parse using human-readable templates"
|
description = "Parse and convert strings using human-readable templates."
|
||||||
license="BSD-3-Clause"
|
license="BSD-3-Clause"
|
||||||
homepage = "https://git.palmoe.dk/vodofrede/eyes/"
|
homepage = "https://git.palmoe.dk/vodofrede/eyes/"
|
||||||
repository = "https://git.palmoe.dk/vodofrede/eyes/"
|
repository = "https://git.palmoe.dk/vodofrede/eyes/"
|
||||||
|
|
34
README.md
34
README.md
|
@ -1,6 +1,6 @@
|
||||||
# eyes
|
# eyes
|
||||||
|
|
||||||
A simpler way to parse using human-readable templates.
|
Parse and convert strings using human-readable templates.
|
||||||
|
|
||||||
The crate's primary purpose is parsing challenge inputs for [Advent of Code](https://adventofcode.com) challenges. It currently provides limited functionality, but more options may be added provided they are useful additions for parsing slightly more complicated formats.
|
The crate's primary purpose is parsing challenge inputs for [Advent of Code](https://adventofcode.com) challenges. It currently provides limited functionality, but more options may be added provided they are useful additions for parsing slightly more complicated formats.
|
||||||
|
|
||||||
|
@ -13,32 +13,42 @@ The only special characters in templates are curly brackets ('{}'). These act as
|
||||||
## Examples:
|
## Examples:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
#[macro_use] extern crate eyes; // not normally necessary
|
use eyes::parse;
|
||||||
fn main() {
|
|
||||||
let input = "#lol @ 338,7643: 20.2x24.5";
|
let input = "#lol @ 338,7643: 20.2x24.5";
|
||||||
let template = "#{} @ {},{}: {}x{}";
|
let template = "#{} @ {},{}: {}x{}";
|
||||||
|
let (id, x, y, w, h) = parse!(input, template, String, isize, isize, f64, f64);
|
||||||
let (id, x, y, w, h) = eyes::parse!(input, template, String, isize, isize, f64, f64);
|
|
||||||
|
|
||||||
assert_eq!((id.as_str(), x, y, w, h), ("lol", 338, 7643, 20.2, 24.5));
|
assert_eq!((id.as_str(), x, y, w, h), ("lol", 338, 7643, 20.2, 24.5));
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**eyes** will try to expand capture groups, so that the following example also works as expected:
|
**eyes** will match capture groups greedily and expand them as far as possible, so that the following example also works as expected:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
#[macro_use] extern crate eyes; // not normally necessary
|
use eyes::parse;
|
||||||
fn main() {
|
|
||||||
let input = "turn off 660,55 through 986,197";
|
let input = "turn off 660,55 through 986,197";
|
||||||
let template = "{} {},{} through {},{}";
|
let template = "{} {},{} through {},{}";
|
||||||
|
let (op, x1, y1, x2, y2) = parse!(input, template, String, usize, usize, usize, usize);
|
||||||
let (op, x1, y1, x2, y2) = eyes::parse!(input, template, String, usize, usize, usize, usize);
|
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
(op.as_str(), x1, y1, x2, y2),
|
(op.as_str(), x1, y1, x2, y2),
|
||||||
("turn off", 660, 55, 986, 197)
|
("turn off", 660, 55, 986, 197)
|
||||||
);
|
);
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Notice that "turn off" is captured correctly, even though it contains a space.
|
Notice that "turn off" is captured correctly, even though it contains a space.
|
||||||
|
|
||||||
|
For error handling, the [`try_parse`] macro is provided which can be very useful in parsing potentially malformed input:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use eyes::try_parse;
|
||||||
|
|
||||||
|
let input = "1 2\n3,4\n5 6";
|
||||||
|
let result = input
|
||||||
|
.lines()
|
||||||
|
.filter_map(|line| try_parse!(line, "{} {}", i64, i64))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
assert_eq!(vec![(1, 2), (5, 6)], result);
|
||||||
|
```
|
||||||
|
|
|
@ -1,7 +1,12 @@
|
||||||
|
use eyes::parse;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
if let Some((a, b, c)) = eyes::try_parse!("1 2,3", "{} {},{}", u8, u8, u8) {
|
if let Some((a, b, c)) = eyes::try_parse!("1 2,3", "{} {},{}", u8, u8, u8) {
|
||||||
assert!(a == 1 && b == 2 && c == 3);
|
assert!(a == 1 && b == 2 && c == 3);
|
||||||
} else {
|
} else {
|
||||||
unreachable!("This should not happen, as the pattern is matchable to the input");
|
unreachable!("This should not happen, as the pattern is matchable to the input");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let input = "1,2, 3";
|
||||||
|
parse!(input, "{},{}, {}", i64, i64, i64);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
/// A list of captures, created by calling [`Captures::new()`] with the input and template strings.
|
/// A list of captures, created by calling [`Captures::new()`] with the input and template strings.
|
||||||
///
|
///
|
||||||
/// An easier way to use this struct is with the [`eyes::parse`] and [`eyes::try_parse`] macros, which allow for automatic type conversion of captures.
|
/// An easier way to use this struct is with the [`parse`] and [`try_parse`] macros, which allow for automatic type conversion of captures.
|
||||||
pub struct Captures<'a> {
|
pub struct Captures<'a> {
|
||||||
captures: Vec<&'a str>,
|
captures: Vec<&'a str>,
|
||||||
}
|
}
|
||||||
|
@ -137,7 +137,9 @@ macro_rules! try_parse {
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! parse {
|
macro_rules! parse {
|
||||||
($input: expr, $pattern: tt, $($type:ty),*) => {
|
($input: expr, $pattern: tt, $($type:ty),*) => {
|
||||||
|
{
|
||||||
$crate::try_parse!($input, $pattern, $($type),*).unwrap()
|
$crate::try_parse!($input, $pattern, $($type),*).unwrap()
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue