use anyhow::{Context, Result};
use std::collections::BTreeMap;
use std::fs;
use std::path::Path;
pub struct ReadmeInfo {
pub title: Option<String>,
pub owner: String,
pub description: String,
pub meta: BTreeMap<String, serde_json::Value>,
}
fn extract_first_title(content: &str) -> Option<String> {
for line in content.lines() {
let trimmed = line.trim();
if trimmed.starts_with('#') {
let title = trimmed.trim_start_matches('#').trim();
if !title.is_empty() {
return Some(title.to_string());
}
}
}
None
}
fn read_readme_content(content: &str) -> String {
let mut found_first_title = false;
let mut lines_after_title: Vec<&str> = Vec::new();
for line in content.lines() {
let trimmed = line.trim();
if !found_first_title && trimmed.starts_with('#') {
found_first_title = true;
continue;
}
if found_first_title {
lines_after_title.push(line);
}
}
lines_after_title.join("\n").trim().to_string()
}
pub fn read_readme_info(readme_path: &Path) -> Result<ReadmeInfo> {
if !readme_path.exists() {
return Ok(ReadmeInfo {
title: None,
owner: "".to_string(),
description: "".to_string(),
meta: BTreeMap::new(),
});
}
let content = fs::read_to_string(readme_path)
.with_context(|| format!("could not read README file at `{}`", readme_path.display()))?;
let mut title: Option<String> = None;
let mut owner = "".to_string();
let mut description = "".to_string();
let mut meta: BTreeMap<String, serde_json::Value> = BTreeMap::new();
if let Some(stripped) = content.strip_prefix("---\n") {
if let Some(end_pos) = stripped.find("\n---\n") {
let yaml_content = &stripped[..end_pos];
let markdown_content = stripped[end_pos + 5..].to_string();
if let Ok(yaml_value) = serde_yaml::from_str::<serde_yaml::Value>(yaml_content)
&& let Some(mapping) = yaml_value.as_mapping()
{
for (key, value) in mapping {
if let Some(key_str) = key.as_str() {
if key_str == "owner" {
if let Some(owner_value) = value.as_str() {
owner = owner_value.to_string();
}
} else {
if let Ok(json_value) = serde_json::to_value(value) {
meta.insert(key_str.to_string(), json_value);
}
}
}
}
}
title = extract_first_title(&markdown_content);
description = read_readme_content(&markdown_content)
}
} else {
title = extract_first_title(&content);
description = read_readme_content(&content)
}
Ok(ReadmeInfo {
title,
owner,
description,
meta,
})
}