-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #265 from pacak/rc-0.9.4
Example for dynamic parser
- Loading branch information
Showing
4 changed files
with
78 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
//! You can construct parser at runtime without having a concrete type too | ||
|
||
use bpaf::*; | ||
|
||
#[derive(Debug, Clone)] | ||
enum Value { | ||
Bool(bool), | ||
Number(usize), | ||
String(String), | ||
} | ||
|
||
fn number(name: &'static str) -> impl Parser<(String, Value)> { | ||
let label = name.to_string(); | ||
long(name) | ||
.argument::<usize>("NUM") | ||
.map(move |n| (label.clone(), Value::Number(n))) | ||
} | ||
|
||
fn bool(name: &'static str) -> impl Parser<(String, Value)> { | ||
let label = name.to_string(); | ||
long(name) | ||
.switch() | ||
.map(move |n| (label.clone(), Value::Bool(n))) | ||
} | ||
|
||
fn string(name: &'static str) -> impl Parser<(String, Value)> { | ||
let label = name.to_string(); | ||
long(name) | ||
.help("this can use a help message") | ||
.argument::<String>("NUM") | ||
.map(move |n| (label.clone(), Value::String(n))) | ||
} | ||
|
||
fn cons<T>(acc: Box<dyn Parser<Vec<T>>>, cur: Box<dyn Parser<T>>) -> Box<dyn Parser<Vec<T>>> | ||
where | ||
T: 'static, | ||
{ | ||
construct!(acc, cur) | ||
.map(|(mut acc, cur)| { | ||
acc.push(cur); | ||
acc | ||
}) | ||
.boxed() | ||
} | ||
|
||
enum Ty { | ||
Bool, | ||
Number, | ||
String, | ||
} | ||
|
||
fn main() { | ||
let items = &[ | ||
("banana", Ty::Bool), | ||
("width", Ty::Number), | ||
("name", Ty::String), | ||
]; | ||
|
||
let mut parser = pure(Vec::<(String, Value)>::new()).boxed(); | ||
for (name, ty) in items { | ||
parser = cons( | ||
parser, | ||
match ty { | ||
Ty::Bool => bool(name).boxed(), | ||
Ty::Number => number(name).boxed(), | ||
Ty::String => string(name).boxed(), | ||
}, | ||
) | ||
} | ||
|
||
let options = parser.run(); | ||
println!("{:?}", options); | ||
} |