embedded files

This commit is contained in:
Vomitblood 2024-03-25 00:29:42 +08:00
parent f19fdadb78
commit 7fe497ebb1
3 changed files with 80 additions and 55 deletions

56
Cargo.lock generated
View file

@ -11,6 +11,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "anyhow"
version = "1.0.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0952808a6c2afd1aa8947271f3a60f1a6763c7b912d210184c5149b5cf147247"
[[package]] [[package]]
name = "atty" name = "atty"
version = "0.2.14" version = "0.2.14"
@ -60,6 +66,12 @@ dependencies = [
"wasi", "wasi",
] ]
[[package]]
name = "glob"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.1.19" version = "0.1.19"
@ -69,6 +81,30 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "include_dir"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24b56e147e6187d61e9d0f039f10e070d0c0a887e24fe0bb9ca3f29bfde62cab"
dependencies = [
"glob",
"include_dir_impl",
"proc-macro-hack",
]
[[package]]
name = "include_dir_impl"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a0c890c85da4bab7bce4204c707396bbd3c6c8a681716a51c8814cfc2b682df"
dependencies = [
"anyhow",
"proc-macro-hack",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.10" version = "1.0.10"
@ -92,6 +128,7 @@ name = "pokerust"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clap", "clap",
"include_dir",
"once_cell", "once_cell",
"rand", "rand",
"serde", "serde",
@ -104,6 +141,12 @@ version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "proc-macro-hack"
version = "0.5.20+deprecated"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc375e1527247fe1a97d8b7156678dfe7c1af2fc075c9a4db3690ecd2a148068"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.79" version = "1.0.79"
@ -175,7 +218,7 @@ checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.55",
] ]
[[package]] [[package]]
@ -195,6 +238,17 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.55" version = "2.0.55"

View file

@ -6,8 +6,10 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
rand = "0.8" rand = "0.8"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
once_cell = "1.8.0" once_cell = "1.8.0"
clap = { version = "2" } clap = { version = "2" }
include_dir = "0.6.0"

View file

@ -1,16 +1,9 @@
use rand::Rng; use rand::Rng;
use std::io::prelude::*;
// set global constants // set global constants
const PROGRAM_DIR: once_cell::sync::Lazy<std::path::PathBuf> = once_cell::sync::Lazy::new(|| { const COLORSCRIPTS_DIR: include_dir::Dir = include_dir::include_dir!("./colorscripts");
std::path::PathBuf::from(std::env::current_exe().unwrap().parent().unwrap())
});
const COLORSCRIPTS_DIR: once_cell::sync::Lazy<std::path::PathBuf> = const POKEMON_JSON: &str = std::include_str!("../pokemon.json");
once_cell::sync::Lazy::new(|| PROGRAM_DIR.join("colorscripts"));
const POKEMON_DATA_PATH: once_cell::sync::Lazy<std::path::PathBuf> =
once_cell::sync::Lazy::new(|| PROGRAM_DIR.join("pokemon.json"));
const REGULAR_SUBDIR: &str = "regular"; const REGULAR_SUBDIR: &str = "regular";
const SHINY_SUBDIR: &str = "shiny"; const SHINY_SUBDIR: &str = "shiny";
@ -31,44 +24,21 @@ const GENERATIONS: [(&str, (u32, u32)); 8] = [
("8", (810, 898)), ("8", (810, 898)),
]; ];
fn validate_files() -> std::io::Result<()> { fn print_file(filepath: &str) -> std::io::Result<()> {
let colorscripts_dir = COLORSCRIPTS_DIR; if let Some(file) = COLORSCRIPTS_DIR.get_file(filepath) {
let pokemon_data_path = POKEMON_DATA_PATH; let content = std::str::from_utf8(file.contents()).unwrap();
println!("{}", content);
if !colorscripts_dir.exists() {
println!(
"Colorscripts directory not found at {}",
colorscripts_dir.display()
);
std::process::exit(1);
}
if !pokemon_data_path.exists() {
println!(
"Pokemon data file not found at {}",
pokemon_data_path.display()
);
std::process::exit(1);
}
Ok(()) Ok(())
} } else {
Err(std::io::Error::new(
fn print_file(filepath: &std::path::Path) -> std::io::Result<()> { std::io::ErrorKind::NotFound,
let file = std::fs::File::open(filepath)?; "File not found",
let reader = std::io::BufReader::new(file); ))
for line in reader.lines() {
println!("{}", line?);
} }
Ok(())
} }
fn list_pokemon_names() -> std::io::Result<()> { fn list_pokemon_names() -> std::io::Result<()> {
let file = std::fs::File::open(POKEMON_DATA_PATH.as_path())?; let pokemon_json: serde_json::Value = serde_json::from_str(POKEMON_JSON)?;
let reader = std::io::BufReader::new(file);
let pokemon_json: serde_json::Value = serde_json::from_reader(reader)?;
let mut count = 0; let mut count = 0;
@ -99,13 +69,10 @@ fn show_pokemon_by_name(
form: Option<&str>, form: Option<&str>,
) -> std::io::Result<()> { ) -> std::io::Result<()> {
// set variables // set variables
let base_path = COLORSCRIPTS_DIR;
let color_subdir = if shiny { SHINY_SUBDIR } else { REGULAR_SUBDIR }; let color_subdir = if shiny { SHINY_SUBDIR } else { REGULAR_SUBDIR };
let size_subdir = if is_large { LARGE_SUBDIR } else { SMALL_SUBDIR }; let size_subdir = if is_large { LARGE_SUBDIR } else { SMALL_SUBDIR };
let file = std::fs::File::open(POKEMON_DATA_PATH.as_path())?; let pokemon_json: serde_json::Value = serde_json::from_str(POKEMON_JSON)?;
let reader = std::io::BufReader::new(file);
let pokemon_json: serde_json::Value = serde_json::from_reader(reader)?;
let pokemon_names: Vec<&str> = pokemon_json let pokemon_names: Vec<&str> = pokemon_json
.as_array() .as_array()
@ -150,8 +117,6 @@ fn show_pokemon_by_name(
} }
} }
let pokemon_file = base_path.join(size_subdir).join(color_subdir).join(&name);
if show_title { if show_title {
if shiny { if shiny {
println!("{} (shiny)", name); println!("{} (shiny)", name);
@ -160,7 +125,13 @@ fn show_pokemon_by_name(
} }
} }
print_file(&pokemon_file) // Construct the embedded file path
let file_path = format!("{}/{}/{}", size_subdir, color_subdir, name);
// Use the adjusted function to print file contents from embedded resources
print_file(&file_path)?;
Ok(())
} }
fn show_random_pokemon( fn show_random_pokemon(
@ -184,9 +155,7 @@ fn show_random_pokemon(
generations generations
}; };
let file = std::fs::File::open(POKEMON_DATA_PATH.as_path())?; let pokemon_json: serde_json::Value = serde_json::from_str(POKEMON_JSON)?;
let reader = std::io::BufReader::new(file);
let pokemon_json: serde_json::Value = serde_json::from_reader(reader)?;
let pokemon: Vec<String> = pokemon_json let pokemon: Vec<String> = pokemon_json
.as_array() .as_array()
.unwrap() .unwrap()
@ -215,7 +184,7 @@ fn show_random_pokemon(
fn main() { fn main() {
// validate files first // validate files first
validate_files().unwrap(); // validate_files().unwrap();
let matches = clap::App::new("pokemon-colorscripts") let matches = clap::App::new("pokemon-colorscripts")
.about("CLI utility to print out unicode image of a pokemon in your shell") .about("CLI utility to print out unicode image of a pokemon in your shell")