xd 0.0.1

tool that dumps binary input in a variety of formats
Documentation
//! Generates a README that prepends CARGO_PKG_REPOSITORY, at the tag
//! with the same name as CARGO_PKG_VERSION, to each image URL.
//!
//! rust-lang/crates.io can do this for a CARGO_PKG_REPOSITORY on
//! Bitbucket, but currently hard-codes “master” as the commit, which
//! isn’t the best for Mercurial repositories, and even when valid,
//! might not be the same commit from which a README was rendered.
//!
//! We can only write to OUT_DIR, otherwise package verification will
//! fail, but the manifest’s readme field can’t refer to paths under
//! OUT_DIR because it’s unpredictable, so we keep a copy in the root
//! directory. tests/build.rs ensures that this copy is up to date.

use std::env;
use std::fs::File;
use std::io::{BufRead, BufReader, Write};
use std::path::Path;

use jane_eyre::Result;
use regex::Regex;

fn main() -> Result<()> {
    jane_eyre::install()?;

    let pattern = Regex::new(
        r"
            (?x)^
            !\[
                (?P<alt>[^\]]*)
            \]\(
                (?P<src>[^)]*)
            \)
            $
        ",
    )?;

    let replacement = format!(
        "![$alt]({}/raw/{}/$src)",
        env!("CARGO_PKG_REPOSITORY"),
        env!("CARGO_PKG_VERSION")
    );

    let original = BufReader::new(File::open("README.md")?);
    let out_dir = env::var("OUT_DIR")?;
    let path = Path::new(&*out_dir).join("README.registry.md");
    let mut registry = File::create(path)?;

    for line in original.lines() {
        writeln!(registry, "{}", pattern.replace(&line?, &*replacement))?;
    }

    Ok(())
}