zero4rs 1.3.21

zero4rs is a powerful, pragmatic, and extremely fast web framework for Rust
Documentation
use std::env;
use std::fs::File;
use std::io::Write;

// {arch}-{vendor}-{sys}-{abi}.
// https://doc.rust-lang.org/reference/conditional-compilation.html
fn main() {
    // BUILDING_INFO='-X main.Name=$(APP_NAME) -X main.Author=$(COMMITER) -X main.Commit=$(GIT_HASH) -X main.Time=$(TIME)'

    let app_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
    let app_name = app_dir + "/" + &std::env::var("CARGO_PKG_NAME").unwrap();
    let author = std::env::var("CARGO_PKG_AUTHORS").unwrap();
    let mut git_version = std::env::var("CARGO_PKG_VERSION").unwrap();
    let mut build_time = "1694885701".to_string();

    if let Ok(input) = std::env::var("BUILDING_INFO") {
        let parts: Vec<&str> = input.split("-X ").collect();

        for part in &parts {
            if let Some(value) = part.trim().strip_prefix("main.Commit=") {
                git_version = value
                    .trim()
                    .trim_end_matches('\'')
                    .trim_start_matches('\'')
                    .to_string();
            } else if let Some(value) = part.trim().strip_prefix("main.Time=") {
                build_time = value
                    .trim()
                    .trim_end_matches('\'')
                    .trim_start_matches('\'')
                    .to_string();
            }
        }
    }

    let target_family = if cfg!(target_family = "unix") {
        "Linux"
    } else if cfg!(target_os = "windows") {
        "macOS"
    } else {
        "unknowos"
    };

    let target_os = if cfg!(target_os = "linux") {
        "Linux"
    } else if cfg!(target_os = "macos") {
        "macOS"
    } else if cfg!(target_os = "windows") {
        "windows"
    } else {
        "unknowos"
    };

    // Get the target architecture
    let target_arch = if cfg!(target_arch = "x86") {
        "32-Bit x86"
    } else if cfg!(target_arch = "x86_64") {
        "64-Bit x86_64"
    } else if cfg!(target_arch = "aarch64") {
        "64-Bit ARM64"
    } else {
        "unknow"
    };

    let dest_path = std::path::Path::new(&env::var("OUT_DIR").unwrap()).join("build_info.rs");
    let mut f = File::create(dest_path).unwrap();

    let build_info = format!(
        "\
            pub const GIT_VERSION: &str = \"{}\";\n\
            pub const MAIN_AUTHOR: &str = \"{}\";\n\
            pub const APP_NAME: &str = \"{}\";\n\
            pub const BUILDING_TIME: i64 = {};\n\
            pub const TARGET_OS: &str = \"{} {}\";\n\
            pub const TARGET_ARCH: &str = \"{}\";\n\
        ",
        git_version.trim(),
        author.trim(),
        app_name.trim().trim_matches(|c| c == ' '),
        build_time,
        target_os,
        target_family,
        target_arch
    );

    f.write_all(build_info.as_bytes()).unwrap();
}