Skip to main content

php_std/types/
mod.rs

1//! The PHP value model: [`Value`], [`Str`], [`Array`], [`ArrayKey`].
2//!
3//! One model for every crate that carries PHP values (request payloads,
4//! documents, cache entries, queue messages), with the engine's semantics:
5//! byte strings, ordered arrays with integer and string keys, `stdClass`,
6//! integers distinct from floats. The codecs ([`crate::json`],
7//! [`crate::serialize`], [`crate::igbinary`]), type juggling and comparison
8//! ([`Value::loose_eq`], [`Value::cmp_php`]) and sorting ([`crate::sort`])
9//! work on it.
10//!
11//! | PHP | Rust |
12//! |---|---|
13//! | `null`, `bool`, `int`, `float` | [`Value::Null`], [`Value::Bool`], [`Value::Int`], [`Value::Float`] |
14//! | `string` (bytes) | [`Value::Str`] / [`Str`] |
15//! | `array` | [`Value::Array`] / [`Array`], keys [`ArrayKey`] |
16//! | `stdClass` | [`Value::Object`] (its property table, string keys as given) |
17//! | other objects (`Utopia\Database\Document`, ...) | [`Value::Ext`], an [`Extension`] |
18//! | `ZEND_HANDLE_NUMERIC_STR` | [`ArrayKey::normalize`], [`numeric_key`] |
19//! | `gettype`, `(bool)`, `(string)`, `(float)` | [`Value::type_name`], [`Value::truthy`], [`Value::to_php_string`], [`Value::to_double`] |
20//! | `==`, `===`, `<=>` (`zend_compare`) | [`Value::loose_eq`], [`Value::strict_eq`], [`Value::cmp_php`], [`Value::compare`] |
21//! | `Utopia\Http\Request` body decoding | [`Value::from_json`] |
22//!
23//! Values have no identity: PHP references and shared instances are copies
24//! here. Two object values are never `===` (separately created PHP objects
25//! are distinct instances), which is the one place where identity shows.
26//!
27//! Against the skeleton in rfc/rust-database.md: [`Array::iter`] yields
28//! borrowed [`KeyRef`]s (a packed array stores no keys), and the engine's
29//! packed-array bookkeeping is part of [`Array`] because it decides the next
30//! free index (see its docs).
31
32mod array;
33mod str;
34pub(crate) mod value;
35
36use std::fmt;
37
38pub(crate) use array::Builder;
39pub use array::{Array, ArrayFull, ArrayKey, Iter, KeyRef, numeric_key};
40pub use str::Str;
41pub use value::Value;
42
43/// Objects of classes other than `stdClass` that a crate carries in
44/// [`Value::Ext`] (for the database: `Document` and `Operator`).
45pub trait Extension: Clone + PartialEq + fmt::Debug + Send + Sync + 'static {
46    /// `get_class()`.
47    fn class(&self) -> &str;
48    /// `(array)` / `getArrayCopy()`: what the codecs write for the object.
49    fn to_array(&self) -> Array<Self>;
50}
51
52/// No extension: plain PHP values.
53#[derive(Clone, Copy, PartialEq, Eq, Debug)]
54pub enum Never {}
55
56impl Extension for Never {
57    /// Unreachable: [`Never`] has no values.
58    fn class(&self) -> &str {
59        match *self {}
60    }
61
62    /// Unreachable: [`Never`] has no values.
63    fn to_array(&self) -> Array<Self> {
64        match *self {}
65    }
66}
67
68/// An engine error thrown while juggling a value: `TypeError`, `ValueError`,
69/// `JsonException`, or `Error` (an object that cannot become a string).
70#[derive(Debug, Clone, PartialEq, Eq)]
71pub struct EngineError {
72    /// The PHP exception class.
73    pub class: &'static str,
74    /// The exception message.
75    pub message: String,
76}
77
78impl EngineError {
79    /// An error of PHP class `class` with `message`.
80    pub fn new(class: &'static str, message: impl Into<String>) -> Self {
81        EngineError { class, message: message.into() }
82    }
83}
84
85impl fmt::Display for EngineError {
86    /// Writes the message.
87    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88        f.write_str(&self.message)
89    }
90}
91
92impl std::error::Error for EngineError {}