Debian Codegraph @

third_party/rust/rust_decimal/build.rs

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