use crate::prelude::*;
use shiori3::*;
use std::path::Path;
use std::path::PathBuf;
pub fn lua_search_path<P: AsRef<Path>>(
load_dir_path: P,
ext: &str,
) -> MyResult<(PathBuf, String, String, PathBuf)> {
let load_dir_path = {
let mut buf = load_dir_path.as_ref().to_path_buf();
buf.push("a");
buf.pop();
buf
};
let load_dir = {
let a = load_dir_path.to_str();
let b = a.ok_or_else(|| ShioriError::from(ShioriErrorKind::Load))?;
String::from(b)
};
let mut lua_path = String::default();
{
let mut add_path = |root: &str| {
let mut add_path = |pre: &str| {
if !lua_path.is_empty() {
lua_path.push(';');
}
lua_path.push_str(&load_dir);
lua_path.push_str(root);
lua_path.push_str(pre);
lua_path.push_str(ext);
};
add_path("\\?.");
add_path("\\?\\init.");
};
add_path("\\profile\\emo\\save");
add_path("\\dic");
add_path("\\emo");
}
let save_dir = {
let mut dir = load_dir_path.to_owned();
dir.push("profile");
dir.push("emo");
dir.push("save");
dir
};
Ok((load_dir_path, load_dir, lua_path, save_dir))
}
#[cfg(any(windows))]
#[test]
fn lua_search_path_test() {
{
let (dir, _, path, _) =
lua_search_path("c:\\留袖 綺麗ね\\ごーすと\\", "lua").unwrap();
assert_eq!(dir.to_string_lossy(), "c:\\留袖 綺麗ね\\ごーすと");
assert_eq!(path, "c:\\留袖 綺麗ね\\ごーすと\\profile\\emo\\save\\?.lua;c:\\留袖 綺麗ね\\ごーすと\\profile\\emo\\save\\?\\init.lua;c:\\留袖 綺麗ね\\ごーすと\\dic\\?.lua;c:\\留袖 綺麗ね\\ごーすと\\dic\\?\\init.lua;c:\\留袖 綺麗ね\\ごーすと\\emo\\?.lua;c:\\留袖 綺麗ね\\ごーすと\\emo\\?\\init.lua");
}
}