strp
Utility library for parsing data from an input string, or stdin if built with the std
feature.
Supports no_std contexts when built without the std
feature enabled. Requires the alloc crate.
The std
feature is enabled by default.
Supports parsing one or multiple values from a string. Can parse primitives, Strings, or any
type which derives the TryParse
trait.
Supports parsing primitives from hexadecimal or binary values.
The try_parse
, parse
, try_scan
and scan
macros put high emphasis on deducing types,
meaning you rarely need to specify the type yourself unless you want to enforce a specific
type, or there's missing context.
Basic parse
and try_parse
usage
parse
and try_parse
parses a single value from the source string,
and has more cohesive errors than scan
and try_scan
.
// Attempts to parse a number from `source` using `try_parse`
let source = String from;
let number = try_parse!;
assert_eq!;
// Internally calls `try_parse` and unwraps the result.
let source = "hello, world!";
let value: String = parse!;
assert_eq!;
Neither parse
or try_parse
accepts anything other than one parsed
value at a time. The following code will not compile.
let source = "some source"
let ok = parse!(source => "{}"); // Ok!
let err = parse!(source => "{} {}"); // Error! Attempting to parse multiple values with `parse`.
let err = parse!(source => "some source"); // Error! Attempting to parse no values using `parse`.
Basic scan
and try_scan
usage
scan
and try_scan
has less cohesive erros than parse
and
try_parse
, but allows parsing multiple values from a single
source string.
// Example of parsing 4 strings from one source string using `try_scan`
let source = String from;
let matched = try_scan!;
assert_eq!;
// Interally calls `try_scan` and unwraps the result.
let source = "add 20, 30";
let : = scan!;
assert_eq!;
Both scan
or try_scan
requires two or more parsed values at a time.
The following code will not compile.
let source = "some source";
let ok = scan!(source => "{} {}"); // Ok!
let err = scan!(source => "{}"); // Error! Attempting to parse a single value with `scan`.
let err = scan!(source => "some source"); // Error! Attempting to parse no values using `scan`.
Using stdin with the std
feature.
// Uses stdin as a source string.
let name: String = parse!;
println!;
let try_parse: = try_parse!;
match try_parse
// You can also use stdin for `scan` and `try_scan`
let : = scan!;
assert_eq!;
let try_scan: = try_scan!;
match try_scan
Inlining matched values.
let mut number = -1;
try_parse!;
assert_eq!;
let = ;
try_scan!.expect;
assert_eq!;
// If the parsing failed, an error is returned by the macro call.
let mut number: i32 = -1;
match try_parse!
// Inlining can also be paired with returning values in `scan` and `try_scan`.
let = ;
let middle = scan!;
assert_eq!;
// `scan` and `try_scan` can mix both inlining matching values,
// or alternatively capture them as a return value.
let = ;
let v = try_scan!;
assert_eq!;
let = ;
let v = try_scan!;
assert_eq!;
let = ;
let v = try_scan!;
assert_eq!;
let = ;
let v = try_scan!;
assert_eq!;
let = ;
let v = try_scan!;
assert_eq!;
Hexadecimal and binary parsing.
// Need to specify 'u64' here, since otherwise the value will be too large.
let hex: =
try_parse!;
assert_eq!;
let bin: = try_parse!;
assert_eq!;
let = scan!;
assert_eq!;
// Parsing as hexadecimal or binary also works with inlining.
let mut bin = -1;
parse!;
assert_eq!;
let = ;
scan!;
assert_eq!;
License: MIT