Fix mistakes
This commit is contained in:
parent
9f19d729d1
commit
312d162c09
@ -1,7 +1,8 @@
|
|||||||
// CRATES
|
// CRATES
|
||||||
use crate::utils::{fetch_posts, format_url, request, val, ErrorTemplate, Params, Post, Subreddit};
|
use crate::utils::{fetch_posts, format_url, request, val, ErrorTemplate, Params, Post, Subreddit, format_num};
|
||||||
use actix_web::{get, http::StatusCode, web, HttpResponse, Result};
|
use actix_web::{get, http::StatusCode, web, HttpResponse, Result};
|
||||||
use askama::Template;
|
use askama::Template;
|
||||||
|
use std::convert::TryInto;
|
||||||
|
|
||||||
// STRUCTS
|
// STRUCTS
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
@ -89,8 +90,8 @@ async fn subreddit(sub: &String) -> Result<Subreddit, &'static str> {
|
|||||||
title: val(&res, "title").await,
|
title: val(&res, "title").await,
|
||||||
description: val(&res, "public_description").await,
|
description: val(&res, "public_description").await,
|
||||||
icon: format_url(val(&res, "icon_img").await.as_str()).await,
|
icon: format_url(val(&res, "icon_img").await.as_str()).await,
|
||||||
members: format_num(members),
|
members: format_num(members.try_into().unwrap()),
|
||||||
active: format_num(active),
|
active: format_num(active.try_into().unwrap()),
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(sub)
|
Ok(sub)
|
||||||
|
14
src/utils.rs
14
src/utils.rs
@ -76,7 +76,7 @@ pub struct ErrorTemplate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// URL HANDLING
|
// FORMATTING
|
||||||
//
|
//
|
||||||
|
|
||||||
pub async fn format_url(url: &str) -> String {
|
pub async fn format_url(url: &str) -> String {
|
||||||
@ -87,6 +87,10 @@ pub async fn format_url(url: &str) -> String {
|
|||||||
return url.to_string();
|
return url.to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn format_num(num: i64) -> String {
|
||||||
|
return if num > 1000 { format!("{}k", num / 1000) } else { num.to_string() };
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// JSON PARSING
|
// JSON PARSING
|
||||||
//
|
//
|
||||||
@ -136,7 +140,7 @@ pub async fn fetch_posts(url: String, fallback_title: String) -> Result<(Vec<Pos
|
|||||||
community: val(post, "subreddit").await,
|
community: val(post, "subreddit").await,
|
||||||
body: val(post, "body").await,
|
body: val(post, "body").await,
|
||||||
author: val(post, "author").await,
|
author: val(post, "author").await,
|
||||||
score: format_score(score),
|
score: format_num(score),
|
||||||
post_type: "link".to_string(),
|
post_type: "link".to_string(),
|
||||||
media: img,
|
media: img,
|
||||||
url: val(post, "permalink").await,
|
url: val(post, "permalink").await,
|
||||||
@ -202,9 +206,3 @@ pub async fn request(url: String) -> Result<serde_json::Value, &'static str> {
|
|||||||
Ok(json)
|
Ok(json)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// FORMATTING
|
|
||||||
pub fn format_num(num: i64) -> String {
|
|
||||||
return if num > 1000 { format!("{}k", num / 1000) } else { num.to_string() };
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user