use std::{
borrow::{Borrow, Cow},
hash::Hash,
};
use super::Value;
use crate::{borrowed, tape};
pub enum Object<'borrow, 'tape, 'input> {
Tape(tape::Object<'tape, 'input>),
Value(&'borrow borrowed::Object<'input>),
}
pub enum Iter<'borrow, 'tape, 'input> {
Tape(tape::object::Iter<'tape, 'input>),
Value(halfbrown::Iter<'borrow, crate::cow::Cow<'input, str>, borrowed::Value<'input>>),
}
pub enum Keys<'borrow, 'tape, 'input> {
Tape(tape::object::Keys<'tape, 'input>),
Value(halfbrown::Keys<'borrow, crate::cow::Cow<'input, str>, borrowed::Value<'input>>),
}
pub enum Values<'borrow, 'tape, 'input> {
Tape(tape::object::Values<'tape, 'input>),
Value(halfbrown::Values<'borrow, crate::cow::Cow<'input, str>, borrowed::Value<'input>>),
}
impl<'borrow, 'tape, 'input> Object<'borrow, 'tape, 'input> {
#[must_use]
pub fn get<'a, Q>(&'a self, k: &Q) -> Option<Value<'a, 'tape, 'input>>
where
str: Borrow<Q>,
for<'b> crate::cow::Cow<'b, str>: Borrow<Q>,
Q: ?Sized + Hash + Eq + Ord,
{
match self {
Object::Tape(t) => t.get(k).map(Value::Tape),
Object::Value(v) => v.get(k).map(Cow::Borrowed).map(Value::Value),
}
}
#[allow(clippy::pedantic)] #[must_use]
pub fn iter<'i>(&'i self) -> Iter<'i, 'tape, 'input> {
match self {
Object::Tape(t) => Iter::Tape(t.iter()),
Object::Value(v) => Iter::Value(v.iter()),
}
}
#[must_use]
pub fn keys<'i>(&'i self) -> Keys<'i, 'tape, 'input> {
match self {
Object::Tape(t) => Keys::Tape(t.keys()),
Object::Value(v) => Keys::Value(v.keys()),
}
}
#[must_use]
pub fn values<'i>(&'i self) -> Values<'i, 'tape, 'input> {
match self {
Object::Tape(t) => Values::Tape(t.values()),
Object::Value(v) => Values::Value(v.values()),
}
}
#[must_use]
pub fn len(&self) -> usize {
match self {
Object::Tape(t) => t.len(),
Object::Value(v) => v.len(),
}
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl<'borrow, 'tape, 'input> Iterator for Iter<'borrow, 'tape, 'input> {
type Item = (&'borrow str, Value<'borrow, 'tape, 'input>);
fn next(&mut self) -> Option<Self::Item> {
match self {
Iter::Tape(t) => t.next().map(|(k, v)| (k, Value::Tape(v))),
Iter::Value(v) => v
.next()
.map(|(k, v)| (k.as_ref(), Value::Value(Cow::Borrowed(v)))),
}
}
}
impl<'borrow, 'tape, 'input> Iterator for Keys<'borrow, 'tape, 'input> {
type Item = &'borrow str;
fn next(&mut self) -> Option<Self::Item> {
match self {
Keys::Tape(t) => t.next(),
Keys::Value(v) => v.next().map(std::convert::AsRef::as_ref),
}
}
}
impl<'borrow, 'tape, 'input> Iterator for Values<'borrow, 'tape, 'input> {
type Item = Value<'borrow, 'tape, 'input>;
fn next(&mut self) -> Option<Self::Item> {
match self {
Values::Tape(t) => t.next().map(Value::Tape),
Values::Value(v) => v.next().map(|v| Value::Value(Cow::Borrowed(v))),
}
}
}
#[cfg(test)]
mod test {
use value_trait::base::ValueAsScalar;
use crate::to_tape;
#[test]
fn get_ints() -> crate::Result<()> {
let mut input = br#"{"snot": 1, "badger":2, "cake":3, "cookie":4}"#.to_vec();
let t = to_tape(input.as_mut_slice())?;
let v = t.as_value();
let a = v.as_object().expect("is an object");
assert_eq!(a.get("snot").and_then(|v| v.as_u64()), Some(1));
assert_eq!(a.get("badger").and_then(|v| v.as_u64()), Some(2));
assert_eq!(a.get("cake").and_then(|v| v.as_u64()), Some(3));
assert_eq!(a.get("cookie").and_then(|v| v.as_u64()), Some(4));
assert_eq!(a.get("monster"), None);
Ok(())
}
#[test]
fn get_container() -> crate::Result<()> {
let mut input =
br#"{"snot": 1, "badger":[2, 2.5], "cake":{"frosting": 3}, "cookie":4}"#.to_vec();
let t = to_tape(input.as_mut_slice())?;
let v = t.as_value();
let a = v.as_object().expect("is an object");
assert_eq!(a.get("snot").and_then(|v| v.as_u64()), Some(1));
let badger = a.get("badger").expect("is an array");
let badger = badger.as_array().expect("is an array");
assert_eq!(badger.get(0).and_then(|v| v.as_u64()), Some(2));
assert_eq!(badger.get(1).and_then(|v| v.as_f64()), Some(2.5));
let cake = a.get("cake").expect("is an object");
let cake = cake.as_object().expect("is an object");
assert_eq!(cake.get("frosting").and_then(|v| v.as_u64()), Some(3));
assert_eq!(a.get("cookie").and_then(|v| v.as_u64()), Some(4));
assert_eq!(a.get("monster"), None);
Ok(())
}
#[test]
fn iter_ints() -> crate::Result<()> {
let mut input = br#"{"snot": 1, "badger":2, "cake":3, "cookie":4}"#.to_vec();
let t = to_tape(input.as_mut_slice())?;
let v = t.as_value();
let v = v
.as_object()
.expect("is an object")
.iter()
.map(|(k, v)| (k, v.as_u64().expect("integer")))
.collect::<Vec<_>>();
assert_eq!(
v,
vec![("snot", 1), ("badger", 2), ("cake", 3), ("cookie", 4)]
);
Ok(())
}
#[test]
fn keys_ints() -> crate::Result<()> {
let mut input = br#"{"snot": 1, "badger":2, "cake":3, "cookie":4}"#.to_vec();
let t = to_tape(input.as_mut_slice())?;
let v = t.as_value();
let v = v
.as_object()
.expect("is an object")
.keys()
.collect::<Vec<_>>();
assert_eq!(v, vec!["snot", "badger", "cake", "cookie"]);
Ok(())
}
#[test]
fn values_ints() -> crate::Result<()> {
let mut input = br#"{"snot": 1, "badger":2, "cake":3, "cookie":4}"#.to_vec();
let t = to_tape(input.as_mut_slice())?;
let v = t.as_value();
let v = v
.as_object()
.expect("is an object")
.values()
.map(|v| v.as_u64().expect("integer"))
.collect::<Vec<_>>();
assert_eq!(v, vec![1, 2, 3, 4]);
Ok(())
}
#[test]
fn iter_container() -> crate::Result<()> {
let mut input =
br#"{"snot": 1, "badger":[2, 2.5], "cake":{"frosting": 3}, "cookie":4}"#.to_vec();
let t = to_tape(input.as_mut_slice())?;
let v = t.as_value();
let v = v
.as_object()
.expect("is an object")
.iter()
.map(|(k, v)| (k, v.as_u64()))
.collect::<Vec<_>>();
assert_eq!(
v,
vec![
("snot", Some(1)),
("badger", None),
("cake", None),
("cookie", Some(4))
]
);
Ok(())
}
#[test]
fn keys_container() -> crate::Result<()> {
let mut input =
br#"{"snot": 1, "badger":[2, 2.5], "cake":{"frosting": 3}, "cookie":4}"#.to_vec();
let t = to_tape(input.as_mut_slice())?;
let v = t.as_value();
let v = v
.as_object()
.expect("is an object")
.keys()
.collect::<Vec<_>>();
assert_eq!(v, vec!["snot", "badger", "cake", "cookie"]);
Ok(())
}
#[test]
fn values_container() -> crate::Result<()> {
let mut input =
br#"{"snot": 1, "badger":[2, 2.5], "cake":{"frosting": 3}, "cookie":4}"#.to_vec();
let t = to_tape(input.as_mut_slice())?;
let v = t.as_value();
let v = v
.as_object()
.expect("is an object")
.values()
.map(|v| v.as_u64())
.collect::<Vec<_>>();
assert_eq!(v, vec![Some(1), None, None, Some(4)]);
Ok(())
}
}