use std::fmt;
#[derive(Clone, Debug, PartialEq)]
pub enum Value {
Undefined,
Null,
Bool(bool),
Number(f64),
String(String),
Array(Vec<Value>),
Object(Object),
}
impl Value {
pub fn as_str(&self) -> Option<&str> {
match self {
Value::String(s) => Some(s),
_ => None,
}
}
pub fn as_f64(&self) -> Option<f64> {
match self {
Value::Number(n) => Some(*n),
_ => None,
}
}
pub fn as_bool(&self) -> Option<bool> {
match self {
Value::Bool(b) => Some(*b),
_ => None,
}
}
pub fn as_array(&self) -> Option<&Vec<Value>> {
match self {
Value::Array(a) => Some(a),
_ => None,
}
}
pub fn as_object(&self) -> Option<&Object> {
match self {
Value::Object(o) => Some(o),
_ => None,
}
}
pub fn get(&self, key: &str) -> Option<&Value> {
self.as_object().and_then(|o| o.get(key))
}
pub fn is_undefined(&self) -> bool {
matches!(self, Value::Undefined)
}
pub fn is_null(&self) -> bool {
matches!(self, Value::Null)
}
}
impl From<&str> for Value {
fn from(s: &str) -> Self {
Value::String(s.to_owned())
}
}
impl From<String> for Value {
fn from(s: String) -> Self {
Value::String(s)
}
}
impl From<f64> for Value {
fn from(n: f64) -> Self {
Value::Number(n)
}
}
impl From<bool> for Value {
fn from(b: bool) -> Self {
Value::Bool(b)
}
}
impl From<Object> for Value {
fn from(o: Object) -> Self {
Value::Object(o)
}
}
impl From<Vec<Value>> for Value {
fn from(a: Vec<Value>) -> Self {
Value::Array(a)
}
}
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match super::stringify(self) {
Some(text) => f.write_str(&text),
None => f.write_str("undefined"),
}
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Object {
entries: Vec<(String, Value)>,
}
impl Object {
pub fn new() -> Self {
Self::default()
}
pub fn len(&self) -> usize {
self.entries.len()
}
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
pub fn get(&self, key: &str) -> Option<&Value> {
self.entries.iter().find(|(k, _)| k == key).map(|(_, v)| v)
}
pub fn get_mut(&mut self, key: &str) -> Option<&mut Value> {
self.entries
.iter_mut()
.find(|(k, _)| k == key)
.map(|(_, v)| v)
}
pub fn contains_key(&self, key: &str) -> bool {
self.get(key).is_some()
}
pub fn insert(&mut self, key: impl Into<String>, value: Value) {
let key = key.into();
if let Some(existing) = self.get_mut(&key) {
*existing = value;
return;
}
let at = match array_index(&key) {
Some(index) => self
.entries
.iter()
.position(|(k, _)| array_index(k).is_none_or(|i| i > index))
.unwrap_or(self.entries.len()),
None => self.entries.len(),
};
self.entries.insert(at, (key, value));
}
pub fn remove(&mut self, key: &str) -> Option<Value> {
let at = self.entries.iter().position(|(k, _)| k == key)?;
Some(self.entries.remove(at).1)
}
pub fn iter(&self) -> impl Iterator<Item = (&str, &Value)> {
self.entries.iter().map(|(k, v)| (k.as_str(), v))
}
pub fn keys(&self) -> impl Iterator<Item = &str> {
self.entries.iter().map(|(k, _)| k.as_str())
}
}
impl<K: Into<String>> FromIterator<(K, Value)> for Object {
fn from_iter<I: IntoIterator<Item = (K, Value)>>(iter: I) -> Self {
let mut object = Object::new();
for (k, v) in iter {
object.insert(k, v);
}
object
}
}
pub(crate) fn array_index(key: &str) -> Option<u32> {
if key.is_empty() || (key.len() > 1 && key.starts_with('0')) {
return None;
}
if !key.bytes().all(|b| b.is_ascii_digit()) {
return None;
}
key.parse::<u32>().ok().filter(|&i| i != u32::MAX)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn keys_follow_js_property_order() {
let mut o = Object::new();
for k in ["b", "2", "a", "1", "01", "4294967295"] {
o.insert(k, Value::Null);
}
o.insert("b", Value::Bool(true));
assert_eq!(
o.keys().collect::<Vec<_>>(),
["1", "2", "b", "a", "01", "4294967295"]
);
assert_eq!(o.get("b"), Some(&Value::Bool(true)));
}
}