diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-06-05 05:10:50 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-06-05 05:10:50 +0900 |
| commit | 0bd0c361297903888c1f53af67d547ef2858882f (patch) | |
| tree | d8dcd1e116569583252c530cf83b8ac41b1f1e86 /crates/shirabe-php-shim | |
| parent | d0edb5b7ac3456f99c2f2576e8992cc12d60574a (diff) | |
| download | php-shirabe-0bd0c361297903888c1f53af67d547ef2858882f.tar.gz php-shirabe-0bd0c361297903888c1f53af67d547ef2858882f.tar.zst php-shirabe-0bd0c361297903888c1f53af67d547ef2858882f.zip | |
refactor(json): make json encode helpers accept serde::Serialize
Change json_encode/json_encode_ex and JsonFile::encode/encode_with_options
to take a generic serde::Serialize value instead of &PhpMixed, implement
Serialize for PhpMixed/ArrayObject, and drop the PhpMixed::String wrapping
at JsonManipulator call sites in favor of passing raw values.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe-php-shim')
| -rw-r--r-- | crates/shirabe-php-shim/Cargo.toml | 1 | ||||
| -rw-r--r-- | crates/shirabe-php-shim/src/lib.rs | 49 |
2 files changed, 48 insertions, 2 deletions
diff --git a/crates/shirabe-php-shim/Cargo.toml b/crates/shirabe-php-shim/Cargo.toml index 881a979..30e132f 100644 --- a/crates/shirabe-php-shim/Cargo.toml +++ b/crates/shirabe-php-shim/Cargo.toml @@ -6,6 +6,7 @@ edition.workspace = true [dependencies] anyhow.workspace = true indexmap.workspace = true +serde.workspace = true zip.workspace = true [lints] diff --git a/crates/shirabe-php-shim/src/lib.rs b/crates/shirabe-php-shim/src/lib.rs index cb487a2..77e51ac 100644 --- a/crates/shirabe-php-shim/src/lib.rs +++ b/crates/shirabe-php-shim/src/lib.rs @@ -13,6 +13,51 @@ pub enum PhpMixed { Object(ArrayObject), } +impl serde::Serialize for PhpMixed { + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: serde::Serializer, + { + use serde::ser::{SerializeMap, SerializeSeq}; + match self { + PhpMixed::Null => serializer.serialize_none(), + PhpMixed::Bool(b) => serializer.serialize_bool(*b), + PhpMixed::Int(i) => serializer.serialize_i64(*i), + PhpMixed::Float(f) => serializer.serialize_f64(*f), + PhpMixed::String(s) => serializer.serialize_str(s), + PhpMixed::List(items) => { + let mut seq = serializer.serialize_seq(Some(items.len()))?; + for item in items { + seq.serialize_element(item)?; + } + seq.end() + } + PhpMixed::Array(entries) => { + let mut map = serializer.serialize_map(Some(entries.len()))?; + for (k, v) in entries { + map.serialize_entry(k, v)?; + } + map.end() + } + PhpMixed::Object(object) => object.serialize(serializer), + } + } +} + +impl serde::Serialize for ArrayObject { + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: serde::Serializer, + { + use serde::ser::SerializeMap; + let mut map = serializer.serialize_map(Some(self.data.len()))?; + for (k, v) in &self.data { + map.serialize_entry(k, v)?; + } + map.end() + } +} + impl PhpMixed { pub fn as_bool(&self) -> Option<bool> { match self { @@ -815,7 +860,7 @@ pub const JSON_UNESCAPED_SLASHES: i64 = 64; pub const JSON_PRETTY_PRINT: i64 = 128; pub const JSON_THROW_ON_ERROR: i64 = 4194304; -pub fn json_encode(_value: &PhpMixed) -> Option<String> { +pub fn json_encode<T: serde::Serialize + ?Sized>(_value: &T) -> Option<String> { todo!() } @@ -1078,7 +1123,7 @@ pub fn random_int(_min: i64, _max: i64) -> i64 { todo!() } -pub fn json_encode_ex(_value: &PhpMixed, _flags: i64) -> Option<String> { +pub fn json_encode_ex<T: serde::Serialize + ?Sized>(_value: &T, _flags: i64) -> Option<String> { todo!() } |
