Skip to content

Commit c407751

Browse files
committed
f Also avoid allocating error messages when not panicking
1 parent 6dfafbf commit c407751

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/io/sqlite_store.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,21 @@ pub struct SqliteStore {
2929

3030
impl SqliteStore {
3131
pub(crate) fn new(dest_dir: PathBuf) -> Self {
32-
let msg =
33-
format!("Failed to create database destination directory: {}", dest_dir.display());
34-
fs::create_dir_all(dest_dir.clone()).expect(&msg);
32+
fs::create_dir_all(dest_dir.clone()).unwrap_or_else(|_| {
33+
panic!("Failed to create database destination directory: {}", dest_dir.display())
34+
});
3535
let mut db_file_path = dest_dir.clone();
3636
db_file_path.push(SQLITE_DB_FILE);
3737

38-
let msg = format!("Failed to open/create database file: {}", db_file_path.display());
39-
let connection = Connection::open(db_file_path).expect(&msg);
38+
let connection = Connection::open(db_file_path.clone()).unwrap_or_else(|_| {
39+
panic!("Failed to open/create database file: {}", db_file_path.display())
40+
});
4041

41-
let msg = format!("Failed to set PRAGMA user_version");
4242
connection
4343
.pragma(Some(rusqlite::DatabaseName::Main), "user_version", SCHEMA_USER_VERSION, |_| {
4444
Ok(())
4545
})
46-
.expect(&msg);
46+
.unwrap_or_else(|_| panic!("Failed to set PRAGMA user_version"));
4747

4848
let sql = format!(
4949
"CREATE TABLE IF NOT EXISTS {} (
@@ -53,8 +53,9 @@ impl SqliteStore {
5353
);",
5454
KV_TABLE_NAME
5555
);
56-
let msg = format!("Failed to create table: {}", KV_TABLE_NAME);
57-
connection.execute(&sql, []).expect(&msg);
56+
connection
57+
.execute(&sql, [])
58+
.unwrap_or_else(|_| panic!("Failed to create table: {}", KV_TABLE_NAME));
5859

5960
let connection = Arc::new(Mutex::new(connection));
6061
Self { connection }

0 commit comments

Comments
 (0)