I'm working on a parsing library that converts text into the desired data by declaring the steps and then getting the data.
Is it possible to iteratively create a (flat) tuple that contains all the variables?
(Or if you know a better solution, that'd be good too)
Desired:
Parser::parse(my_string) // returns Parser<()>
.parse_int() // returns Parser<(i32)>
.parse_string() // returns Parser<(i32, &str)>
.parse_bool() // returns Parser<(i32, &str, bool)>
.finish(); // returns Result<(i32, &str, bool), ParseError>
What I can do is nesting deeper and deeper by wrapping the existing value in a tuple. Currently:
Parser::parse(my_string) // returns Parser<()>
.parse_int() // returns Parser<((), i32)>
.parse_string() // returns Parser<(((), i32), &str)>
.parse_bool() // returns Parser<((((), i32), &str), bool)>
.finish(); // returns Result<((((), i32), &str), bool)), ParseError>
Here the parse_int
function works somewhat like this:
impl<D> Parser<D> {
pub fn parse_int(self) -> Parser<(D, i32)> {
let value = // Parse the int
return Parser {
...
data: (self.data, value)
}
}
}
Of course this nested tuple would be horrible to work with.