lights/src/utils.rs
Andre Henriques c1b3625a14
All checks were successful
continuous-integration/drone/push Build is passing
chore: list animations
2023-07-10 12:41:46 +01:00

53 lines
1.5 KiB
Rust

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<String>,
}
#[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<SqliteConnectionManager>;
pub fn get_conn(db: &State<DBPool>) -> Result<Conn, String> {
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<Statement<'a>, 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);
}