From 8d4d29a3c25063d71c561f30ccc1c31ab85d2bc7 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Fri, 7 Feb 2020 19:00:15 -0500 Subject: Add test template for "run" --- src/db.rs | 11 ++++++++ src/lib.rs | 1 + src/main.rs | 4 +-- src/models.rs | 26 ------------------- src/server.rs | 19 +++++--------- src/template.rs | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 100 insertions(+), 41 deletions(-) create mode 100644 src/template.rs diff --git a/src/db.rs b/src/db.rs index cec7d56..198ce03 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,6 +1,7 @@ use crate::diesel::BoolExpressionMethods; use crate::error::Error; use crate::models; +use crate::template; use bcrypt; use chrono::DateTime; use chrono::Utc; @@ -248,6 +249,16 @@ pub fn get_entries(conn: &PgConnection, username: &str) -> Result Result { + match entry_type { + "run" => Ok(template::running_template()), + _ => Err(Error::NotFound), + } +} + // pub fn get_entries_with_data( // conn: &PgConnection, // username: &str, diff --git a/src/lib.rs b/src/lib.rs index 80cef09..2036560 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,6 +24,7 @@ pub mod models; mod schema; pub mod server; mod strava; +pub mod template; pub struct Params { pub base_url: String, diff --git a/src/main.rs b/src/main.rs index cf0d308..ec0e660 100644 --- a/src/main.rs +++ b/src/main.rs @@ -135,9 +135,7 @@ fn main() { .value_of("base_url") .unwrap_or("http://localhost:8000"); - let static_path = matches - .value_of("static_path") - .unwrap_or("./static"); + let static_path = matches.value_of("static_path").unwrap_or("./static"); let db_url = matches.value_of("database_url").unwrap(); let conn = PgConnection::establish(db_url).unwrap(); diff --git a/src/models.rs b/src/models.rs index 624a1ce..83153fd 100644 --- a/src/models.rs +++ b/src/models.rs @@ -170,29 +170,3 @@ pub struct Entry { pub timestamp: Option>, pub payload: Value, } - -#[derive(Debug, Serialize, Deserialize, Clone)] -pub enum Unit { - TimestampRfc3339, - Meters, - Seconds, - Unknown, -} - -#[derive(Debug, Serialize, Deserialize, Clone)] -pub struct Column { - field: String, - display_name: Option, - unit: Unit, -} - -#[derive(Debug, Serialize, Deserialize, Clone)] -pub enum TemplateSpec { - Table(Vec), -} - -#[derive(Debug, Serialize, Deserialize, Clone)] -pub struct Template { - pub entry_type: String, - pub spec: TemplateSpec, -} diff --git a/src/server.rs b/src/server.rs index a203af9..3d264a9 100644 --- a/src/server.rs +++ b/src/server.rs @@ -13,12 +13,12 @@ use rocket::request::FromRequest; use rocket::request::Request; use rocket::response::Redirect; use rocket::State; +use rocket_contrib::serve::StaticFiles; use rocket_contrib::templates::Template; use serde_json::map::Map; use serde_json::to_value; use serde_json::Value as Json; use std::collections::HashMap; -use rocket_contrib::serve::StaticFiles; use crate::db; use crate::error::Error; @@ -78,17 +78,15 @@ fn profile(conn: Db, username: String) -> Result { context.insert("title".to_string(), json!(username)); let entries = db::get_entries(&*conn, &username)?; - let headings = - entries.first() + let headings = entries + .first() .and_then(|e| e.payload.as_object()) .map(|e| e.keys().collect::>()); context.insert("headings".to_string(), json!(headings)); context.insert( "entries".to_string(), - json!(entries - .into_iter() - .map(|e| e.payload) - .collect::>())); + json!(entries.into_iter().map(|e| e.payload).collect::>()), + ); Ok(Template::render("profile", context)) } @@ -196,8 +194,7 @@ fn link_strava(params: State) -> Redirect { )) } -pub fn start(conn: diesel::PgConnection, db_url: &str, base_url: &str, - static_path: &str) { +pub fn start(conn: diesel::PgConnection, db_url: &str, base_url: &str, static_path: &str) { let mut database_config = HashMap::new(); let mut databases = HashMap::new(); database_config.insert("url", Value::from(db_url)); @@ -239,9 +236,7 @@ pub fn start(conn: diesel::PgConnection, db_url: &str, base_url: &str, link_strava_callback ], ) - .mount( - "/static", - StaticFiles::from(static_path)) + .mount("/static", StaticFiles::from(static_path)) .attach(Template::fairing()) .attach(Db::fairing()) .launch(); diff --git a/src/template.rs b/src/template.rs new file mode 100644 index 0000000..21defa3 --- /dev/null +++ b/src/template.rs @@ -0,0 +1,80 @@ +use serde::Deserialize; +use serde::Serialize; + +pub fn running_template() -> TemplateSpec { + TemplateSpec::Table(vec![ + Column { + display_name: Some("Date".to_string()), + field: FieldSpec::App( + Function::DisplayUnit(Unit::Timestamp, "date".to_string()), + vec![FieldSpec::Field("start_time".to_string())], + ), + }, + Column { + display_name: Some("Time".to_string()), + field: FieldSpec::App( + Function::DisplayUnit(Unit::Seconds, "".to_string()), + vec![FieldSpec::Field("moving_time".to_string())], + ), + }, + Column { + display_name: Some("Distance".to_string()), + field: FieldSpec::App( + Function::DisplayUnit(Unit::Meters, "miles".to_string()), + vec![FieldSpec::Field("distance".to_string())], + ), + }, + Column { + display_name: Some("Pace".to_string()), + field: FieldSpec::App( + Function::DisplayUnit(Unit::Pace, "".to_string()), + vec![FieldSpec::App( + Function::Div, + vec![ + FieldSpec::Field("moving_time".to_string()), + FieldSpec::Field("distance".to_string()), + ], + )], + ), + }, + ]) +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub enum Unit { + Timestamp, // As Rfc3339 + Meters, + Seconds, + Speed, // As m/s + Pace, // As s/m +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub enum Function { + TimestampToDate, + DisplayUnit(Unit, String), + Div, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub enum FieldSpec { + Field(String), + App(Function, Vec), +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct Column { + display_name: Option, + field: FieldSpec, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub enum TemplateSpec { + Table(Vec), +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct Template { + pub entry_type: String, + pub spec: TemplateSpec, +} -- cgit v1.2.3