flowmark 0.1.3

Fast, modern Markdown formatter with smart typography and paragraph wrapping
Documentation
use std::fs;
use std::path::Path;
use std::process::Command;

fn main() {
    // Get Python version from python-repo submodule git tag
    let python_version = if Path::new("python-repo/.git").exists() {
        Command::new("git")
            .args(["describe", "--tags", "--always"])
            .current_dir("python-repo")
            .output()
            .ok()
            .and_then(|output| {
                if output.status.success() { String::from_utf8(output.stdout).ok() } else { None }
            })
            .map_or_else(read_metadata_version, |s| s.trim().to_string())
    } else {
        // Fallback to reading from Cargo.toml metadata when submodule not initialized
        read_metadata_version()
    };

    println!("cargo:rustc-env=PYTHON_SOURCE_VERSION={python_version}");
    println!("cargo:rerun-if-changed=python-repo/.git/HEAD");
    println!("cargo:rerun-if-changed=Cargo.toml");
}

fn read_metadata_version() -> String {
    // Read version from [package.metadata.python_source] in Cargo.toml
    let cargo_toml = fs::read_to_string("Cargo.toml").unwrap_or_default();
    cargo_toml
        .lines()
        .skip_while(|line| !line.contains("[package.metadata.python_source]"))
        .skip(1)
        .find(|line| line.trim().starts_with("version"))
        .and_then(|line| line.split('=').nth(1))
        .map_or_else(|| "unknown".to_string(), |v| v.trim().trim_matches('"').to_string())
}