2 releases

0.1.1 Dec 4, 2022
0.1.0 Nov 20, 2021

#116 in #argument

Download history 24/week @ 2024-02-26 57/week @ 2024-03-04 12/week @ 2024-03-11

93 downloads per month
Used in scanfmt

MIT/Apache

15KB
391 lines

Scanfmt

This crate offers a macro for parsing text.

The macro accepts a format string, and the name of arguments to parse into.

The syntax of the format string literal is quite similar to the format! macro family:

format_string := text [ maybe_format text ] *
maybe_format := '{' '{' | '}' '}' | format
format := '{' [ argument ] [ ':' format_spec ] '}'
argument := integer | identifier

format_spec := 'o' | 'x' | 'X' | 'b'

In the above grammar, text must not contain any '{' or '}' characters.

Usage

use scanfmt::{scanfmt, ScanError};
fn my_format(s: &str) -> Result<(u16, u32), ScanError> {
    let a;
    let b;
    scanfmt!(s, "a: {}, b: {}", a, b);
    Ok((a, b))
}

Note that scanfmt! requires the function to return a result to ensure that variables are always initialized.

Dependencies

~1.5MB
~33K SLoC