mirror of
https://github.com/redlib-org/redlib.git
synced 2025-05-04 20:30:37 +00:00
Create utils.rs for Utilities
This commit is contained in:
parent
4350d5b7b3
commit
2c06ae1d8f
8 changed files with 90 additions and 101 deletions
30
src/post.rs
30
src/post.rs
|
@ -4,6 +4,10 @@ use askama::Template;
|
|||
use chrono::{TimeZone, Utc};
|
||||
use pulldown_cmark::{html, Options, Parser};
|
||||
|
||||
#[path = "utils.rs"]
|
||||
mod utils;
|
||||
use utils::{Comment, Flair, Post, val};
|
||||
|
||||
// STRUCTS
|
||||
#[derive(Template)]
|
||||
#[template(path = "post.html", escape = "none")]
|
||||
|
@ -13,28 +17,6 @@ struct PostTemplate {
|
|||
sort: String,
|
||||
}
|
||||
|
||||
// Post flair with text, background color and foreground color
|
||||
pub struct Flair(String, String, String);
|
||||
|
||||
pub struct Post {
|
||||
pub title: String,
|
||||
pub community: String,
|
||||
pub body: String,
|
||||
pub author: String,
|
||||
pub url: String,
|
||||
pub score: String,
|
||||
pub media: String,
|
||||
pub time: String,
|
||||
pub flair: Flair,
|
||||
}
|
||||
|
||||
pub struct Comment {
|
||||
pub body: String,
|
||||
pub author: String,
|
||||
pub score: String,
|
||||
pub time: String,
|
||||
}
|
||||
|
||||
async fn render(id: String, sort: String) -> Result<HttpResponse> {
|
||||
println!("id: {}", id);
|
||||
let post: Post = fetch_post(&id).await;
|
||||
|
@ -70,10 +52,6 @@ async fn sorted(web::Path((_sub, id, _title, sort)): web::Path<(String, String,
|
|||
}
|
||||
|
||||
// UTILITIES
|
||||
async fn val(j: &serde_json::Value, k: &str) -> String {
|
||||
String::from(j["data"][k].as_str().unwrap_or(""))
|
||||
}
|
||||
|
||||
async fn media(data: &serde_json::Value) -> String {
|
||||
let post_hint: &str = data["data"]["post_hint"].as_str().unwrap_or("");
|
||||
let has_media: bool = data["data"]["media"].is_object();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue