From 9f8080ff21c9691d4133144ec1f3f38f4d8fbe22 Mon Sep 17 00:00:00 2001 From: Erin Nova Date: Sun, 23 Jan 2022 18:47:47 -0500 Subject: [PATCH] Create page for Recipe --- src/structures.rs | 159 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 112 insertions(+), 47 deletions(-) diff --git a/src/structures.rs b/src/structures.rs index 4e5c3dc..c56cfea 100644 --- a/src/structures.rs +++ b/src/structures.rs @@ -2,6 +2,7 @@ use uuid::Uuid; use chrono::prelude::*; use serde_derive::{Deserialize, Serialize}; use build_html::{self, Html, HtmlContainer, ContainerType, Container, HtmlPage}; +use std::fmt; use crate::io::write_html; @@ -18,6 +19,21 @@ pub enum TimeUnits { Seconds, } +impl fmt::Display for TimeUnits { + // This trait requires `fmt` with this exact signature. + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + // Write strictly the first element into the supplied output + // stream: `f`. Returns `fmt::Result` which indicates whether the + // operation succeeded or failed. Note that `write!` uses syntax which + // is very similar to `println!`. + match &self { + TimeUnits::Hours => write!(f, "hr."), + TimeUnits::Minutes => write!(f, "min."), + TimeUnits::Seconds => write!(f, "sec."), + } + } +} + #[derive(Debug, Serialize, Deserialize)] pub enum MeasurementUnits { Grams, @@ -34,6 +50,32 @@ pub enum MeasurementUnits { Teaspoons, } +type mu = MeasurementUnits; + +impl fmt::Display for MeasurementUnits { + // This trait requires `fmt` with this exact signature. + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + // Write strictly the first element into the supplied output + // stream: `f`. Returns `fmt::Result` which indicates whether the + // operation succeeded or failed. Note that `write!` uses syntax which + // is very similar to `println!`. + match &self { + mu::Grams => write!(f, "g"), + mu::Kilograms => write!(f, "kg"), + mu::Pounds => write!(f, "lbs"), + mu::Litres => write!(f, "L"), + mu::Millilitres => write!(f, "mL"), + mu::Gallons => write!(f, "gal"), + mu::Ounces => write!(f, "oz"), + mu::Pinches => write!(f, "pinch(es)"), + mu::Drops => write!(f, "drop(s)"), + mu::Cups => write!(f, "cup(s)"), + mu::Tablespoons => write!(f, "tablespoon(s)"), + mu::Teaspoons => write!(f, "teaspoon(s)"), + } + } +} + #[derive(Debug, PartialEq, Clone, Serialize, Deserialize)] pub struct Tag(pub char, pub String); @@ -129,7 +171,61 @@ impl Recipe { } pub fn construct_page(&self) { - let mut recipe_page = build_html::HtmlPage::new() + // Metadata + let meta = Container::new(ContainerType::UnorderedList) + .with_attributes(vec![("class", "recipe")]) + .with_paragraph(format!("⏲️ Preparation time: {} {}", &self.prep_time.0, &self.prep_time.1)) + .with_paragraph(format!("🍳 Cooking time: {} {}", &self.cooking_time.0, &self.cooking_time.1)) + .with_paragraph(format!("🍽️ Servings: {}", &self.servings)); + + // Ingredients + let mut ingredients_container = Container::new(ContainerType::UnorderedList) + .with_attributes(vec![("class", "recipe")]); + for i in &self.ingredients { + let ingredient = format!("{} {} {}", &i.0, &i.1, &i.2); + ingredients_container.add_paragraph(ingredient); + } + + // Directions + let mut directions_container = Container::new(ContainerType::OrderedList) + .with_attributes(vec![("class", "recipe")]); + for n in &self.directions { + directions_container.add_paragraph(&n.1); + } + + // Tags + let mut tags_html = String::new(); + tags_html.push_str(r#"

Tags: "#); + match &self.tags { + Some(list) => { + for x in 0..list.len() { + if x != list.len()-1 { + tags_html.push_str(format!("#{},", list[x].1, list[x].1).as_str()); + } else { + tags_html.push_str(format!("#{}", list[x].1, list[x].1).as_str()); + } + } + }, + None => { + tags_html.push_str(r#"no tags!! :o"#); + }, + } + tags_html.push_str(r#"

"#); + + // Date + let edit_date = match &self.edited_date { + Some(date) => { + format!( + "

Last edited on: {}-{}-{}

", + date.year(), + date.month(), + date.day()) + }, + None => "

No edits

".to_string(), + }; + + // Construct Main Page + let recipe_page = build_html::HtmlPage::new() .with_head_link("/favicon.ico", "icon") // Favicon .with_stylesheet("/style.css") // Link stylesheet .with_meta(vec![("charset", "UTF-8")]) @@ -137,52 +233,21 @@ impl Recipe { .with_meta(vec![("name", "description"), ("content", DESCRIPTION)]) // Add the description .with_title(TITLE) .with_header(1, &self.name) - .with_raw(r#"
"#); - - // Metadata - let meta = Container::new(ContainerType::UnorderedList) - .with_attributes(vec![("class", "recipe")]) - .with_paragraph(format!("⏲️ Preparation time: {} {:?}", &self.prep_time.0, &self.prep_time.1)) - .with_paragraph(format!("🍳 Cooking time: {} {:?}", &self.cooking_time.0, &self.cooking_time.1)) - .with_paragraph(format!("🍽️ Servings: {}", &self.servings)); - recipe_page.add_container(meta); - - // Append ingredients - recipe_page.add_header(2, r#"Ingredients:"#); - let mut ingredients_container = Container::new(ContainerType::UnorderedList) - .with_attributes(vec![("class", "recipe")]); - for i in &self.ingredients { - let ingredient = format!("{:?} {:?} {}", &i.0, &i.1, &i.2); - ingredients_container.add_paragraph(ingredient); - } - recipe_page.add_container(ingredients_container); - - // Append directions - recipe_page.add_header(2, r#"Directions:"#); - let mut directions_container = Container::new(ContainerType::OrderedList) - .with_attributes(vec![("class", "recipe")]); - for n in &self.directions { - directions_container.add_paragraph(&n.1); - } - recipe_page.add_container(directions_container); - - recipe_page.add_header(2, r#"Author:"#); - recipe_page.add_paragraph(&self.attribution); - - // Append tags - let mut tags_html = String::new(); - tags_html.push_str(r#"

Tags: "#); - match &self.tags { - Some(list) => { - for x in list { - tags_html.push_str(format!("#{},", x.1, x.1).as_str()); - } - }, - None => { - tags_html.push_str(r#"no tags!! :o"#); - }, - } - + .with_raw(r#"


"#) + .with_container(meta) + .with_header(2, r#"Ingredients:"#) + .with_container(ingredients_container) + .with_header(2, r#"Directions:"#) + .with_container(directions_container) + .with_header(2, r#"Misc."#) + .with_raw(format!("

Author: {}

", &self.attribution).as_str()) + .with_raw(tags_html) + .with_raw(format!( + "

Recipe added on: {}-{}-{}

", + &self.posted_date.year(), + &self.posted_date.month(), + &self.posted_date.day())) + .with_raw(edit_date); write_html(recipe_page.to_html_string(), "en", Some(&self.shortcode)); }