diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-06-21 16:31:44 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-06-21 16:32:52 +0900 |
| commit | 657440d01423b50153d07ebc288d9bd2fc982d52 (patch) | |
| tree | 1a4b936f8eb0d2df660a390dc9aed0c076ebaa37 /crates/shirabe-php-shim/src | |
| parent | fcef25f6ef36287a4984ffdaab39df84f5aceeba (diff) | |
| download | php-shirabe-657440d01423b50153d07ebc288d9bd2fc982d52.tar.gz php-shirabe-657440d01423b50153d07ebc288d9bd2fc982d52.tar.zst php-shirabe-657440d01423b50153d07ebc288d9bd2fc982d52.zip | |
refactor(json): eliminate json_last_error in favor of Result
json_encode/json_encode_ex now return anyhow::Result<String> 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) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe-php-shim/src')
| -rw-r--r-- | crates/shirabe-php-shim/src/json.rs | 21 | ||||
| -rw-r--r-- | crates/shirabe-php-shim/src/runtime.rs | 1 |
2 files changed, 7 insertions, 15 deletions
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<T: serde::Serialize + ?Sized>(value: &T) -> Option<String> { +pub fn json_encode<T: serde::Serialize + ?Sized>(value: &T) -> anyhow::Result<String> { // PHP's json_encode() with no flags escapes slashes and non-ASCII characters. json_encode_ex(value, 0) } -pub fn json_encode_ex<T: serde::Serialize + ?Sized>(value: &T, flags: i64) -> Option<String> { +pub fn json_encode_ex<T: serde::Serialize + ?Sized>( + value: &T, + flags: i64, +) -> anyhow::Result<String> { // 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<T: serde::Serialize + ?Sized>(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" |
