Expand description
§speedyspec-php-serialize
PHP’s serialize format in Rust: serialize, unserialize, maybe_serialize, and conversions to hook values.
Part of the SpeedySpec platform.
Role: platform. Seam: container.
§What it gives you
serializeandunserialize: PHP’s format, byte for byte, overPhpValueandPhpKey.maybe_serialize,maybe_unserializeandis_serialized: WordPress’s helpers.to_hook_valueandfrom_hook_value: conversions to the Hook API’sValue.
§Install
cargo add speedyspec-php-serialize§Requirements
Rust 1.99.
§Example
Serialize an array, check it, and read it back.
use speedyspec_php_serialize::{PhpKey, PhpValue, is_serialized, serialize, unserialize};
let value = PhpValue::Array(vec![(PhpKey::Str(b"a".to_vec()), PhpValue::Int(1))]);
let bytes = serialize(&value);
assert_eq!(bytes, b"a:1:{s:1:\"a\";i:1;}".to_vec());
assert!(is_serialized(&bytes, true));
assert_eq!(unserialize(&bytes).unwrap(), value);§Where it fits
Options, metadata and theme.json values are stored serialized, so the contracts for those seams
(speedyspec-wp-meta-contracts, speedyspec-wp-posts-contracts, speedyspec-wp-theme-json-contracts) use these
types. No PHP runtime is involved.
§Versioning
SemVer, 0.x: a minor version may break the API. MSRV is Rust 1.99. SpeedySpec’s own version (ADR-028) is the PHP runtime’s major, not this crate’s.
§License
MIT
§In detail
PHP’s serialize format in Rust (a domain service): serialize, unserialize, WordPress’s maybe_serialize
family, and conversions to the Hook API’s values. No PHP runtime is involved.
Modules§
- semantics
- PHP value semantics the WordPress ports lean on:
empty(), array reads and writes,array_merge(), integer keys, the(array)and(string)casts and PHP 8’s loose==.
Structs§
- Opaque
PhpValue - What a
Valuecannot represent (objects, cycles, keys that are not UTF-8), carried inValue::Opaque. - Unserialize
Error - PHP’s
unserialize()notice: where reading stopped.
Enums§
- Convert
Error - A
Valuethat cannot become PHP’s serialized form. - PhpKey
- An array key or property name.
- PhpValue
- A value in PHP’s serialize format.
Functions§
- from_
hook_ value - A hook value in serialized form; an opaque value from elsewhere is
Foreign. - is_
serialized is_serialized().- maybe_
serialize maybe_serialize(): arrays and objects serialized, and strings that look serialized serialized again.- maybe_
unserialize maybe_unserialize(): a serialized string’s value,falsewhen it does not unserialize (@unserialize).- php_
float 1.0E+25,0.1,100000000000000,-0,INF,-INF,NAN.- serialize
- PHP’s
serialize(). - to_
hook_ value - A serialized value as the Hook API carries it. Objects become opaque (the same
Rcfor everyr:to one); a value with a cycle or a key that is not UTF-8 becomes one opaque value whole. - unserialize
- PHP’s
unserialize(): bytes after the first complete value are ignored.