Skip to content

Commit df31147

Browse files
committed
Use nom::combinator::map_res instead of nom::combinator::verify when possible
1 parent d369cf2 commit df31147

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

nafcodec/src/decoder/parser.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,28 @@ pub fn variable_u64(i: &[u8]) -> IResult<&[u8], u64> {
4848
}
4949

5050
pub fn format_descriptor(i: &[u8]) -> IResult<&[u8], &[u8]> {
51-
// NOTE: for some reason `nom::bytes::tag`
51+
// NOTE: for some reason `nom::bytes::tag` does not work?
5252
nom::combinator::verify(nom::bytes::take(3u32), |x: &[u8]| x == &[0x01, 0xF9, 0xEC]).parse(i)
5353
}
5454

5555
pub fn format_version(i: &[u8]) -> IResult<&[u8], FormatVersion> {
56-
match nom::combinator::verify(self::byte, |&byte: &u8| byte == 1 || byte == 2).parse(i) {
57-
Err(e) => Err(e),
58-
Ok((i, 1)) => Ok((i, FormatVersion::V1)),
59-
Ok((i, 2)) => Ok((i, FormatVersion::V2)),
60-
_ => unreachable!(),
61-
}
56+
nom::combinator::map_res(self::byte, |byte: u8| match byte {
57+
1 => Ok(FormatVersion::V1),
58+
2 => Ok(FormatVersion::V2),
59+
_ => Err("invalid format version"),
60+
})
61+
.parse(i)
6262
}
6363

6464
pub fn sequence_type(i: &[u8]) -> IResult<&[u8], SequenceType> {
65-
match nom::combinator::verify(self::byte, |&byte: &u8| byte <= 0x03).parse(i) {
66-
Err(e) => Err(e),
67-
Ok((i, 0)) => Ok((i, SequenceType::Dna)),
68-
Ok((i, 1)) => Ok((i, SequenceType::Rna)),
69-
Ok((i, 2)) => Ok((i, SequenceType::Protein)),
70-
Ok((i, 3)) => Ok((i, SequenceType::Text)),
71-
_ => unreachable!(),
72-
}
65+
nom::combinator::map_res(self::byte, |byte: u8| match byte {
66+
0 => Ok(SequenceType::Dna),
67+
1 => Ok(SequenceType::Rna),
68+
2 => Ok(SequenceType::Protein),
69+
3 => Ok(SequenceType::Text),
70+
_ => Err("invalid sequence type"),
71+
})
72+
.parse(i)
7373
}
7474

7575
pub fn flags(i: &[u8]) -> IResult<&[u8], Flags> {

0 commit comments

Comments
 (0)