aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/json/json_file.rs
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-08-02 17:32:43 +0900
committernsfisis <nsfisis@gmail.com>2026-08-02 17:32:43 +0900
commit7e31bab9ff7209c9b6d7928a581d7a449846f42e (patch)
treeb121966a169ee74afe04d066576e862a6702bbf6 /crates/shirabe/src/json/json_file.rs
parentebcb4a7f013c0511dd6617686395cef17822d1e2 (diff)
downloadphp-shirabe-7e31bab9ff7209c9b6d7928a581d7a449846f42e.tar.gz
php-shirabe-7e31bab9ff7209c9b6d7928a581d7a449846f42e.tar.zst
php-shirabe-7e31bab9ff7209c9b6d7928a581d7a449846f42e.zip
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 <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/src/json/json_file.rs')
-rw-r--r--crates/shirabe/src/json/json_file.rs24
1 files changed, 11 insertions, 13 deletions
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<T: serde::Serialize + ?Sized>(data: &T) -> String {
+ pub fn encode<T: serde::Serialize + ?Sized>(data: &T) -> anyhow::Result<String> {
Self::encode_with_options(data, JsonEncodeOptions::default())
}
pub fn encode_with_options<T: serde::Serialize + ?Sized>(
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<String> {
+ 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.