Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fee1-dead/scanfmt
scan a string with given template
https://github.com/fee1-dead/scanfmt
Last synced: about 2 months ago
JSON representation
scan a string with given template
- Host: GitHub
- URL: https://github.com/fee1-dead/scanfmt
- Owner: fee1-dead
- Created: 2021-11-20T13:22:48.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2021-11-20T14:17:29.000Z (about 3 years ago)
- Last Synced: 2024-10-30T15:23:58.438Z (2 months ago)
- Language: Rust
- Size: 8.79 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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 | identifierformat_spec := 'o' | 'x' | 'X' | 'b'
```In the above grammar, `text` must not contain any `'{'` or `'}'` characters.
## Usage
```rust
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.