mirror of
https://github.com/redlib-org/redlib.git
synced 2025-05-14 14:02:50 +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
|
@ -3,6 +3,10 @@ use actix_web::{get, web, HttpResponse, Result};
|
|||
use askama::Template;
|
||||
use chrono::{TimeZone, Utc};
|
||||
|
||||
#[path = "utils.rs"]
|
||||
mod utils;
|
||||
pub use utils::{Flair, Post, Subreddit, val};
|
||||
|
||||
// STRUCTS
|
||||
#[derive(Template)]
|
||||
#[template(path = "subreddit.html", escape = "none")]
|
||||
|
@ -12,27 +16,6 @@ struct SubredditTemplate {
|
|||
sort: String,
|
||||
}
|
||||
|
||||
// Post flair with text, background color and foreground color
|
||||
pub struct Flair(pub String, pub String, pub String);
|
||||
|
||||
pub struct Post {
|
||||
pub title: String,
|
||||
pub community: String,
|
||||
pub author: String,
|
||||
pub score: String,
|
||||
pub image: String,
|
||||
pub url: String,
|
||||
pub time: String,
|
||||
pub flair: Flair,
|
||||
}
|
||||
|
||||
pub struct Subreddit {
|
||||
pub name: String,
|
||||
pub title: String,
|
||||
pub description: String,
|
||||
pub icon: String,
|
||||
}
|
||||
|
||||
async fn render(sub_name: String, sort: String) -> Result<HttpResponse> {
|
||||
let mut sub: Subreddit = subreddit(&sub_name).await;
|
||||
let posts: Vec<Post> = posts(sub_name, &sort).await;
|
||||
|
@ -60,11 +43,6 @@ async fn sorted(web::Path((sub, sort)): web::Path<(String, String)>) -> Result<H
|
|||
render(sub, sort).await
|
||||
}
|
||||
|
||||
// UTILITIES
|
||||
async fn val(j: &serde_json::Value, k: &str) -> String {
|
||||
String::from(j["data"][k].as_str().unwrap_or(""))
|
||||
}
|
||||
|
||||
// SUBREDDIT
|
||||
async fn subreddit(sub: &String) -> Subreddit {
|
||||
let url: String = format!("https://www.reddit.com/r/{}/about.json", sub);
|
||||
|
@ -105,9 +83,10 @@ pub async fn posts(sub: String, sort: &String) -> Vec<Post> {
|
|||
posts.push(Post {
|
||||
title: val(post, "title").await,
|
||||
community: val(post, "subreddit").await,
|
||||
body: String::new(),
|
||||
author: val(post, "author").await,
|
||||
score: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() },
|
||||
image: img,
|
||||
media: img,
|
||||
url: val(post, "permalink").await,
|
||||
time: Utc.timestamp(unix_time, 0).format("%b %e '%y").to_string(),
|
||||
flair: Flair(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue