Skip to main content

Crate speedyspec_php_serialize

Crate speedyspec_php_serialize 

Source
Expand description

§speedyspec-php-serialize

crates.io docs.rs MIT

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

  • serialize and unserialize: PHP’s format, byte for byte, over PhpValue and PhpKey.
  • maybe_serialize, maybe_unserialize and is_serialized: WordPress’s helpers.
  • to_hook_value and from_hook_value: conversions to the Hook API’s Value.

§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§

OpaquePhpValue
What a Value cannot represent (objects, cycles, keys that are not UTF-8), carried in Value::Opaque.
UnserializeError
PHP’s unserialize() notice: where reading stopped.

Enums§

ConvertError
A Value that 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, false when 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 Rc for every r: 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.