//! A simple command-line tool that calculates and displays the SHA-256 hash of
//! its own source file.
//!
//! This utility demonstrates how to use the `get_file_hash!` macro to obtain
//! the hash of a specified file at compile time and incorporate it into runtime
//! logic.

use get_file_hash_core::get_file_hash;
use sha2::{Digest, Sha256};

const README_TEMPLATE_PART1: &str = r##"# `get_file_hash` macro

This project provides a Rust procedural macro, `get_file_hash!`, designed to compute the SHA-256 hash of a specified file at compile time. This hash is then embedded directly into your compiled executable. This feature is invaluable for:

*   **Integrity Verification:** Ensuring the deployed code hasn't been tampered with.
*   **Versioning:** Embedding a unique identifier linked to the exact source code version.
*   **Cache Busting:** Generating unique names for assets based on their content.

## Project Structure

*   `get_file_hash_core`: A foundational crate containing the `get_file_hash!` macro definition.
*   `get_file_hash`: The main library crate that re-exports the macro.
*   `src/bin/get_file_hash.rs`: An example executable demonstrating the macro's usage by hashing its own source file and updating this `README.md`.
*   `build.rs`: A build script that also utilizes the `get_file_hash!` macro to hash `Cargo.toml` during the build process.

## Usage of `get_file_hash!` Macro

To use the `get_file_hash!` macro, ensure you have `get_file_hash` (or `get_file_hash_core` for direct usage) as a dependency in your `Cargo.toml`.

### Example

```rust
use get_file_hash::get_file_hash;
use sha2::{Digest, Sha256};

fn main() {
    // The macro resolves the path relative to CARGO_MANIFEST_DIR
    let file_hash = get_file_hash!("src/lib.rs");
    println!("The SHA-256 hash of src/lib.rs is: {}", file_hash);
}
```

"##;

const README_TEMPLATE_PART2: &str = r"## Setup and Building

1.  **Clone the repository:**
    ```bash
    git clone <repository-url>
    cd <repository-name>
    ```
2.  **Build the project:**
    ```bash
    cargo build
    ```
    During the build, `build.rs` will execute and print the hash of `Cargo.toml`.
3.  **Run the example executable:**
    ```bash
    cargo run --bin get_file_hash
    ```
    This will print the hash of `src/bin/get_file_hash.rs` to your console.

## Updating this `README.md`

The hash information in this `README.md` is automatically generated by running the example executable.
To update it, execute:

```bash
cargo run --bin get_file_hash > README.md
```

## Current File Hash Information (of `src/bin/get_file_hash.rs`)

*   **Target File:** `src/bin/get_file_hash.rs`
";

/// The main entry point of the application.
///
/// This function calculates the SHA-256 hash of the `get_file_hash.rs` source
/// file using a custom procedural macro and then prints the hash to the
/// console. It also includes a basic integrity verification check.
fn main() {
    // Calculate the SHA-256 hash of the current file (`get_file_hash.rs`) at
    // compile time. The `get_file_hash!` macro reads the file content and
    // computes its hash.
    let self_hash = get_file_hash!("get_file_hash.rs");

    let status_message = if self_hash.starts_with("e3b0") {
        "Warning: This hash represents an empty file."
    } else {
        "Integrity Verified."
    };

    print!("{}{}", README_TEMPLATE_PART1, README_TEMPLATE_PART2);
    println!("*   **SHA-256 Hash:** `{}`", self_hash);
    println!("*   **Status:** {}.\n", status_message);
}
