use std::{fs, io, path::Path};
use serde::Deserialize;
use toml::value::Datetime;
use crate::frontmatter_writeback::split_frontmatter;
#[derive(Debug, Default, Deserialize)]
pub struct LinkedIn {
pub description: Option<String>,
pub description_file: Option<String>,
pub created: Option<Datetime>,
pub published: Option<Datetime>,
}
impl LinkedIn {
pub fn post_text(&self, dir: &Path) -> Result<Option<String>, io::Error> {
if let Some(ref file) = self.description_file {
let text = fs::read_to_string(dir.join(file))?;
Ok(Some(text.trim_end().to_string()))
} else {
Ok(self.description.clone())
}
}
}
#[derive(Debug, Deserialize)]
pub struct FrontMatter {
pub title: Option<String>,
pub description: Option<String>,
pub date: Option<Datetime>,
pub linkedin: Option<LinkedIn>,
}
impl FrontMatter {
pub fn from_content(content: &str) -> Result<Self, toml::de::Error> {
let fm_str = split_frontmatter(content)
.map(|(_, fm, _)| fm)
.unwrap_or("");
toml::from_str(fm_str)
}
}
#[cfg(test)]
mod tests {
use std::fs;
use tempfile::tempdir;
use super::*;
#[test]
fn test_frontmatter_parses_linkedin_description() {
let content = "+++\ntitle = \"My Post\"\n\n[linkedin]\ndescription = \"Hello LinkedIn\"\n+++\n\nBody.\n";
let fm = FrontMatter::from_content(content).unwrap();
let li = fm.linkedin.expect("[linkedin] section should be present");
assert_eq!(li.description.as_deref(), Some("Hello LinkedIn"));
}
#[test]
fn test_frontmatter_parses_description_file() {
let content = "+++\ntitle = \"My Post\"\n\n[linkedin]\ndescription_file = \"linkedin.md\"\n+++\n\nBody.\n";
let fm = FrontMatter::from_content(content).unwrap();
let li = fm.linkedin.expect("[linkedin] section should be present");
assert_eq!(li.description_file.as_deref(), Some("linkedin.md"));
assert!(li.description.is_none());
}
#[test]
fn test_frontmatter_linkedin_absent() {
let content = "+++\ntitle = \"My Post\"\ndate = 2026-04-03\n+++\n\nBody.\n";
let fm = FrontMatter::from_content(content).unwrap();
assert!(fm.linkedin.is_none());
}
#[test]
fn test_frontmatter_parses_created_and_published() {
let content = "+++\ntitle = \"T\"\n\n[linkedin]\ndescription = \"d\"\ncreated = 2026-04-01\npublished = 2026-04-03\n+++\n\nBody.\n";
let fm = FrontMatter::from_content(content).unwrap();
let li = fm.linkedin.unwrap();
assert!(li.created.is_some());
assert!(li.published.is_some());
assert_eq!(li.created.unwrap().to_string(), "2026-04-01");
assert_eq!(li.published.unwrap().to_string(), "2026-04-03");
}
#[test]
fn test_frontmatter_parses_date() {
let content = "+++\ntitle = \"My Post\"\ndate = 2026-04-03\n+++\n\nBody.\n";
let fm = FrontMatter::from_content(content).unwrap();
assert!(fm.date.is_some());
assert_eq!(fm.date.unwrap().to_string(), "2026-04-03");
}
#[test]
fn test_post_text_returns_inline_description() {
let li = LinkedIn {
description: Some("Inline text".to_string()),
description_file: None,
created: None,
published: None,
};
let result = li.post_text(Path::new("/any")).unwrap();
assert_eq!(result.as_deref(), Some("Inline text"));
}
#[test]
fn test_post_text_returns_none_when_absent() {
let li = LinkedIn::default();
let result = li.post_text(Path::new("/any")).unwrap();
assert!(result.is_none());
}
#[test]
fn test_post_text_reads_from_file() {
let dir = tempdir().unwrap();
fs::write(dir.path().join("linkedin.md"), "File-based post text").unwrap();
let li = LinkedIn {
description: Some("should be ignored".to_string()),
description_file: Some("linkedin.md".to_string()),
created: None,
published: None,
};
let result = li.post_text(dir.path()).unwrap();
assert_eq!(result.as_deref(), Some("File-based post text"));
}
#[test]
fn test_post_text_file_not_found_returns_error() {
let dir = tempdir().unwrap();
let li = LinkedIn {
description: None,
description_file: Some("nonexistent.md".to_string()),
created: None,
published: None,
};
let result = li.post_text(dir.path());
assert!(result.is_err());
}
#[test]
fn test_description_file_takes_precedence_over_description() {
let dir = tempdir().unwrap();
fs::write(dir.path().join("post.md"), "From file").unwrap();
let li = LinkedIn {
description: Some("inline".to_string()),
description_file: Some("post.md".to_string()),
created: None,
published: None,
};
let result = li.post_text(dir.path()).unwrap();
assert_eq!(result.as_deref(), Some("From file"));
}
}