From b657e862df6a90ad2f957f02b964d2576e2f03be Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 6 Nov 2018 02:28:18 -0500 Subject: no need to create the connection twice here --- src/db.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/db.rs b/src/db.rs index ef5a58f..e9e2c62 100644 --- a/src/db.rs +++ b/src/db.rs @@ -15,16 +15,19 @@ pub struct DB { impl DB { pub fn new>(path: &P) -> Result { - 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>(path: &P) -> Result<()> { + fn create>( + path: &P + ) -> Result { 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!(); -- cgit v1.2.3-54-g00ecf