use r2d2::{PooledConnection}; use r2d2_sqlite::{SqliteConnectionManager, rusqlite::Statement}; use rocket::{serde::{Deserialize, Serialize, json::{self, serde_json::json}}, Responder, State}; use crate::DBPool; #[derive(Deserialize, Serialize)] #[serde(crate="rocket::serde")] pub struct ApiResponse { pub code: u32, pub message: String } #[derive(Deserialize, Serialize)] #[serde(crate="rocket::serde")] pub struct ApiResponseActiveList { pub code: u32, pub active_animations: Vec, } #[derive(Responder)] pub struct ApiResponder(String); pub fn create_message(code: u32, message: &str) -> String { return json!(ApiResponse {code, message: message.to_string()}).to_string(); } pub fn get_error_message(message: &str) -> String { return create_message(500, message); } pub type Conn = PooledConnection; pub fn get_conn(db: &State) -> Result { let conn = db.get(); if let Ok(conn) = conn { return Ok(conn); } Err(json!(ApiResponse {code: 500, message: "Clould not conncect to the db.".to_string()}).to_string()) } pub fn prepare_statement<'a>(conn: &'a Conn, stat: &str) -> Result, String> { let stmt = conn.prepare(stat); if stmt.is_err() || stmt.as_ref().ok().is_none() { println!("stmt: {}", stat); return Err(get_error_message("Could not construct statetment")); } let stmt: Statement<'a> = stmt.unwrap(); return Ok(stmt); }