use crate::Error;
use crate::dir_context::PathResolver;
use crate::runtime::Runtime;
use crate::support::files::{
hash_file_b58 as blake3_hash_file_b58, hash_file_b64 as blake3_hash_file_b64,
hash_file_b64u as blake3_hash_file_b64u, hash_file_hex as blake3_hash_file_hex, hash_file_sha256_b58,
hash_file_sha256_b64, hash_file_sha256_b64u, hash_file_sha256_hex, hash_file_sha512_b58, hash_file_sha512_b64,
hash_file_sha512_b64u, hash_file_sha512_hex,
};
use mlua::{IntoLua, Lua, Value};
fn hash_path_to_lua_value<F>(
lua: &Lua,
runtime: &Runtime,
path: String,
ctx: &'static str,
hash_fn: F,
) -> mlua::Result<Value>
where
F: FnOnce(simple_fs::SPath) -> crate::Result<String>,
{
let full_path =
runtime
.dir_context()
.resolve_path(runtime.session(), path.clone().into(), PathResolver::WksDir, None)?;
let hash_string =
hash_fn(full_path).map_err(|e| Error::from(format!("{ctx} - Failed to hash file '{path}'.\nCause: {e}")))?;
hash_string.into_lua(lua)
}
pub(super) fn file_hash_sha256(lua: &Lua, runtime: &Runtime, path: String) -> mlua::Result<Value> {
hash_path_to_lua_value(lua, runtime, path, "aip.file.hash_sha256", hash_file_sha256_hex)
}
pub(super) fn file_hash_sha256_b64(lua: &Lua, runtime: &Runtime, path: String) -> mlua::Result<Value> {
hash_path_to_lua_value(lua, runtime, path, "aip.file.hash_sha256_b64", hash_file_sha256_b64)
}
pub(super) fn file_hash_sha256_b64u(lua: &Lua, runtime: &Runtime, path: String) -> mlua::Result<Value> {
hash_path_to_lua_value(lua, runtime, path, "aip.file.hash_sha256_b64u", hash_file_sha256_b64u)
}
pub(super) fn file_hash_sha256_b58u(lua: &Lua, runtime: &Runtime, path: String) -> mlua::Result<Value> {
hash_path_to_lua_value(lua, runtime, path, "aip.file.hash_sha256_b58u", hash_file_sha256_b58)
}
pub(super) fn file_hash_sha512(lua: &Lua, runtime: &Runtime, path: String) -> mlua::Result<Value> {
hash_path_to_lua_value(lua, runtime, path, "aip.file.hash_sha512", hash_file_sha512_hex)
}
pub(super) fn file_hash_sha512_b64(lua: &Lua, runtime: &Runtime, path: String) -> mlua::Result<Value> {
hash_path_to_lua_value(lua, runtime, path, "aip.file.hash_sha512_b64", hash_file_sha512_b64)
}
pub(super) fn file_hash_sha512_b64u(lua: &Lua, runtime: &Runtime, path: String) -> mlua::Result<Value> {
hash_path_to_lua_value(lua, runtime, path, "aip.file.hash_sha512_b64u", hash_file_sha512_b64u)
}
pub(super) fn file_hash_sha512_b58u(lua: &Lua, runtime: &Runtime, path: String) -> mlua::Result<Value> {
hash_path_to_lua_value(lua, runtime, path, "aip.file.hash_sha512_b58u", hash_file_sha512_b58)
}
pub(super) fn file_hash_blake3(lua: &Lua, runtime: &Runtime, path: String) -> mlua::Result<Value> {
hash_path_to_lua_value(lua, runtime, path, "aip.file.hash_blake3", blake3_hash_file_hex)
}
pub(super) fn file_hash_blake3_b64(lua: &Lua, runtime: &Runtime, path: String) -> mlua::Result<Value> {
hash_path_to_lua_value(lua, runtime, path, "aip.file.hash_blake3_b64", blake3_hash_file_b64)
}
pub(super) fn file_hash_blake3_b64u(lua: &Lua, runtime: &Runtime, path: String) -> mlua::Result<Value> {
hash_path_to_lua_value(lua, runtime, path, "aip.file.hash_blake3_b64u", blake3_hash_file_b64u)
}
pub(super) fn file_hash_blake3_b58u(lua: &Lua, runtime: &Runtime, path: String) -> mlua::Result<Value> {
hash_path_to_lua_value(lua, runtime, path, "aip.file.hash_blake3_b58u", blake3_hash_file_b58)
}