Skip to content

Commit

Permalink
feat: luau add qsv_writejson helper
Browse files Browse the repository at this point in the history
  • Loading branch information
jqnatividad committed Dec 25, 2024
1 parent 54e41f0 commit 18cc63e
Showing 1 changed file with 46 additions and 23 deletions.
69 changes: 46 additions & 23 deletions src/cmd/luau.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1846,35 +1846,58 @@ fn setup_helpers(

let sanitized_filename = sanitize(filepath);

// // Convert Lua value to serde_json::Value using proper serialization
// let json_value = serde_json::to_string(&table_or_value).map_err(|e| {
// mlua::Error::RuntimeError(format!("Failed to convert Luau value to JSON: {e}"))
// })?;
// Convert Lua value to serde_json::Value using proper serialization
let json_value = if pretty.unwrap_or(false) {
match serde_json::to_string_pretty(&table_or_value) {
Ok(v) => v,
Err(e) => {
return helper_err!(
"qsv_writejson",
"Failed to convert Luau value to JSON: {e}"
);
},
}
} else {
match serde_json::to_string(&table_or_value) {
Ok(v) => v,
Err(e) => {
return helper_err!(
"qsv_writejson",
"Failed to convert Luau value to JSON: {e}"
);
},
}
};

// Create file
let file = std::fs::File::create(&sanitized_filename).map_err(|e| {
mlua::Error::RuntimeError(format!(
"Failed to create JSON file \"{}\": {e}",
sanitized_filename
))
})?;
let file = match std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(&sanitized_filename)
{
Ok(f) => f,
Err(e) => {
return helper_err!(
"qsv_writejson",
"Failed to create JSON file \"{sanitized_filename}\": {e}"
);
},
};
let mut file = std::io::BufWriter::with_capacity(DEFAULT_WTR_BUFFER_CAPACITY, file);

// Write JSON
if pretty.unwrap_or(false) {
serde_json::to_writer_pretty(file, &table_or_value).map_err(|e| {
mlua::Error::RuntimeError(format!("Failed to write pretty JSON to file: {e}"))
})?;
} else {
serde_json::to_writer(file, &table_or_value).map_err(|e| {
mlua::Error::RuntimeError(format!("Failed to write JSON to file: {e}"))
})?;
if let Err(e) = file.write_all(json_value.as_bytes()) {
return helper_err!(
"qsv_writejson",
"Failed to write JSON to file \"{sanitized_filename}\": {e}"
);
}

// info!(
// "qsv_writejson() - saved {} bytes to file: {}",
// json_value.to_string().len(),
// sanitized_filename
// );
info!(
"qsv_writejson() - saved {} bytes to file: {sanitized_filename}",
json_value.to_string().len()
);

Ok(true)
},
Expand Down

0 comments on commit 18cc63e

Please sign in to comment.