From ec3ca6841cee55944868965fc006e28359f07558 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Wed, 29 Sep 2021 08:36:49 -0400 Subject: [PATCH] use less ram Signed-off-by: Christine Dodrill --- src/post/mod.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/post/mod.rs b/src/post/mod.rs index f1fbaef..6e629ab 100644 --- a/src/post/mod.rs +++ b/src/post/mod.rs @@ -2,7 +2,7 @@ use chrono::prelude::*; use color_eyre::eyre::{eyre, Result, WrapErr}; use glob::glob; use serde::Serialize; -use std::{cmp::Ordering, path::PathBuf}; +use std::{borrow::Borrow, cmp::Ordering, path::PathBuf}; use tokio::fs; pub mod frontmatter; @@ -81,7 +81,7 @@ impl Post { } } -async fn read_post(dir: &str, fname: PathBuf) -> Result { +async fn read_post(dir: &str, fname: PathBuf, cli: &Option) -> Result { let body = fs::read_to_string(fname.clone()) .await .wrap_err_with(|| format!("can't read {:?}", fname))?; @@ -98,8 +98,8 @@ async fn read_post(dir: &str, fname: PathBuf) -> Result { .with_timezone(&Utc) .into(); - let mentions: Vec = match std::env::var("MI_TOKEN") { - Ok(token) => mi::Client::new(token.to_string(), crate::APPLICATION_NAME.to_string())? + let mentions: Vec = match cli { + Some(cli) => cli .mentioners(format!("https://christine.website/{}", link)) .await .map_err(|why| tracing::error!("error: can't load mentions for {}: {}", link, why)) @@ -109,7 +109,7 @@ async fn read_post(dir: &str, fname: PathBuf) -> Result { wm.title.as_ref().unwrap_or(&"".to_string()) != &"Bridgy Response".to_string() }) .collect(), - Err(_) => vec![], + None => vec![], }; let time_taken = estimated_read_time::text( @@ -140,9 +140,14 @@ async fn read_post(dir: &str, fname: PathBuf) -> Result { } pub async fn load(dir: &str) -> Result> { + let cli = match std::env::var("MI_TOKEN") { + Ok(token) => mi::Client::new(token.to_string(), crate::APPLICATION_NAME.to_string()).ok(), + Err(_) => None, + }; + let futs = glob(&format!("{}/*.markdown", dir))? .filter_map(Result::ok) - .map(|fname| read_post(dir, fname)); + .map(|fname| read_post(dir, fname, cli.borrow())); let mut result: Vec = futures::future::join_all(futs) .await