diff options
Diffstat (limited to 'src/post.rs')
| -rw-r--r-- | src/post.rs | 46 |
1 files changed, 21 insertions, 25 deletions
diff --git a/src/post.rs b/src/post.rs index 0250bff..afe6b77 100644 --- a/src/post.rs +++ b/src/post.rs @@ -1,35 +1,30 @@ -use std::collections::HashMap; use crate::metadata::Metadata; use crate::template::{TemplateContext, TemplateValue}; +use std::collections::HashMap; pub struct Post { pub metadata: Metadata, pub index: u8, pub html: String, - pub raw: String + pub raw: String, } impl Post { pub fn to_template_context(posts: &Vec<Post>) -> TemplateContext { let mut context = HashMap::new(); - let posts_collection = posts - .iter() - .map(|post| post.to_template_value()) - .collect(); + let posts_collection = posts.iter().map(|post| post.to_template_value()).collect(); context.insert( "has_posts".to_string(), - TemplateValue::Bool(posts.len() > 0) + TemplateValue::Bool(posts.len() > 0), ); context.insert( "posts_length".to_string(), - TemplateValue::Unsigned( - posts.len().try_into().unwrap() - ) + TemplateValue::Unsigned(posts.len().try_into().unwrap()), ); context.insert( "posts".to_string(), - TemplateValue::Collection(posts_collection) + TemplateValue::Collection(posts_collection), ); context @@ -39,46 +34,46 @@ impl Post { context.insert( "id".to_string(), - TemplateValue::String(format!("{}", self.metadata.id)) + TemplateValue::String(format!("{}", self.metadata.id)), ); context.insert( "created_on".to_string(), - TemplateValue::Unsigned(self.metadata.created_on) + TemplateValue::Unsigned(self.metadata.created_on), ); if let Some(created_on_utc) = self.metadata.created_on_utc() { context.insert( "created_on_utc".to_string(), - TemplateValue::String(created_on_utc) + TemplateValue::String(created_on_utc), ); } - context.insert( - "title".to_string(), - TemplateValue::String(self.title()) - ); + context.insert("title".to_string(), TemplateValue::String(self.title())); context.insert( "index".to_string(), - TemplateValue::Unsigned(self.index.into()) + TemplateValue::Unsigned(self.index.into()), ); context.insert( "html".to_string(), - TemplateValue::String(format!("{}", self.html)) + TemplateValue::String(format!("{}", self.html)), ); context.insert( "escaped_html".to_string(), - TemplateValue::String(format!("{}", self.escaped_html())) + TemplateValue::String(format!("{}", self.escaped_html())), ); context.insert( "raw".to_string(), - TemplateValue::String(format!("{}", self.raw)) + TemplateValue::String(format!("{}", self.raw)), ); context } fn title(&self) -> String { - self.raw.trim() - .split('\n').next().unwrap() + self.raw + .trim() + .split('\n') + .next() + .unwrap() .replace('#', "") .replace("&", "&") .trim() @@ -86,7 +81,8 @@ impl Post { } fn escaped_html(&self) -> String { - self.html.replace("&", "&") + self.html + .replace("&", "&") .replace("<", "<") .replace(">", ">") .replace("\"", """) |