X-Git-Url: http://git.euphorik.ch/?p=recipes.git;a=blobdiff_plain;f=backend%2Fsrc%2Fdb.rs;fp=backend%2Fsrc%2Fdb.rs;h=bae57d00b71784f10f80022f56bfcab336563df8;hp=28bf62c657725fd7fd3fb9297d0f35b4a562cfd4;hb=45d4867cb37ce8d7007c4d98de70d81d0b705b92;hpb=b1ffd1a04a55d6653ed55ea99f09550e5a8a9a96 diff --git a/backend/src/db.rs b/backend/src/db.rs index 28bf62c..bae57d0 100644 --- a/backend/src/db.rs +++ b/backend/src/db.rs @@ -1,4 +1,4 @@ -use std::{fmt::Display, fs::{self, File}, path::Path, io::Read}; +use std::{fmt, fs::{self, File}, path::Path, io::Read}; use itertools::Itertools; use chrono::{prelude::*, Duration}; @@ -22,6 +22,14 @@ pub enum DBError { Other(String), } +impl fmt::Display for DBError { + fn fmt(&self, f: &mut fmt::Formatter) -> std::result::Result<(), fmt::Error> { + write!(f, "{:?}", self) + } +} + +impl std::error::Error for DBError { } + impl From for DBError { fn from(error: rusqlite::Error) -> Self { DBError::SqliteError(error) @@ -95,7 +103,7 @@ impl Connection { Self::create_connection(SqliteConnectionManager::file(file)) } - fn create_connection(manager: SqliteConnectionManager) -> Result {; + fn create_connection(manager: SqliteConnectionManager) -> Result { let pool = r2d2::Pool::new(manager).unwrap(); let connection = Connection { pool }; connection.create_or_update()?; @@ -206,11 +214,11 @@ impl Connection { } /// - pub fn sign_up(&self, password: &str, email: &str) -> Result { - self.sign_up_with_given_time(password, email, Utc::now()) + pub fn sign_up(&self, email: &str, password: &str) -> Result { + self.sign_up_with_given_time(email, password, Utc::now()) } - fn sign_up_with_given_time(&self, password: &str, email: &str, datetime: DateTime) -> Result { + fn sign_up_with_given_time(&self, email: &str, password: &str, datetime: DateTime) -> Result { let mut con = self.pool.get()?; let tx = con.transaction()?; let token = @@ -313,7 +321,7 @@ impl Connection { } /// Execute a given SQL file. - pub fn execute_file + Display>(&self, file: P) -> Result<()> { + pub fn execute_file + fmt::Display>(&self, file: P) -> Result<()> { let con = self.pool.get()?; let sql = load_sql_file(file)?; con.execute_batch(&sql).map_err(DBError::from) @@ -334,7 +342,7 @@ impl Connection { } } -fn load_sql_file + Display>(sql_file: P) -> Result { +fn load_sql_file + fmt::Display>(sql_file: P) -> Result { let mut file = File::open(&sql_file).map_err(|err| DBError::Other(format!("Cannot open SQL file ({}): {}", &sql_file, err.to_string())))?; let mut sql = String::new(); file.read_to_string(&mut sql).map_err(|err| DBError::Other(format!("Cannot read SQL file ({}) : {}", &sql_file, err.to_string())))?; @@ -352,7 +360,7 @@ mod tests { #[test] fn sign_up() -> Result<()> { let connection = Connection::new_in_memory()?; - match connection.sign_up("12345", "paul@test.org")? { + match connection.sign_up("paul@test.org", "12345")? { SignUpResult::UserCreatedWaitingForValidation(_) => (), // Nominal case. other => panic!("{:?}", other), } @@ -372,7 +380,7 @@ mod tests { 0, NULL );", [])?; - match connection.sign_up("12345", "paul@test.org")? { + match connection.sign_up("paul@test.org", "12345")? { SignUpResult::UserAlreadyExists => (), // Nominal case. other => panic!("{:?}", other), } @@ -393,7 +401,7 @@ mod tests { 0, :token );", named_params! { ":token": token })?; - match connection.sign_up("12345", "paul@test.org")? { + match connection.sign_up("paul@test.org", "12345")? { SignUpResult::UserCreatedWaitingForValidation(_) => (), // Nominal case. other => panic!("{:?}", other), } @@ -404,7 +412,7 @@ mod tests { fn sign_up_then_send_validation_at_time() -> Result<()> { let connection = Connection::new_in_memory()?; let validation_token = - match connection.sign_up("12345", "paul@test.org")? { + match connection.sign_up("paul@test.org", "12345")? { SignUpResult::UserCreatedWaitingForValidation(token) => token, // Nominal case. other => panic!("{:?}", other), }; @@ -419,7 +427,7 @@ mod tests { fn sign_up_then_send_validation_too_late() -> Result<()> { let connection = Connection::new_in_memory()?; let validation_token = - match connection.sign_up_with_given_time("12345", "paul@test.org", Utc::now() - Duration::days(1))? { + match connection.sign_up_with_given_time("paul@test.org", "12345", Utc::now() - Duration::days(1))? { SignUpResult::UserCreatedWaitingForValidation(token) => token, // Nominal case. other => panic!("{:?}", other), }; @@ -434,7 +442,7 @@ mod tests { fn sign_up_then_send_validation_with_bad_token() -> Result<()> { let connection = Connection::new_in_memory()?; let _validation_token = - match connection.sign_up("12345", "paul@test.org")? { + match connection.sign_up("paul@test.org", "12345")? { SignUpResult::UserCreatedWaitingForValidation(token) => token, // Nominal case. other => panic!("{:?}", other), }; @@ -450,12 +458,12 @@ mod tests { fn sign_up_then_send_validation_then_sign_in() -> Result<()> { let connection = Connection::new_in_memory()?; - let password = "12345"; let email = "paul@test.org"; + let password = "12345"; // Sign up. let validation_token = - match connection.sign_up(password, email)? { + match connection.sign_up(email, password)? { SignUpResult::UserCreatedWaitingForValidation(token) => token, // Nominal case. other => panic!("{:?}", other), }; @@ -479,12 +487,12 @@ mod tests { fn sign_up_then_send_validation_then_authentication() -> Result<()> { let connection = Connection::new_in_memory()?; - let password = "12345"; let email = "paul@test.org"; + let password = "12345"; // Sign up. let validation_token = - match connection.sign_up(password, email)? { + match connection.sign_up(email, password)? { SignUpResult::UserCreatedWaitingForValidation(token) => token, // Nominal case. other => panic!("{:?}", other), }; @@ -519,12 +527,12 @@ mod tests { fn sign_up_then_send_validation_then_sign_out_then_sign_in() -> Result<()> { let connection = Connection::new_in_memory()?; - let password = "12345"; let email = "paul@test.org"; + let password = "12345"; // Sign up. let validation_token = - match connection.sign_up(password, email)? { + match connection.sign_up(email, password)? { SignUpResult::UserCreatedWaitingForValidation(token) => token, // Nominal case. other => panic!("{:?}", other), };