Skip to content

Commit 7928e7d

Browse files
committed
fix(social): avoid writing the file twice
1 parent 49060ac commit 7928e7d

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "emile"
3-
version = "0.5.2"
3+
version = "0.5.3"
44
authors = ["Geobert Quach <[email protected]>"]
55
edition = "2021"
66

src/publish.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,22 @@ pub async fn publish_post(post: &Path, cfg: &SiteConfig) -> Result<String> {
5353
);
5454
}
5555

56-
fs::write(&dest, &new_content)?;
57-
fs::remove_file(&post)?;
58-
5956
if let Some(social_cfg) = cfg.social.as_ref() {
60-
push_to_social(social_cfg, &new_content, &dest).await?;
57+
match push_to_social(social_cfg, &new_content, &dest).await {
58+
Ok(new_content) => {
59+
fs::write(&dest, &new_content)?;
60+
fs::remove_file(&post)?;
61+
}
62+
Err(e) => {
63+
// write the post even if social media failed
64+
fs::write(&dest, &new_content)?;
65+
fs::remove_file(&post)?;
66+
return Err(e);
67+
}
68+
}
69+
} else {
70+
fs::write(&dest, &new_content)?;
71+
fs::remove_file(&post)?;
6172
}
6273

6374
Ok(dest.to_string_lossy().to_string())

src/social/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::{
1010
use anyhow::{anyhow, bail, Result};
1111
use reqwest::Url;
1212
use serde_derive::Deserialize;
13-
use tracing::error;
13+
use tracing::{error, info};
1414

1515
use crate::{
1616
config::{SocialApi, SocialCfg},
@@ -239,7 +239,7 @@ fn create_toot_link(
239239
Ok(tpl.replace("{links}", links))
240240
}
241241

242-
pub async fn push_to_social(cfg: &SocialCfg, content: &str, dest: &Path) -> Result<()> {
242+
pub async fn push_to_social(cfg: &SocialCfg, content: &str, dest: &Path) -> Result<String> {
243243
if cfg.instances.is_empty() {
244244
bail!("No social servers defined.");
245245
}
@@ -270,11 +270,12 @@ pub async fn push_to_social(cfg: &SocialCfg, content: &str, dest: &Path) -> Resu
270270
acc
271271
});
272272

273+
info!("Inject social links: {links:?}");
274+
273275
let new_content = content.replace(
274276
&cfg.link_tag,
275277
&create_toot_link(&templates_dir, cfg, &language, &links)?,
276278
);
277-
std::fs::write(dest, new_content)?;
278279

279-
Ok(())
280+
Ok(new_content)
280281
}

0 commit comments

Comments
 (0)