From 7e31bab9ff7209c9b6d7928a581d7a449846f42e Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 2 Aug 2026 17:32:43 +0900 Subject: fix(json): propagate JsonFile::encode errors instead of unwrapping PHP's JsonFile::encode throws a RuntimeException when json_encode fails; the port swallowed that into an .unwrap() marked TODO(phase-c). Return anyhow::Result from encode/encode_with_options and propagate at every call site (print_table and list_repositories become Result-returning to carry it). Co-Authored-By: Claude Fable 5 --- crates/shirabe/src/json/json_file.rs | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'crates/shirabe/src/json/json_file.rs') diff --git a/crates/shirabe/src/json/json_file.rs b/crates/shirabe/src/json/json_file.rs index cba11f01..632519a7 100644 --- a/crates/shirabe/src/json/json_file.rs +++ b/crates/shirabe/src/json/json_file.rs @@ -220,7 +220,7 @@ impl JsonFile { if self.path == "php://memory" { file_put_contents( &self.path, - Self::encode_with_options(&hash, options).as_bytes(), + Self::encode_with_options(&hash, options)?.as_bytes(), ); return Ok(()); @@ -256,7 +256,7 @@ impl JsonFile { &self.path, &format!( "{}{}", - Self::encode_with_options(&hash, options.clone()), + Self::encode_with_options(&hash, options.clone())?, if options.pretty_print { "\n" } else { "" }, ), )?; @@ -443,25 +443,23 @@ impl JsonFile { Ok(true) } - pub fn encode(data: &T) -> String { + pub fn encode(data: &T) -> anyhow::Result { Self::encode_with_options(data, JsonEncodeOptions::default()) } pub fn encode_with_options( data: &T, options: JsonEncodeOptions, - ) -> String { - let json = json_encode_ex(data, options.to_flags()) - .map_err(|err| RuntimeException { - message: format!("JSON encoding failed: {}", err), - code: 0, - }) - .unwrap(); // TODO(phase-c): propagating an Err. + ) -> anyhow::Result { + let json = json_encode_ex(data, options.to_flags()).map_err(|err| RuntimeException { + message: format!("JSON encoding failed: {}", err), + code: 0, + })?; if options.pretty_print && options.indent != Self::INDENT_DEFAULT { // Pretty printing and not using default indentation let indent_owned = options.indent; - return Preg::replace_callback( + return Ok(Preg::replace_callback( php_regex!(r"#^ {4,}#m"), move |m: &indexmap::IndexMap< shirabe_external_packages::composer::pcre::CaptureKey, @@ -475,10 +473,10 @@ impl JsonFile { str_repeat(&indent_owned, (strlen(whole) / 4) as usize) }, &json, - ); + )); } - json + Ok(json) } /// Parses json string and returns hash. -- cgit v1.3.1