use crate::schema::config; use crate::schema::entries; use crate::schema::raw_data; use crate::schema::strava_tokens; use crate::schema::tasks; use crate::schema::users; use chrono::DateTime; use chrono::Utc; use diesel::deserialize; use diesel::deserialize::FromSql; use diesel::pg::Pg; use diesel::serialize; use diesel::serialize::Output; use diesel::serialize::ToSql; use diesel::sql_types; use serde::Deserialize; use serde::Serialize; use serde_json::Value; use std::fmt; use std::io::Write; #[derive(PartialEq, Debug, Clone, Copy, AsExpression, FromSqlRow)] #[sql_type = "sql_types::Text"] pub enum TaskState { NEW = 0, SUCCESSFUL, FAILED, } impl ToSql for TaskState { fn to_sql(&self, out: &mut Output) -> serialize::Result { let t = match *self { TaskState::NEW => "new".to_string(), TaskState::SUCCESSFUL => "success".to_string(), TaskState::FAILED => "failed".to_string(), }; >::to_sql(&t, out) } } impl FromSql for TaskState { fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result { let s = >::from_sql(bytes)?; match s.as_str() { "new" => Ok(TaskState::NEW), "success" => Ok(TaskState::SUCCESSFUL), "failed" => Ok(TaskState::FAILED), &_ => Err("Unrecognized task state".into()), } } } #[derive(Insertable)] #[table_name = "tasks"] pub struct NewTask<'a> { pub start_at: DateTime, pub state: TaskState, pub username: &'a str, pub payload: &'a Value, } #[derive(Queryable, Debug, Clone)] pub struct Task { pub id: i64, pub state: TaskState, pub start_at: DateTime, pub username: String, pub payload: Value, } #[derive(Insertable, Queryable)] #[table_name = "config"] pub struct Config { pub strava_client_secret: String, pub strava_client_id: String, pub rocket_secret_key: String, pub singleton: bool, } #[derive(Insertable)] #[table_name = "users"] pub struct NewUser<'a> { pub username: &'a str, pub password: &'a str, } #[derive(Queryable, Clone)] pub struct User { pub username: String, pub password: String, } impl fmt::Debug for User { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, "User {{ username: {}, password: }}", self.username ) } } #[derive(AsChangeset, Insertable, Queryable)] #[table_name = "strava_tokens"] pub struct StravaToken { pub username: String, pub refresh_token: String, pub access_token: String, pub expires_at: DateTime, } #[derive(PartialEq, Debug, Clone, Copy, AsExpression, FromSqlRow, Serialize, Deserialize)] #[sql_type = "sql_types::Text"] pub enum DataType { StravaActivity = 0, } impl ToSql for DataType { fn to_sql(&self, out: &mut Output) -> serialize::Result { let t = match *self { DataType::StravaActivity => "s:activi".to_string(), }; >::to_sql(&t, out) } } impl FromSql for DataType { fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result { let s = >::from_sql(bytes)?; match s.as_str() { "s:activi" => Ok(DataType::StravaActivity), &_ => Err("Unrecognized data type".into()), } } } #[derive(Insertable, Queryable, Debug, Serialize, Deserialize, Clone)] #[table_name = "raw_data"] pub struct RawDataKey { pub data_type: DataType, pub id: i64, pub username: String, } #[derive(Insertable, Queryable, Identifiable, Debug, Serialize, Deserialize, Clone)] #[table_name = "raw_data"] pub struct RawData { pub data_type: DataType, pub id: i64, pub username: String, pub payload: Value, pub entry_type: Option, pub entry_id: Option, } #[derive(Insertable, Debug, Serialize, Deserialize, Clone)] #[table_name = "entries"] pub struct NewEntry<'a> { pub username: &'a str, pub entry_type: &'a str, pub timestamp: Option>, pub payload: Value, } #[derive(Queryable, Debug, Serialize, Deserialize, Clone)] pub struct Entry { pub username: String, pub entry_type: String, pub id: i64, pub timestamp: Option>, pub payload: Value, }