use std::{
fs::File,
io::{BufWriter, Write},
path::{Path, PathBuf},
};
use walkdir::WalkDir;
use crate::parser::{RawsStyleSerializable, TypedJsonSerializable};
pub fn subdirectories(directory: PathBuf) -> Option<Vec<walkdir::DirEntry>> {
if !(directory.exists() && directory.is_dir()) {
return None;
}
Some(
WalkDir::new(directory)
.max_depth(1)
.min_depth(1)
.into_iter()
.filter_map(std::result::Result::ok)
.filter(|e| e.file_type().is_dir())
.collect(),
)
}
pub fn get_parent_dir_name<P: AsRef<Path>>(full_path: &P) -> String {
match full_path.as_ref().parent() {
Some(parent_dir) => {
String::from(parent_dir.file_name().unwrap_or_default().to_string_lossy())
}
None => String::from("!Unavailable!"),
}
}
pub fn write_json_string_to_file<P: AsRef<Path>>(parsed_json_string: &String, out_filepath: &P) {
log::info!("Saving json to to {:?}", out_filepath.as_ref().display());
let out_file = match File::create(out_filepath) {
Ok(f) => f,
Err(e) => {
log::error!(
"Unable to open {} for writing \n{:?}",
out_filepath.as_ref().display(),
e
);
return;
}
};
let mut stream = BufWriter::new(out_file);
let write_error = &format!(
"Unable to write to {}",
out_filepath.as_ref().to_string_lossy()
);
match write!(stream, "[") {
Ok(_x) => (),
Err(e) => {
log::error!("{}\n{:?}", write_error, e);
return;
}
};
match write!(stream, "{parsed_json_string}") {
Ok(_x) => (),
Err(e) => {
log::error!("{}\n{:?}", write_error, e);
return;
}
};
match write!(stream, "]") {
Ok(_x) => (),
Err(e) => {
log::error!("{}\n{:?}", write_error, e);
return;
}
};
match stream.flush() {
Ok(_x) => (),
Err(e) => {
log::error!("{}\n{:?}", write_error, e);
}
};
}
#[allow(dead_code)]
pub fn write_json_string_vec_to_file<P: AsRef<Path>>(
parsed_json_string_vec: &Vec<String>,
out_filepath: &P,
) {
log::info!("Saving json to to {:?}", out_filepath.as_ref().display());
let out_file = match File::create(out_filepath) {
Ok(f) => f,
Err(e) => {
log::error!(
"Unable to open {} for writing \n{:?}",
out_filepath.as_ref().display(),
e
);
return;
}
};
let mut stream = BufWriter::new(out_file);
let write_error = &format!(
"Unable to write to {}",
out_filepath.as_ref().to_string_lossy()
);
match write!(stream, "[") {
Ok(_x) => (),
Err(e) => {
log::error!("{}\n{:?}", write_error, e);
return;
}
};
for parsed_json_string in parsed_json_string_vec {
match write!(stream, "{parsed_json_string}") {
Ok(_x) => (),
Err(e) => {
log::error!("{}\n{:?}", write_error, e);
return;
}
};
}
match write!(stream, "]") {
Ok(_x) => (),
Err(e) => {
log::error!("{}\n{:?}", write_error, e);
return;
}
};
match stream.flush() {
Ok(_x) => (),
Err(e) => {
log::error!("{}\n{:?}", write_error, e);
}
};
}
pub fn stringify_raw_vec(
serializable_vec: Vec<Box<impl TypedJsonSerializable + ?Sized>>,
) -> Vec<String> {
let mut results: Vec<String> = Vec::new();
if serializable_vec.is_empty() {
return results;
}
for raw_object in serializable_vec {
match raw_object.to_typed_json_string() {
Ok(s) => {
results.push(s.to_string());
}
Err(e) => {
log::error!("Failure to serialize parsed raw data\n{}", e);
}
}
}
results
}
pub fn raws_stringify_df_vec(
raws_stringable_vec: Vec<Box<impl RawsStyleSerializable + ?Sized>>,
) -> Vec<String> {
let mut results: Vec<String> = Vec::new();
if raws_stringable_vec.is_empty() {
return results;
}
for df_object in raws_stringable_vec {
results.push(df_object.to_raws_style());
}
results
}
pub fn path_from_game_directory<P: AsRef<Path>>(game_path: &P) -> Result<PathBuf, String> {
let game_path = Path::new(game_path.as_ref());
if !game_path.exists() {
return Err(String::from(
"Provided game path for parsing doesn't exist!",
));
}
if !game_path.is_dir() {
return Err(String::from("Game path needs to be a directory"));
}
if !game_path.join("gamelog.txt").exists() {
log::warn!("Unable to find gamelog.txt in game directory. Is it valid?");
}
Ok(game_path.to_path_buf())
}
pub fn write_raws_string_vec_to_file<P: AsRef<Path>>(
parsed_raws_string_vec: &Vec<String>,
out_filepath: &P,
) {
log::info!("Saving raws to to {:?}", out_filepath.as_ref().display());
let out_file = match File::create(out_filepath) {
Ok(f) => f,
Err(e) => {
log::error!(
"Unable to open {} for writing \n{:?}",
out_filepath.as_ref().display(),
e
);
return;
}
};
let mut stream = BufWriter::new(out_file);
let write_error = &format!(
"Unable to write to {}",
out_filepath.as_ref().to_string_lossy()
);
for parsed_json_string in parsed_raws_string_vec {
match writeln!(stream, "{parsed_json_string}") {
Ok(_x) => (),
Err(e) => {
log::error!("{}\n{:?}", write_error, e);
return;
}
};
}
match stream.flush() {
Ok(_x) => (),
Err(e) => {
log::error!("{}\n{:?}", write_error, e);
}
};
}