intermodal/bin/gen/src/readme.rs
Casey Rodarmor 342266853e
Improve bin/gen error messages
Create an error enum with actual error messages.

type: development
2020-04-29 00:36:25 -07:00

40 lines
1.1 KiB
Rust

use crate::common::*;
#[derive(Template)]
#[template(path = "README.md")]
pub(crate) struct Readme {
pub(crate) table_of_contents: String,
pub(crate) packages: Table<Package>,
}
const HEADING_PATTERN: &str = "(?m)^(?P<MARKER>#+) (?P<TEXT>.*)$";
impl Readme {
#[throws]
pub(crate) fn load(config: &Config, template: &Path) -> Readme {
let text = fs::read_to_string(template).context(error::Filesystem { path: template })?;
let header_re = Regex::new(HEADING_PATTERN)?;
let mut lines = Vec::new();
for captures in header_re.captures_iter(&text).skip(2) {
let marker = captures.name("MARKER").unwrap().as_str();
let text = captures.name("TEXT").unwrap().as_str();
let level = marker.len();
let indentation = " ".repeat((level - 2) * 2);
let slug = text
.to_lowercase()
.replace(' ', "-")
.replace('.', "")
.replace('&', "");
lines.push(format!("{}- [{}](#{})", indentation, text, slug));
}
Readme {
table_of_contents: lines.join("\n"),
packages: Table::new(config.packages.clone()),
}
}
}