Make demo use JSON, include compile time, sha256 hash, arg option

This commit is contained in:
2024-12-21 22:26:17 -06:00
parent bb7eada73c
commit 9aaefc7bb4
5 changed files with 499 additions and 5 deletions

View File

@@ -1,5 +1,34 @@
static LONG_STRING: &'static str = include_str!(concat!(env!("OUT_DIR"), "/long_string.txt"));
use serde::{Deserialize, Serialize};
use sha2::Digest;
// Shared between build.rs and main.rs
#[derive(Serialize, Deserialize, Debug)]
struct KeyData<'a> {
value: &'a str,
value_hash: String,
compile_time: String,
}
static KEY: &'static str = include_str!(concat!(env!("OUT_DIR"), "/key.json"));
fn main() {
println!("This package was compiled at {}", LONG_STRING);
let key_data: KeyData = serde_json::from_str(KEY).unwrap();
// Print the key data
let args: Vec<String> = std::env::args().collect();
if args.contains(&"--help".to_string()) {
println!("Usage: {} [--json] [--help]", args[0]);
println!("--json: Print the key data as JSON");
println!("--help: Print this help message");
return;
} else if args.contains(&"--json".to_string()) {
println!("{}", serde_json::to_string_pretty(&key_data).unwrap());
return;
}
// Check the hash of the value
let value_hash = sha2::Sha256::digest(key_data.value.as_bytes());
let hash_match = hex::encode(value_hash) == key_data.value_hash;
println!("Hash match: {}", hash_match);
}