Support for returning parsed data from `InputValidator`
- Dominant language
- Rust
- Stars
- 257
- Forks
- 18
- Avg merge
- 4h 54m
- Merged PRs (30d)
- 11
Description
I propose that `Input` and `InputValidator` should be changed to:
```rust
pub struct Input<'a, T = String> {
...
pub validation: Box>,
...
}
impl<'a, T> Input<'a, T> {
pub fn run(mut self) -> io::Result;
}
pub trait InputValidator {
type Output;
fn check(&self, input: &str) -> Result;
}
```
Currently you might need to both parse the provided string in the validator and after `Input::run` returns, which this could prevent.
Example with current `demand`:
```rust
use demand::{Input, InputValidator};
struct Parser;
fn parse(s: &str) -> Result {
let value: serde_json::Value = serde_json::from_str(s).map_err(|e| e.to_string())?;
Ok(value)
}
impl InputValidator for Parser {
fn check(&self, input: &str) -> Result<(), String> {
parse(input).map(|_| ())
}
}
fn main() -> Result<(), Box> {
let json = Input::new("Enter JSON:").validator(Parser).run()?;
let value = parse(&json).unwrap();
println!("{value:#?}");
Ok(())
}
```
With new proposed `demand`:
```rust
use demand::{Input, InputValidator};
struct Parser;
impl InputValidator for Parser {
type Output = serde_json::Value;
fn check(&self, input: &str) -> Result {
serde_json::from_str(input).map_err(|e| e.to_string())
}
}
fn main() -> Result<(), Box> {
let json = Input::new("Enter JSON:").validator(Parser).run()?;
println!("{value:#?}");
Ok(())
}
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.