Skip to main content

automd_rs/
lib.rs

1//! Update README blocks from Cargo.toml; handler dispatches by block name to generators.
2
3pub mod error;
4pub mod generators;
5pub mod handler;
6pub mod parser;
7
8pub use error::{Error, Result};
9pub use handler::{BlockHandler, DefaultHandler, UpdateContext};
10pub use parser::cargo::{ParsedManifest, parse as parse_manifest};
11pub use parser::readme::{
12    BlockRequest, assign_and_generate, parse_readme_blocks, replace_blocks_once, update_readme,
13};
14
15use std::path::Path;
16use urlogger::{LogLevel, log};
17
18pub fn run(manifest_dir: &Path, readme_path: &Path) -> Result<String> {
19    run_with_handler(manifest_dir, readme_path, &DefaultHandler::default())
20}
21
22/// Run with custom handler: parse Cargo.toml → parse README → generate per block → replace once.
23pub fn run_with_handler(
24    manifest_dir: &Path,
25    readme_path: &Path,
26    handler: &dyn BlockHandler,
27) -> Result<String> {
28    let config = parser::cargo::parse(manifest_dir)?;
29    log!(LogLevel::Trace, "config: {:?}", config);
30
31    let readme_content = std::fs::read_to_string(readme_path)?;
32
33    let context = UpdateContext::new(config, manifest_dir.to_path_buf());
34    log!(LogLevel::Trace, "context: {:?}", context);
35
36    let updated = parser::readme::replace_blocks_with_handler(&readme_content, handler, &context)?;
37
38    std::fs::write(readme_path, &updated)?;
39    Ok(updated)
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn test_run_with_handler() {
48        let dir = std::env::temp_dir().join("automd_rs_test");
49        let _ = std::fs::create_dir_all(&dir);
50        let cargo_toml = dir.join("Cargo.toml");
51        let readme = dir.join("README.md");
52        std::fs::write(
53            &cargo_toml,
54            r#"
55[package]
56name = "test-pkg"
57version = "0.1.0"
58description = "d"
59repository = "https://github.com/foo/bar.git"
60"#,
61        )
62        .unwrap();
63        std::fs::write(
64            &readme,
65            "Title\n\n<!-- automdrs:badges version -->\n<!-- /automdrs -->\n",
66        )
67        .unwrap();
68        let result = run_with_handler(&dir, &readme, &crate::handler::DefaultHandler::default());
69        let out = result.unwrap();
70        assert!(out.contains("crates/v/test-pkg"));
71        assert!(out.contains("<!-- automdrs:badges version -->"));
72        let _ = std::fs::remove_dir_all(&dir);
73    }
74
75    #[test]
76    fn test_run() {
77        let dir = std::env::temp_dir().join("automd_rs_test_run");
78        let _ = std::fs::create_dir_all(&dir);
79        let cargo_toml = dir.join("Cargo.toml");
80        let readme = dir.join("README.md");
81        std::fs::write(
82            &cargo_toml,
83            r#"
84[package]
85name = "run-pkg"
86version = "0.1.0"
87description = "d"
88repository = "https://github.com/a/b.git"
89"#,
90        )
91        .unwrap();
92        std::fs::write(
93            &readme,
94            "Hi\n<!-- automdrs:with-automdrs -->\n<!-- /automdrs -->\n",
95        )
96        .unwrap();
97        let result = run(&dir, &readme);
98        assert!(result.is_ok());
99        let out = result.unwrap();
100        assert!(out.contains("automd-rs"));
101        let _ = std::fs::remove_dir_all(&dir);
102    }
103}