Skip to content

Commit

Permalink
Handle utf8 convert errors in butlerdb (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipK authored Mar 9, 2022
1 parent 8263aaf commit f77f1e6
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/itch/butler_db_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,42 @@ fn parse_path<'a>(i: &'a [u8]) -> nom::IResult<&[u8], DbPaths<'a>> {
let (i, _taken) = take_until(prefix)(i)?;
let (i, _taken) = tag(prefix)(i)?;
let (i, base_path) = take_until(suffix)(i)?;

let base_path_str = std::str::from_utf8(base_path);
let base_path = match base_path_str{
Ok(base_path) => base_path,
Err(e) => {
return convert_to_nom_error(i, e);
},
};

let prefix = ":[{\"path\":\"";
let suffix = "\",\"depth";
let (i, _taken) = take_until(prefix)(i)?;
let (i, _taken) = tag(prefix)(i)?;
let (i, path) = take_until(suffix)(i)?;
let path_str = std::str::from_utf8(path);
let path = match path_str{
Ok(path) => path,
Err(e) => {
return convert_to_nom_error(i, e);
},
};

IResult::Ok((
i,
DbPaths {
base_path: std::str::from_utf8(base_path).unwrap(),
path: std::str::from_utf8(path).unwrap(),
base_path,
path,
},
))
}

fn convert_to_nom_error(i: &[u8], e: std::str::Utf8Error) -> Result<(&[u8], DbPaths), nom::Err<nom::error::Error<&[u8]>>> {
use nom::error::*;
IResult::Err(nom::Err::Error(Error::from_external_error(i, ErrorKind::AlphaNumeric,e)))

}

#[cfg(test)]
mod tests {

Expand Down

0 comments on commit f77f1e6

Please sign in to comment.