mod string;
use core::{
mem::replace,
ops::{Index, IndexMut},
};
use crate::{pointer::JsonPointer, value::misc::define_value};
pub use string::String;
define_value! {
name: Value,
string: String,
lifetime: '_,
}
impl Value {
#[inline]
pub fn take(&mut self) -> Value {
replace(self, Value::Null)
}
#[inline]
pub fn get<I: JsonPointer>(&self, idx: I) -> Option<&Value> {
match self {
Value::Array(v) => v.get(idx.as_index()?),
Value::Object(v) => v.get(idx.as_key()?),
_ => None,
}
}
#[inline]
pub fn get_mut<I: JsonPointer>(&mut self, idx: I) -> Option<&mut Value> {
match self {
Value::Array(v) => v.get_mut(idx.as_index()?),
Value::Object(v) => v.get_mut(idx.as_key()?),
_ => None,
}
}
#[inline]
pub fn as_null(&self) -> Option<()> {
match self {
Self::Null => Some(()),
_ => None,
}
}
#[inline]
pub fn as_bool(&self) -> Option<bool> {
match self {
Self::Boolean(v) => Some(*v),
_ => None,
}
}
#[inline]
pub fn as_array(&self) -> Option<&Array<Self>> {
match self {
Self::Array(v) => Some(v),
_ => None,
}
}
#[inline]
pub fn as_array_mut(&mut self) -> Option<&mut Array<Self>> {
match self {
Self::Array(v) => Some(v),
_ => None,
}
}
#[inline]
pub fn as_object(&self) -> Option<&Object<String, Value>> {
match self {
Self::Object(v) => Some(v),
_ => None,
}
}
#[inline]
pub fn as_object_mut(&mut self) -> Option<&mut Object<String, Value>> {
match self {
Self::Object(v) => Some(v),
_ => None,
}
}
#[inline]
pub fn as_number(&self) -> Option<Number> {
match *self {
Self::Number(v) => Some(v),
_ => None,
}
}
#[inline]
pub fn as_i64(&self) -> Option<i64> {
self.as_number()?.as_i64()
}
#[inline]
pub fn as_u64(&self) -> Option<u64> {
self.as_number()?.as_u64()
}
#[inline]
pub fn as_f64(&self) -> Option<f64> {
self.as_number()?.as_f64()
}
#[inline]
pub fn as_str(&self) -> Option<&str> {
match self {
Self::String(v) => Some(v),
_ => None,
}
}
#[inline]
pub fn is_str(&self) -> bool {
self.as_str().is_some()
}
#[inline]
pub fn is_null(&self) -> bool {
self.as_null().is_some()
}
#[inline]
pub fn is_bool(&self) -> bool {
self.as_bool().is_some()
}
#[inline]
pub fn is_array(&self) -> bool {
self.as_array().is_some()
}
#[inline]
pub fn is_object(&self) -> bool {
self.as_object().is_some()
}
#[inline]
pub fn is_number(&self) -> bool {
self.as_number().is_some()
}
#[inline]
pub fn is_i64(&self) -> bool {
self.as_i64().is_some()
}
#[inline]
pub fn is_u64(&self) -> bool {
self.as_u64().is_some()
}
#[inline]
pub fn is_f64(&self) -> bool {
self.as_f64().is_some()
}
pub fn pointer<P>(&self, p: P) -> Option<&Value>
where
P: IntoIterator,
P::Item: JsonPointer,
{
let mut tmp = self;
for pointer in p {
tmp = match tmp {
Value::Object(obj) => obj.get(pointer.as_key()?),
Value::Array(arr) => arr.get(pointer.as_index()?),
_ => None,
}?
}
Some(tmp)
}
pub fn pointer_mut<P>(&mut self, p: P) -> Option<&mut Value>
where
P: IntoIterator,
P::Item: JsonPointer,
{
let mut tmp = self;
for pointer in p {
tmp = match tmp {
Value::Object(obj) => obj.get_mut(pointer.as_key()?),
Value::Array(arr) => arr.get_mut(pointer.as_index()?),
_ => None,
}?
}
Some(tmp)
}
}
impl Index<usize> for Value {
type Output = Value;
#[inline]
fn index(&self, idx: usize) -> &Self::Output {
match self.as_array() {
Some(v) => match v.get(idx) {
Some(v) => v,
_ => panic!("given index does not exist in the array"),
},
_ => panic!("value is not an array"),
}
}
}
impl IndexMut<usize> for Value {
#[inline]
fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
match self.as_array_mut() {
Some(v) => match v.get_mut(idx) {
Some(v) => v,
_ => panic!("given index does not exist in the array"),
},
_ => panic!("value is not an array"),
}
}
}
impl Index<&str> for Value {
type Output = Value;
#[inline]
fn index(&self, key: &str) -> &Self::Output {
match self.as_object() {
Some(v) => match v.get(key) {
Some(v) => v,
_ => panic!("given key does not exist in the object"),
},
_ => panic!("value is not an object"),
}
}
}
impl IndexMut<&str> for Value {
#[inline]
fn index_mut(&mut self, key: &str) -> &mut Self::Output {
match self.as_object_mut() {
Some(v) => match v.get_mut(key) {
Some(v) => v,
_ => panic!("given key does not exist in the object"),
},
_ => panic!("value is not an object"),
}
}
}