properly handle parsing and invalid unicode errors
This commit is contained in:
parent
2be7962206
commit
3c57615eaa
41
src/db.rs
41
src/db.rs
|
|
@ -79,7 +79,8 @@ impl DB {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let z_db_file = File::open(path).with_context(|| anyhow!("could not open z db file"))?;
|
let z_db_file =
|
||||||
|
File::open(path).with_context(|| anyhow!("could not open z database file"))?;
|
||||||
let reader = BufReader::new(z_db_file);
|
let reader = BufReader::new(z_db_file);
|
||||||
|
|
||||||
for (idx, read_line) in reader.lines().enumerate() {
|
for (idx, read_line) in reader.lines().enumerate() {
|
||||||
|
|
@ -95,24 +96,46 @@ impl DB {
|
||||||
|
|
||||||
match split_line.as_slice() {
|
match split_line.as_slice() {
|
||||||
[epoch_str, rank_str, path_str] => {
|
[epoch_str, rank_str, path_str] => {
|
||||||
let epoch = epoch_str
|
let epoch = match epoch_str.parse::<i64>() {
|
||||||
.parse::<i64>()
|
Ok(epoch) => epoch,
|
||||||
.with_context(|| anyhow!("could not parse epoch: '{}'", epoch_str))?;
|
Err(e) => {
|
||||||
let rank = rank_str
|
eprintln!(
|
||||||
.parse::<f64>()
|
"invalid epoch '{}' at line {}: {}",
|
||||||
.with_context(|| anyhow!("could not parse rank: '{}'", rank_str))?;
|
epoch_str, line_number, e
|
||||||
let path = match PathBuf::from(path_str).canonicalize() {
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let rank = match rank_str.parse::<f64>() {
|
||||||
|
Ok(rank) => rank,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("invalid rank '{}' at line {}: {}", rank_str, line_number, e);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let path_abs = match Path::new(path_str).canonicalize() {
|
||||||
Ok(path) => path,
|
Ok(path) => path,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("invalid path '{}' at line {}: {}", path_str, line_number, e);
|
eprintln!("invalid path '{}' at line {}: {}", path_str, line_number, e);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
let path_str = match path_abs.to_str() {
|
||||||
|
Some(path) => path,
|
||||||
|
None => {
|
||||||
|
eprintln!(
|
||||||
|
"invalid unicode in path '{}' at line {}",
|
||||||
|
path_abs.display(),
|
||||||
|
line_number
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// FIXME: When we switch to PathBuf for storing directories inside Dir, just
|
// FIXME: When we switch to PathBuf for storing directories inside Dir, just
|
||||||
// pass `PathBuf::from(path_str)`
|
// pass `PathBuf::from(path_str)`
|
||||||
self.dirs.push(Dir {
|
self.dirs.push(Dir {
|
||||||
path: path.display().to_string(),
|
path: path_str.to_string(),
|
||||||
rank,
|
rank,
|
||||||
last_accessed: epoch,
|
last_accessed: epoch,
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue