https://github.com/doumanash/banjin
Simple code generator for string parsing
https://github.com/doumanash/banjin
parser
Last synced: 2 months ago
JSON representation
Simple code generator for string parsing
- Host: GitHub
- URL: https://github.com/doumanash/banjin
- Owner: DoumanAsh
- License: apache-2.0
- Created: 2018-12-04T15:21:25.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-12-15T14:03:18.000Z (over 6 years ago)
- Last Synced: 2025-03-28T17:50:36.353Z (3 months ago)
- Topics: parser
- Language: Rust
- Homepage:
- Size: 23.4 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# banjin
[](https://travis-ci.org/DoumanAsh/banjin)
[](https://crates.io/crates/banjin)
[](https://docs.rs/crate/banjin/)
[](https://deps.rs/repo/github/DoumanAsh/banjin)Simple code generator for manual parsing
## Attributes
### Struct
There are several attributes that control behaviour of parser
Each, attached to struct's field- `starts_with = ` - Specifies string with which next parse step should start(can be stacked). Errors if prefix is missing.
- `ends_with = ` - Specifies string with which parse step should end(can be stacked). Errors if suffix is missing. If empty, expects EOF.
- `skip = ` - Specifies to skip characters, until not meeting character outside of specified in string.
- `skip(ws)` - Specifies to skip all white space characters.
- `format()` - Specifies list of characters that should contain value to parse from.
- `format(not())` - Specifies list of characters that should contain value to parse from.##### Formats
- Literal string - When string is specified as argument to `format`, it is used as set of characters.
- `numeric` - When specified, match using `char::is_numeric()`
- `digit()` - When specified, match using `char::is_digit()`
- `ascii` - When specified, match using `char::is_ascii()`
- `alphabetic` - When specified, match using `char::is_alphabetic()`### Enum
- `format = ` - Specifies string to match against.
- `case` - Specifies case sensitive match. By default it is insensitive.
- `default` - Specifies variant as taking default value. Should take only single `String` and
there can be only one## Usage
### Struct
```rust
use std::str::FromStr;#[derive(banjin::Parser)]
pub struct Data {
#[starts_with = "prefix"]
#[skip(ws)]
#[starts_with = "+"]
#[skip(ws)]
#[format(ascii)]
#[format(digit(10))]
pub first: u32,
#[skip(ws)]
#[format(not("d"))]
#[format("13")]
#[ends_with = "d"]
#[ends_with = ""]
pub second: String,
}fn main() {
let data = Data::from_str("prefix + 666 13d").expect("Parse");
assert_eq!(data.first, 666);
assert_eq!(data.second, "13");let data = Data::from_str("prefix + 666 13");
assert!(data.is_err());let data = Data::from_str("prefix 666 13d");
assert!(data.is_err());let data = Data::from_str("prefix + 666 13dg");
assert!(data.is_err());let data = Data::from_str("");
assert!(data.is_err());
}```
### Enum
```rust
use std::str::FromStr;#[derive(banjin::Parser, PartialEq, Eq, Debug)]
enum Gender {
Male,
#[case]
Female,
#[default]
Other(String)
}fn main() {
let gender = Gender::from_str("male").expect("Parse");
assert_eq!(gender, Gender::Male);let gender = Gender::from_str("female").expect("Parse");
match gender {
Gender::Other(text) => assert_eq!(text, "female"),
_ => panic!("Unexpected!")
}let gender = Gender::from_str("none").expect("Parse");
match gender {
Gender::Other(text) => assert_eq!(text, "none"),
_ => panic!("Unexpected!")
}
}
```