forked from paupino/rust-decimal
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.rs
52 lines (49 loc) · 2.01 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use std::{fs, path::PathBuf};
fn main() {
println!("cargo:rerun-if-changed=README.md");
let readme = fs::read_to_string("README.md").unwrap();
let output = PathBuf::from(std::env::var("OUT_DIR").unwrap()).join("README-lib.md");
fs::write(output, prepare(&readme)).unwrap();
}
fn prepare(readme: &str) -> String {
// This is a naive implementation to get things off the ground.
// We just do a few things for this at the moment:
// 1. Strip header stuff
// 2. Replace the build document link
// 3. Replace serde examples with ignore flags (to avoid feature flagging configuration in docs)
let mut cleaned = String::new();
let mut body = false;
let mut feature_section = false;
let mut feature = String::new();
for line in readme.lines() {
if !body {
if line.starts_with("[docs]") {
body = true;
}
continue;
}
// Add the line as is, unless it contains "(BUILD.md)"
if line.contains("(BUILD.md)") {
cleaned.push_str(&line.replace(
"(BUILD.md)",
"(https://github.com/paupino/rust-decimal/blob/master/BUILD.md)",
));
} else if feature_section && line.starts_with("```rust") {
// This is a bit naive, but it's to make the Serde examples cleaner. Should probably
// be a bit more "defensive" here.
cleaned.push_str("```rust\n");
cleaned.push_str("# use rust_decimal::Decimal;\n");
cleaned.push_str("# use serde::{Serialize, Deserialize};\n");
cleaned.push_str(&format!("# #[cfg(features = \"{}\")]", feature));
} else {
if !feature_section && line.starts_with("## Features") {
feature_section = true;
} else if feature_section && line.starts_with("### ") {
feature = line.replace("### ", "").replace('`', "");
}
cleaned.push_str(line);
}
cleaned.push('\n');
}
cleaned
}