Refactor more stuff

This commit is contained in:
Tobias Reisinger 2023-12-04 23:59:26 +01:00
parent 5a7b2de0ea
commit 9394a1ae52
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
15 changed files with 167 additions and 86 deletions

View file

@ -40,7 +40,10 @@ pub async fn init(db: &str) -> Pool<Sqlite> {
run_migrations(&pool).await;
let mut pool_conn = pool.acquire().await.unwrap();
let mut pool_conn = pool
.acquire()
.await
.expect("Failed to acquire pool connection");
DbSchedule::get_on(&mut pool_conn)
.await

View file

@ -46,8 +46,8 @@ impl Period {
pub fn new_on() -> Self {
Period {
start: NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
end: NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
start: NaiveTime::MIN,
end: NaiveTime::MIN,
}
}
}
@ -103,8 +103,10 @@ impl From<Vec<u8>> for DbPeriods {
let end_val_h: u32 = value[i - 1] as u32;
let end_val_m: u32 = value[i] as u32;
vec.push(Period {
start: NaiveTime::from_hms_opt(start_val_h, start_val_m, 0).unwrap(),
end: NaiveTime::from_hms_opt(end_val_h, end_val_m, 0).unwrap(),
start: NaiveTime::from_hms_opt(start_val_h, start_val_m, 0)
.expect("Failed to parse period start time from database"),
end: NaiveTime::from_hms_opt(end_val_h, end_val_m, 0)
.expect("Failed to parse period end time from database"),
});
}
DbPeriods(vec)

View file

@ -3,14 +3,12 @@ use actix_web::http::StatusCode;
#[derive(Debug)]
pub enum ApiError {
ProtectedSchedule,
InternalError(String),
}
impl ApiError {
pub fn get_code(&self) -> StatusCode {
match self {
ApiError::ProtectedSchedule => StatusCode::FORBIDDEN,
ApiError::InternalError(_) => StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
@ -19,7 +17,6 @@ impl From<&ApiError> for String {
fn from(err: &ApiError) -> Self {
match err {
ApiError::ProtectedSchedule => String::from("the targeted schedule is protected"),
ApiError::InternalError(msg) => msg.clone(),
}
}
}

View file

@ -1,17 +1,22 @@
use std::fmt::{Debug, Display, Formatter};
use actix::MailboxError;
use actix_web::http::StatusCode;
use actix_web::HttpResponse;
use serde::ser::SerializeStruct;
use serde::{Serialize, Serializer};
use crate::errors::{ApiError, DatabaseError};
use crate::types::ControllerUid;
#[derive(Debug)]
pub enum EmgauwaError {
Api(ApiError),
Uid(uuid::Error),
Serialization(serde_json::Error),
Database(DatabaseError),
Internal(String),
Connection(ControllerUid),
}
impl EmgauwaError {
@ -21,6 +26,8 @@ impl EmgauwaError {
EmgauwaError::Serialization(_) => StatusCode::INTERNAL_SERVER_ERROR,
EmgauwaError::Database(err) => err.get_code(),
EmgauwaError::Uid(_) => StatusCode::BAD_REQUEST,
EmgauwaError::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
EmgauwaError::Connection(_) => StatusCode::GATEWAY_TIMEOUT,
}
}
}
@ -32,6 +39,8 @@ impl From<&EmgauwaError> for String {
EmgauwaError::Serialization(_) => String::from("error during (de-)serialization"),
EmgauwaError::Database(err) => String::from(err),
EmgauwaError::Uid(_) => String::from("the uid is in a bad format"),
EmgauwaError::Internal(_) => String::from("general error"),
EmgauwaError::Connection(_) => String::from("the target controller is not connected"),
}
}
}
@ -66,6 +75,12 @@ impl From<uuid::Error> for EmgauwaError {
}
}
impl From<MailboxError> for EmgauwaError {
fn from(value: MailboxError) -> Self {
EmgauwaError::Internal(value.to_string())
}
}
impl From<&EmgauwaError> for HttpResponse {
fn from(err: &EmgauwaError) -> Self {
HttpResponse::build(err.get_code()).json(err)
@ -90,12 +105,6 @@ impl Display for EmgauwaError {
}
}
impl Debug for EmgauwaError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", String::from(self))
}
}
impl actix_web::error::ResponseError for EmgauwaError {
fn status_code(&self) -> StatusCode {
self.get_code()

View file

@ -85,7 +85,7 @@ impl TryFrom<&str> for ControllerUid {
impl From<&[u8]> for ControllerUid {
fn from(value: &[u8]) -> Self {
Self(Uuid::from_slice(value).unwrap())
Self(Uuid::from_slice(value).expect("Failed to parse controller uid from database"))
}
}

View file

@ -6,8 +6,9 @@ pub use controller_uid::ControllerUid;
pub use schedule_uid::ScheduleUid;
use serde_derive::{Deserialize, Serialize};
use crate::db::DbSchedule;
use crate::errors::EmgauwaError;
use crate::models::Controller;
use crate::models::{Controller, Relay};
pub type Weekday = i64;
@ -15,4 +16,6 @@ pub type Weekday = i64;
#[rtype(result = "Result<(), EmgauwaError>")]
pub enum ControllerWsAction {
Register(Controller),
Schedules(Vec<DbSchedule>),
Relays(Vec<Relay>),
}

View file

@ -145,7 +145,9 @@ impl From<&[u8]> for ScheduleUid {
match value {
[Self::OFF_U8] => Self::Off,
[Self::ON_U8] => Self::On,
value_bytes => Self::Any(Uuid::from_slice(value_bytes).unwrap()),
value_bytes => Self::Any(
Uuid::from_slice(value_bytes).expect("Failed to parse schedule uid from database"),
),
}
}
}