diff options
author | Jesse Luehrs <doy@tozt.net> | 2018-11-06 02:28:18 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2018-11-06 02:28:18 -0500 |
commit | b657e862df6a90ad2f957f02b964d2576e2f03be (patch) | |
tree | f09e6f2a54764c67efb43d3d57dd8199b750d57a /src/db.rs | |
parent | a7cceb1aa5afb13de9f51c10670757d39d50e2a3 (diff) | |
download | lastfm-query-b657e862df6a90ad2f957f02b964d2576e2f03be.tar.gz lastfm-query-b657e862df6a90ad2f957f02b964d2576e2f03be.zip |
no need to create the connection twice here
Diffstat (limited to 'src/db.rs')
-rw-r--r-- | src/db.rs | 17 |
1 files changed, 10 insertions, 7 deletions
@@ -15,16 +15,19 @@ pub struct DB { impl DB { pub fn new<P: AsRef<std::path::Path>>(path: &P) -> Result<DB> { - if !path.as_ref().exists() { - Self::create(path)?; + let conn = if path.as_ref().exists() { + rusqlite::Connection::open(path)? } + else { + Self::create(path)? + }; - return Ok(DB { - conn: rusqlite::Connection::open(path)?, - }) + return Ok(DB { conn }) } - fn create<P: AsRef<std::path::Path>>(path: &P) -> Result<()> { + fn create<P: AsRef<std::path::Path>>( + path: &P + ) -> Result<rusqlite::Connection> { println!( "Initializing database at {}", path.as_ref().to_string_lossy(), @@ -34,7 +37,7 @@ impl DB { std::fs::create_dir_all(parent)?; let conn = rusqlite::Connection::open(path)?; conn.execute(SCHEMA, rusqlite::NO_PARAMS)?; - Ok(()) + Ok(conn) } else { unimplemented!(); |