Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use T: AsRef<Path> as param to SqliteDatabase::new #675

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add `descriptor::checksum::get_checksum_bytes` method.
- Add `Excess` enum to handle remaining amount after coin selection.
- Move change creation from `Wallet::create_tx` to `CoinSelectionAlgorithm::coin_select`.
- Change the interface of `SqliteDatabase::new` to accept any type that implement AsRef<Path>

## [v0.20.0] - [v0.19.0]

Expand Down
13 changes: 9 additions & 4 deletions src/database/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// You may not use this file except in accordance with one or both of these
// licenses.
use std::path::Path;
use std::path::PathBuf;

use bitcoin::consensus::encode::{deserialize, serialize};
use bitcoin::hash_types::Txid;
Expand Down Expand Up @@ -60,17 +62,20 @@ static MIGRATIONS: &[&str] = &[
#[derive(Debug)]
pub struct SqliteDatabase {
/// Path on the local filesystem to store the sqlite file
pub path: String,
pub path: PathBuf,
/// A rusqlite connection object to the sqlite database
pub connection: Connection,
}

impl SqliteDatabase {
/// Instantiate a new SqliteDatabase instance by creating a connection
/// to the database stored at path
pub fn new(path: String) -> Self {
pub fn new<T: AsRef<Path>>(path: T) -> Self {
let connection = get_connection(&path).unwrap();
SqliteDatabase { path, connection }
SqliteDatabase {
path: PathBuf::from(path.as_ref()),
connection,
}
}
fn insert_script_pubkey(
&self,
Expand Down Expand Up @@ -908,7 +913,7 @@ impl BatchDatabase for SqliteDatabase {
}
}

pub fn get_connection(path: &str) -> Result<Connection, Error> {
pub fn get_connection<T: AsRef<Path>>(path: &T) -> Result<Connection, Error> {
let connection = Connection::open(path)?;
migrate(&connection)?;
Ok(connection)
Expand Down