pub enum Value {
Null,
Bool(bool),
Int(i64),
Float(f64),
String(String),
Bytes(Vec<u8>),
List(Vec<Value>),
Map(Vec<(Key, Value)>),
Object {
class: String,
properties: Vec<(Key, Value)>,
},
}Expand description
A PHP value, as produced by unserialize() or accepted by serialize().
PHP arrays are ordered maps with int or string keys. An array whose keys are exactly
0..n in order decodes to Value::List; any other array decodes to Value::Map,
which keeps PHP’s key order.
Variants§
Null
PHP null.
Bool(bool)
PHP bool.
Int(i64)
PHP int (64-bit).
Float(f64)
PHP float.
String(String)
PHP string holding valid UTF-8.
Bytes(Vec<u8>)
PHP string that is not valid UTF-8. Decoding yields this variant only for such
strings; both string variants serialize to a PHP string.
List(Vec<Value>)
PHP array with the keys 0..n.
Map(Vec<(Key, Value)>)
PHP array with any other keys, in PHP order.
Object
A PHP object. Decode-only: the class is never instantiated or looked up, so
decrypting untrusted payloads cannot trigger unserialize gadget chains, and the
value cannot be serialized again (Error::Serialize). Property names keep PHP’s
visibility mangling ("\0*\0name" for protected, "\0Class\0name" for private).
Implementations§
Source§impl Value
impl Value
Sourcepub fn list<I, V>(items: I) -> Self
pub fn list<I, V>(items: I) -> Self
Builds a Value::List from an iterator.
Examples found in repository?
30fn cases() -> Vec<(&'static str, Value)> {
31 vec![
32 ("null", Value::Null),
33 ("true", true.into()),
34 ("false", false.into()),
35 ("int_zero", 0.into()),
36 ("int_negative", (-42).into()),
37 ("int_max", i64::MAX.into()),
38 ("int_min", i64::MIN.into()),
39 ("float", 2.5.into()),
40 ("float_whole", 2.0.into()),
41 ("float_tenth", 0.1.into()),
42 ("float_large", 1.0e25.into()),
43 ("float_small", 1.5e-7.into()),
44 ("float_negative_zero", (-0.0).into()),
45 ("float_pi", std::f64::consts::PI.into()),
46 ("float_max", f64::MAX.into()),
47 ("float_min_subnormal", 4.9e-324.into()),
48 ("float_inf", f64::INFINITY.into()),
49 ("float_negative_inf", f64::NEG_INFINITY.into()),
50 ("float_nan", f64::NAN.into()),
51 ("string_empty", "".into()),
52 ("string_unicode", "Grüße 世界 🚀 café".into()),
53 ("string_quotes", r#"say "hi"; s:3:"x";"#.into()),
54 ("string_binary", Value::Bytes(vec![0x00, 0xff, 0xfe, 0x80])),
55 (
56 "list",
57 Value::list([
58 Value::Int(1),
59 "two".into(),
60 true.into(),
61 Value::Null,
62 Value::list([3, 4]),
63 ]),
64 ),
65 ("empty_array", Value::List(vec![])),
66 (
67 "assoc",
68 Value::map([
69 ("user", Value::from("alice")),
70 ("id", 7.into()),
71 ("active", true.into()),
72 ("score", 2.5.into()),
73 ("tags", Value::list(["x", "y"])),
74 ("meta", Value::Null),
75 ]),
76 ),
77 (
78 "mixed_keys",
79 Value::Map(vec![
80 (5.into(), "a".into()),
81 ("x".into(), "b".into()),
82 (7.into(), Value::map([("nested", Value::list([1, 2]))])),
83 ]),
84 ),
85 (
86 "non_sequential_list",
87 Value::Map(vec![(1.into(), "a".into()), (0.into(), "b".into())]),
88 ),
89 (
90 "nested",
91 Value::map([(
92 "l1",
93 Value::map([(
94 "l2",
95 Value::map([(
96 "l3",
97 Value::List(vec!["deep".into(), 1.5.into(), false.into()]),
98 )]),
99 )]),
100 )]),
101 ),
102 ]
103}Sourcepub fn map<I, K, V>(entries: I) -> Self
pub fn map<I, K, V>(entries: I) -> Self
Builds a Value::Map from key/value pairs, keeping their order.
Examples found in repository?
30fn cases() -> Vec<(&'static str, Value)> {
31 vec![
32 ("null", Value::Null),
33 ("true", true.into()),
34 ("false", false.into()),
35 ("int_zero", 0.into()),
36 ("int_negative", (-42).into()),
37 ("int_max", i64::MAX.into()),
38 ("int_min", i64::MIN.into()),
39 ("float", 2.5.into()),
40 ("float_whole", 2.0.into()),
41 ("float_tenth", 0.1.into()),
42 ("float_large", 1.0e25.into()),
43 ("float_small", 1.5e-7.into()),
44 ("float_negative_zero", (-0.0).into()),
45 ("float_pi", std::f64::consts::PI.into()),
46 ("float_max", f64::MAX.into()),
47 ("float_min_subnormal", 4.9e-324.into()),
48 ("float_inf", f64::INFINITY.into()),
49 ("float_negative_inf", f64::NEG_INFINITY.into()),
50 ("float_nan", f64::NAN.into()),
51 ("string_empty", "".into()),
52 ("string_unicode", "Grüße 世界 🚀 café".into()),
53 ("string_quotes", r#"say "hi"; s:3:"x";"#.into()),
54 ("string_binary", Value::Bytes(vec![0x00, 0xff, 0xfe, 0x80])),
55 (
56 "list",
57 Value::list([
58 Value::Int(1),
59 "two".into(),
60 true.into(),
61 Value::Null,
62 Value::list([3, 4]),
63 ]),
64 ),
65 ("empty_array", Value::List(vec![])),
66 (
67 "assoc",
68 Value::map([
69 ("user", Value::from("alice")),
70 ("id", 7.into()),
71 ("active", true.into()),
72 ("score", 2.5.into()),
73 ("tags", Value::list(["x", "y"])),
74 ("meta", Value::Null),
75 ]),
76 ),
77 (
78 "mixed_keys",
79 Value::Map(vec![
80 (5.into(), "a".into()),
81 ("x".into(), "b".into()),
82 (7.into(), Value::map([("nested", Value::list([1, 2]))])),
83 ]),
84 ),
85 (
86 "non_sequential_list",
87 Value::Map(vec![(1.into(), "a".into()), (0.into(), "b".into())]),
88 ),
89 (
90 "nested",
91 Value::map([(
92 "l1",
93 Value::map([(
94 "l2",
95 Value::map([(
96 "l3",
97 Value::List(vec!["deep".into(), 1.5.into(), false.into()]),
98 )]),
99 )]),
100 )]),
101 ),
102 ]
103}Sourcepub fn is_null(&self) -> bool
pub fn is_null(&self) -> bool
Returns true for Value::Null.
Sourcepub fn as_bool(&self) -> Option<bool>
pub fn as_bool(&self) -> Option<bool>
Returns the value of a Value::Bool.
Sourcepub fn as_i64(&self) -> Option<i64>
pub fn as_i64(&self) -> Option<i64>
Returns the value of a Value::Int.
Sourcepub fn as_f64(&self) -> Option<f64>
pub fn as_f64(&self) -> Option<f64>
Returns the value of a Value::Float.
Sourcepub fn as_str(&self) -> Option<&str>
pub fn as_str(&self) -> Option<&str>
Returns the text of a Value::String.
Sourcepub fn as_list(&self) -> Option<&[Value]>
pub fn as_list(&self) -> Option<&[Value]>
Returns the items of a Value::List.
Sourcepub fn get(&self, key: &str) -> Option<&Value>
pub fn get(&self, key: &str) -> Option<&Value>
Looks up a string key in a Value::Map or the properties of a Value::Object.