Skip to content

Commit

Permalink
remove some expects from rust
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoBSalgueiro committed Sep 20, 2023
1 parent 5e4224d commit 3bb0097
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
19 changes: 9 additions & 10 deletions src-tauri/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ pub async fn convert_pgn(
let uncompressed: Box<dyn std::io::Read + Send> = if extension == "bz2" {
Box::new(bzip2::read::MultiBzDecoder::new(file))
} else if extension == "zst" {
Box::new(zstd::Decoder::new(file).expect("zstd decoder"))
Box::new(zstd::Decoder::new(file)?)
} else {
Box::new(file)
};
Expand Down Expand Up @@ -551,7 +551,7 @@ pub async fn get_db_info(
_ => "".to_string(),
};

let storage_size = path.metadata().expect("get metadata").len() as usize;
let storage_size = path.metadata()?.len() as usize;
let filename = path.file_name().expect("get filename").to_string_lossy();

let is_indexed = check_index_exists(db)?;
Expand Down Expand Up @@ -845,8 +845,7 @@ pub async fn get_games(
count = Some(
count_query
.select(diesel::dsl::count(games::id))
.first(db)
.expect("count games"),
.first(db)?,
);
}

Expand All @@ -855,7 +854,7 @@ pub async fn get_games(
// diesel::debug_query::<diesel::sqlite::Sqlite, _>(&sql_query)
// );

let games: Vec<(Game, Player, Player, Event, Site)> = sql_query.load(db).expect("load games");
let games: Vec<(Game, Player, Player, Event, Site)> = sql_query.load(db)?;
let normalized_games = normalize_games(games);

Ok(QueryResponse {
Expand Down Expand Up @@ -934,7 +933,7 @@ pub async fn get_players(
}

if !query.options.skip_count {
count = Some(count_query.count().get_result(db).expect("count players"));
count = Some(count_query.count().get_result(db)?);
}

if let Some(limit) = query.options.page_size {
Expand All @@ -960,7 +959,7 @@ pub async fn get_players(
},
};

let players = sql_query.load::<Player>(db).expect("load players");
let players = sql_query.load::<Player>(db)?;

Ok(QueryResponse {
data: players,
Expand Down Expand Up @@ -1000,7 +999,7 @@ pub async fn get_tournaments(
}

if !query.options.skip_count {
count = Some(count_query.count().get_result(db).expect("count players"));
count = Some(count_query.count().get_result(db)?);
}

if let Some(limit) = query.options.page_size {
Expand All @@ -1022,7 +1021,7 @@ pub async fn get_tournaments(
},
};

let events = sql_query.load::<Event>(db).expect("load events");
let events = sql_query.load::<Event>(db)?;

Ok(QueryResponse {
data: events,
Expand Down Expand Up @@ -1059,7 +1058,7 @@ pub async fn get_players_game_info(
.filter(games::white_id.eq(id).or(games::black_id.eq(id)));

let info: Vec<(i32, i32, Option<String>, Option<String>, Option<String>)> =
sql_query.load(db).expect("load games");
sql_query.load(db)?;

let mut game_info = PlayerGameInfo::default();

Expand Down
9 changes: 3 additions & 6 deletions src-tauri/src/db/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ pub async fn search_position(
games::white_material,
games::black_material,
))
.load(db)
.expect("load games");
.load(db)?;

info!("got {} games: {:?}", games.len(), start.elapsed());
}
Expand Down Expand Up @@ -281,8 +280,7 @@ pub async fn search_position(
.inner_join(events::table.on(games::event_id.eq(events::id)))
.inner_join(sites::table.on(games::site_id.eq(sites::id)))
.filter(games::id.eq_any(ids))
.load(db)
.expect("load games");
.load(db)?;
let normalized_games = normalize_games(games);

let openings: Vec<PositionStats> = openings.into_iter().map(|(_, v)| v).collect();
Expand Down Expand Up @@ -322,8 +320,7 @@ pub async fn is_position_in_db(
games::white_material,
games::black_material,
))
.load(db)
.expect("load games");
.load(db)?;

info!("got {} games: {:?}", games.len(), start.elapsed());
}
Expand Down
12 changes: 4 additions & 8 deletions src-tauri/src/puzzle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub struct PuzzleDatabaseInfo {
pub async fn get_puzzle_db_info(
file: PathBuf,
app: tauri::AppHandle,
) -> Result<PuzzleDatabaseInfo, String> {
) -> Result<PuzzleDatabaseInfo, Error> {
let db_path = PathBuf::from("puzzles").join(file);

let path = resolve_path(
Expand All @@ -102,18 +102,14 @@ pub async fn get_puzzle_db_info(
&app.env(),
db_path,
Some(BaseDirectory::AppData),
)
.or(Err("resolve path"))?;
)?;

let mut db =
diesel::SqliteConnection::establish(&path.to_string_lossy()).expect("open database");

let puzzle_count = puzzles::table
.count()
.get_result::<i64>(&mut db)
.expect("get puzzle count") as usize;
let puzzle_count = puzzles::table.count().get_result::<i64>(&mut db)? as usize;

let storage_size = path.metadata().expect("get metadata").len() as usize;
let storage_size = path.metadata()?.len() as usize;
let filename = path.file_name().expect("get filename").to_string_lossy();

Ok(PuzzleDatabaseInfo {
Expand Down

0 comments on commit 3bb0097

Please sign in to comment.