From 657440d01423b50153d07ebc288d9bd2fc982d52 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 21 Jun 2026 16:31:44 +0900 Subject: refactor(json): eliminate json_last_error in favor of Result json_encode/json_encode_ex now return anyhow::Result instead of Option, so callers no longer need json_last_error() to get the failure reason. Co-Authored-By: Claude Opus 4.8 (1M context) --- crates/shirabe-php-shim/src/json.rs | 21 +++++++-------------- crates/shirabe-php-shim/src/runtime.rs | 1 - 2 files changed, 7 insertions(+), 15 deletions(-) (limited to 'crates/shirabe-php-shim/src') diff --git a/crates/shirabe-php-shim/src/json.rs b/crates/shirabe-php-shim/src/json.rs index ed704f9..23c4c41 100644 --- a/crates/shirabe-php-shim/src/json.rs +++ b/crates/shirabe-php-shim/src/json.rs @@ -17,29 +17,22 @@ pub const JSON_PRETTY_PRINT: i64 = 128; pub const JSON_THROW_ON_ERROR: i64 = 4194304; pub const JSON_INVALID_UTF8_IGNORE: i64 = 1048576; -pub const JSON_ERROR_NONE: i64 = 0; -pub const JSON_ERROR_DEPTH: i64 = 1; -pub const JSON_ERROR_STATE_MISMATCH: i64 = 2; -pub const JSON_ERROR_CTRL_CHAR: i64 = 3; -pub const JSON_ERROR_UTF8: i64 = 5; - -pub fn json_last_error() -> i64 { - todo!() -} - -pub fn json_encode(value: &T) -> Option { +pub fn json_encode(value: &T) -> anyhow::Result { // PHP's json_encode() with no flags escapes slashes and non-ASCII characters. json_encode_ex(value, 0) } -pub fn json_encode_ex(value: &T, flags: i64) -> Option { +pub fn json_encode_ex( + value: &T, + flags: i64, +) -> anyhow::Result { // serde_json's compact output already matches PHP's `json_encode` with both // JSON_UNESCAPED_SLASHES and JSON_UNESCAPED_UNICODE set: forward slashes and non-ASCII // characters are emitted verbatim. The two flags below re-apply PHP's default escaping when // they are absent. // TODO(phase-c): other flags (e.g. JSON_PRETTY_PRINT, JSON_HEX_*, JSON_THROW_ON_ERROR) are not // handled yet; add them when a call site needs them. - let mut s = serde_json::to_string(value).ok()?; + let mut s = serde_json::to_string(value)?; if flags & JSON_UNESCAPED_SLASHES == 0 { s = s.replace('/', "\\/"); @@ -60,7 +53,7 @@ pub fn json_encode_ex(value: &T, flags: i64) -> Op s = out; } - Some(s) + Ok(s) } // PHP's two-argument `json_decode`: without JSON_THROW_ON_ERROR it never throws, diff --git a/crates/shirabe-php-shim/src/runtime.rs b/crates/shirabe-php-shim/src/runtime.rs index cc63af6..2f98891 100644 --- a/crates/shirabe-php-shim/src/runtime.rs +++ b/crates/shirabe-php-shim/src/runtime.rs @@ -61,7 +61,6 @@ pub fn defined(name: &str) -> bool { | "CURL_VERSION_LIBZ" | "CURL_VERSION_ZSTD" | "GLOB_BRACE" - | "JSON_ERROR_UTF8" | "OPENSSL_VERSION_TEXT" | "PHP_BINARY" | "SIGINT" -- cgit v1.3.1