winnow-rs / winnow

Making parsing a breeze
https://docs.rs/winnow
Other
525 stars 40 forks source link

the trait SliceLen is not implemented for char #407

Closed baoyachi closed 8 months ago

baoyachi commented 9 months ago

Please complete the following tasks

rust version

rustc 1.74.0 (79e9716c9 2023-11-13)

winnow version

0.5.31

Minimal reproducible code

use std::num::{NonZeroU32, NonZeroU8};
use winnow::{Parser, PResult};
use winnow::ascii::space1;
use winnow::error::{ContextError, ParseError, ParserError};
use winnow::token::{tag, take};

fn main() {
    assert!(parse_human_format("2022-07-01 00:40:05 +08:00").is_ok());
    assert!(parse_human_format("2022-07-00 00:40:05 +08:00").is_err());
}

fn u8_len2(input: &mut &str) -> PResult<u8> {
    take(2_usize).try_map(str::parse).parse_next(input)
}

fn non_zero_u8_len2(input: &mut &str) -> PResult<NonZeroU8> {
    take(2_usize).try_map(str::parse).parse_next(input)
}

//
fn non_zero_u32_len4(input: &mut &str) -> PResult<NonZeroU32> {
    take(4_usize).try_map(str::parse).parse_next(input)
}

// 2022-07-14 00:40:05 +08:00
fn parse_human_format(input: &str) -> Result<(), ParseError<&str, ContextError>> {
    (non_zero_u32_len4,
     tag('-'),
     non_zero_u8_len2,
     tag('-'),
     non_zero_u8_len2,
     space1,
     u8_len2,
     tag(":"),
     u8_len2,
     tag(":"),
     u8_len2,
     space1,
     tag("+"),
     u8_len2,
     tag(":"),
     u8_len2,
    ).parse(input)?;
    Ok(())
}


### Steps to reproduce the bug with the above code

exec above rust code,cause error

### Actual Behaviour

Maybe tag support `char`

### Expected Behaviour

-

### Additional Context

_No response_
epage commented 9 months ago

Is there a reason you are using tag, rather than

  • one_of
  • char directly (as it implements Parser)
baoyachi commented 8 months ago

For this example, both one_of and char are suitable. However, in certain scenarios, semantically, one_of tends to imply a multiple choice selection where only one option is chosen. Using the char type requires hardcoding, and it does not passing parameters into a tag.