scan-rules
This crate provides some macros for quickly parsing values out of text. Roughly speaking, it does the inverse of the print!
/format!
macros; or, in other words, a similar job to scanf
from C.
The macros of interest are:
readln!
- reads and scans a line from standard input.try_readln!
- likereadln!
, except it returns aResult
instead of panicking.scan!
- scans the provided string.
Plus a convenience macro:
let_scan!
- scans a string and binds captured values directly to local variables. Only supports one pattern and panics if it doesn't match.
If you are interested in implementing support for your own types, see the ScanFromStr
trait.
The available abstract scanners can be found in the scanner
module.
Links
Compatibility
v0.1.0 was tested against rustc
versions 1.3.0-1.6.0, 1.7.0-beta.1, and nightly 2016-01-20.
-
rustc
< 1.7 will have only concrete implementations ofScanFromStr
for theEverything
,Ident
,Line
,NonSpace
,Number
,Word
, andWordish
scanners for&str
andString
output types. 1.7 and higher will have generic implementations for all output types such that&str: Into<Output>
. -
rustc
< 1.5 will not support scanning theSocketAddrV4
andSocketAddrV6
types, due to missingFromStr
implementations. -
rustc
< 1.4 will not support scanning theIpv4Addr
,Ipv6Addr
orSocketAddr
types, due to theirFromStr
implementations producing errors that do not implementError
. -
rustc
< 1.3 is explicitly not supported, due to limitations in the macro syntax.
Quick Examples
Here is a simple CLI program that asks the user their name and age. You can run this using cargo run --example ask_age
.
extern crate scan_rules;
use Word;
This example shows how to parse one of several different syntaxes. You can run this using cargo run --example scan_data
.
extern crate scan_rules;
use BTreeSet;
// `Word` is an "abstract" scanner; rather than scanning itself, it scans some
// *other* type using custom rules. In this case, it scans a word into a
// string slice. You can use `Word<String>` to get an owned string.
use Word;
This example demonstrates using runtime scanners and the let_scan!
convenience macro. You can run this using cargo run --example runtime_scanners
.
//! **NOTE**: requires the `regex` feature.
extern crate scan_rules;
License
Licensed under either of
- MIT license (see LICENSE or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0 (see LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be dual licensed as above, without any additional terms or conditions.