#string #parser #parse-input #binary-parser #multiple-values

无需std strp_macros

一个旨在与strp一起使用的宏库

9个稳定版本

3.1.0 2022年11月5日
3.0.0 2022年11月3日
2.1.1 2022年11月3日
1.0.1 2022年11月2日

#multiple-values中排名#23


strp中使用

MIT许可证

21KB
425

strp

一个用于从输入字符串或(如果使用std功能构建)stdin解析数据的实用库。在未启用std功能时支持no_std上下文。需要alloc crate。默认启用std功能。

支持从字符串解析一个或多个值。可以解析基本类型、字符串或任何实现了TryParse特质的类型。

支持从十六进制或二进制值解析基本类型。

try_parseparsetry_scanscan宏高度重视类型推断,意味着除非你想强制指定特定类型或存在缺失上下文,否则很少需要你自己指定类型。

基本的parsetry_parse用法

parsetry_parse从源字符串解析单个值,并且比scantry_scan有更连贯的错误。

// Attempts to parse  a number from `source` using `try_parse`
let source = String::from("number: 30");
let number = try_parse!(source => "number: {}");
assert_eq!(number, Ok(30));

// Internally calls `try_parse` and unwraps the result.
let source = "hello, world!";
let value: String = parse!(source => "hello, {}!");
assert_eq!(value, "world".to_string());

neither parse nor try_parse接受一次解析多个值。以下代码将无法编译。

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`.

基本的scantry_scan用法

scantry_scan的错误比parsetry_parse更不连贯,但允许从单个源字符串解析多个值。

// Example of parsing 4 strings from one source string using `try_scan`
let source = String::from("this is four words!");
let matched = try_scan!(source => "{} {} {} {}!");
assert_eq!(
    matched,
    Ok((
        "this".to_string(),
         "is".to_string(),
         "four".to_string(),
         "words".to_string()
    ))
);

// Interally calls `try_scan` and unwraps the result.
let source = "add 20, 30";
let (left, right): (u32, u32) = scan!(source => "add {}, {}");
assert_eq!(left + right, 50);

Both scan or try_scan需要一次解析两个或更多值。以下代码将无法编译。

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`.

使用std功能与stdin一起使用。

// Uses stdin as a source string.
let name: String = parse!("hello! my name is {}.");
println!("hello, {name}!");

let try_parse: Result<String, _> = try_parse!("Please, enter your name: {}.");
match try_parse {
    Ok(name) => println!("Thank you for inputing your name, {name}!"),
    Err(_) => println!("No name was given."),
}

// You can also use stdin for `scan` and `try_scan`
let (a, b, c): (u32, u32, u32) = scan!("{} + {} = {}");
assert_eq!(a + b, c);

let try_scan: Result<(u32, u32, u32), _> = try_scan!("{} + {} = {}");
match try_scan {
    Ok((a,b,c)) => println!("{a} + {b} = {c}"),
    Err(e) => println!("an erro occured: {e:?}"),
}

内联匹配的值。

let mut number = -1;
try_parse!("input number: 20" => "input number: {number}");
assert_eq!(number, 20);

let (mut l, mut r) = ("".to_string(), "".to_string());
try_scan!("hello world!" => "{l} {r}").expect("failed to parse");
assert_eq!((l, r), ("hello".to_string(), "world!".to_string()));

// If the parsing failed, an error is returned by the macro call.
let mut number: i32 = -1;
match try_parse!("fail 20" => "success {number}"){
    Ok(_) => println!("parsed value: {number}"),
    Err(_) => println!("failed to parse input string"),
}

// Inlining can also be paired with returning values in `scan` and `try_scan`.
let (mut left, mut right) = ("".to_string(), "".to_string());
let middle = scan!("left middle right" => "{left} {} {right}");
assert_eq!(
    (left, middle, right),
    ("left".to_string(), "middle".to_string(), "right".to_string())
);

// `scan` and `try_scan` can mix both inlining matching values,
// or alternatively capture them as a return value.
let (mut x, mut y, mut z) = (0, 0, 0);
let v = try_scan!("10, 20, 30, 40" => "{}, {x}, {y}, {z}");
assert_eq!((v, x, y, z), (Ok(10), 20, 30, 40));

let (mut x, mut y, mut z) = (0, 0, 0);
let v = try_scan!("10, 20, 30, 40" => "{x}, {}, {y}, {z}");
assert_eq!((v, x, y, z), (Ok(20), 10, 30, 40));

let (mut x, mut y, mut z) = (0, 0, 0);
let v = try_scan!("10, 20, 30, 40" => "{x}, {y}, {}, {z}");
assert_eq!((v, x, y, z), (Ok(30), 10, 20, 40));

let (mut x, mut y, mut z) = (0, 0, 0);
let v = try_scan!("10, 20, 30, 40" => "{x}, {y}, {z}, {}");
assert_eq!((v, x, y, z), (Ok(40), 10, 20, 30));

let (mut x, mut y) = (0, 0);
let v = try_scan!("10, 10, 20, 20" => "{x}, {}, {y}, {}");
assert_eq!(v, Ok((x,y)));

十六进制和二进制解析。

// Need to specify 'u64' here, since otherwise the value will be too large.
let hex: Result<u64, _> =
    try_parse!("input hex: 0x0123456789ABCDEF" => "input hex: 0x{:x}");
assert_eq!(hex, Ok(0x0123456789ABCDEF));

let bin: Result<u32, _> = try_parse!("input bin: 0b11110001" => "input bin: 0b{:b}");
assert_eq!(bin, Ok(0b11110001));

let (bin, hex) = scan!("bin: 0b101, hex: 0xFE" => "bin: 0b{:b}, hex: 0x{:x}");
assert_eq!((bin, hex), (0b101u32, 0xFEu32));

// Parsing as hexadecimal or binary also works with inlining.
let mut bin = -1;
parse!("binary value: 101" => "binary value: {bin:b}");
assert_eq!(bin, 0b101);

let (mut bin, mut hex) = (-1, -1);
scan!("bin: 1111, hex: F" => "bin: {bin:b}, hex: {hex:x}");
assert_eq!((bin, hex), (0b1111, 0xF));

许可证:MIT


lib.rs:

strp crate的宏crate。

依赖关系

~1.5MB
~35K SLoC