Skip to content

Commit dbe7abd

Browse files
committed
Itch: Parse paths as lossy
1 parent c188c6f commit dbe7abd

File tree

3 files changed

+27
-29
lines changed

3 files changed

+27
-29
lines changed

src/itch/butler_db_parser.rs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,29 @@ use nom::{
55
};
66

77
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
8-
pub(crate) struct DbPaths<'a> {
9-
pub(crate) base_path: &'a str,
10-
pub(crate) path: &'a str,
8+
pub(crate) struct DbPaths {
9+
pub(crate) base_path: String,
10+
pub(crate) path: String,
1111
}
1212

13-
pub(crate) fn parse_butler_db<'a>(content: &'a [u8]) -> nom::IResult<&[u8], Vec<DbPaths<'a>>> {
13+
pub(crate) fn parse_butler_db<'a>(content: &'a [u8]) -> nom::IResult<&[u8], Vec<DbPaths>> {
1414
many0(parse_path)(content)
1515
}
1616

17-
fn parse_path<'a>(i: &'a [u8]) -> nom::IResult<&[u8], DbPaths<'a>> {
17+
fn parse_path<'a>(i: &'a [u8]) -> nom::IResult<&[u8], DbPaths> {
1818
let prefix = "{\"basePath\":\"";
1919
let suffix = "\",\"totalSize\"";
2020
let (i, _taken) = take_until(prefix)(i)?;
2121
let (i, _taken) = tag(prefix)(i)?;
2222
let (i, base_path) = take_until(suffix)(i)?;
23-
let base_path_str = std::str::from_utf8(base_path);
24-
let base_path = match base_path_str{
25-
Ok(base_path) => base_path,
26-
Err(e) => {
27-
return convert_to_nom_error(i, e);
28-
},
29-
};
23+
let base_path = String::from_utf8_lossy(base_path).to_string();
3024

3125
let prefix = ":[{\"path\":\"";
3226
let suffix = "\",\"depth";
3327
let (i, _taken) = take_until(prefix)(i)?;
3428
let (i, _taken) = tag(prefix)(i)?;
3529
let (i, path) = take_until(suffix)(i)?;
36-
let path_str = std::str::from_utf8(path);
37-
let path = match path_str{
38-
Ok(path) => path,
39-
Err(e) => {
40-
return convert_to_nom_error(i, e);
41-
},
42-
};
30+
let path = String::from_utf8_lossy(path).to_string();
4331

4432
IResult::Ok((
4533
i,
@@ -50,12 +38,6 @@ fn parse_path<'a>(i: &'a [u8]) -> nom::IResult<&[u8], DbPaths<'a>> {
5038
))
5139
}
5240

53-
fn convert_to_nom_error(i: &[u8], e: std::str::Utf8Error) -> Result<(&[u8], DbPaths), nom::Err<nom::error::Error<&[u8]>>> {
54-
use nom::error::*;
55-
IResult::Err(nom::Err::Error(Error::from_external_error(i, ErrorKind::AlphaNumeric,e)))
56-
57-
}
58-
5941
#[cfg(test)]
6042
mod tests {
6143

@@ -91,4 +73,21 @@ mod tests {
9173
assert_eq!(paths[5].base_path, "/home/philip/.config/itch/apps/islands");
9274
assert_eq!(paths[5].path, "Islands_Linux.x86_64");
9375
}
76+
77+
#[test]
78+
fn parse_itch_butler_db_test_other() {
79+
let content = include_bytes!("../testdata/itch/other-butler.db-wal");
80+
let result = parse_butler_db(content);
81+
assert!(result.is_ok());
82+
let (_r, paths) = result.unwrap();
83+
assert_eq!(paths.len(), 94);
84+
85+
assert_eq!(paths[0].base_path, "/home/deck/.config/itch/apps/risetoruins");
86+
assert_eq!(paths[0].path, "Core.jar");
87+
//The parser finds douplicates
88+
assert_eq!(paths[0], paths[1]);
89+
assert_eq!(paths[1], paths[2]);
90+
91+
92+
}
9493
}

src/itch/itch_platform.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ impl Platform<ItchGame, ItchErrors> for ItchPlatform {
4848
}),
4949
}?;
5050

51-
//This is done to remove douplicates
51+
//This is done dedupe
5252
let paths: HashSet<&DbPaths> = paths.iter().collect();
53-
5453
let res = paths.iter().filter_map(|e| dbpath_to_game(*e)).collect();
5554
Ok(res)
5655
}
@@ -71,8 +70,8 @@ impl Platform<ItchGame, ItchErrors> for ItchPlatform {
7170
}
7271
}
7372

74-
fn dbpath_to_game(paths: &DbPaths<'_>) -> Option<ItchGame> {
75-
let recipt = Path::new(paths.base_path)
73+
fn dbpath_to_game(paths: &DbPaths) -> Option<ItchGame> {
74+
let recipt = Path::new(paths.base_path.as_str())
7675
.join(".itch")
7776
.join("receipt.json.gz");
7877
if !&recipt.exists() {
3.1 MB
Binary file not shown.

0 commit comments

Comments
 (0)