use crate::arena::value::DataValue;
use datavalue::NumberValue;
static SINGLETON_NULL: DataValue<'static> = DataValue::Null;
static SINGLETON_TRUE: DataValue<'static> = DataValue::Bool(true);
static SINGLETON_FALSE: DataValue<'static> = DataValue::Bool(false);
static SINGLETON_EMPTY_STRING: DataValue<'static> = DataValue::String("");
static SINGLETON_EMPTY_ARRAY: DataValue<'static> = DataValue::Array(&[]);
static SINGLETON_EMPTY_OBJECT: DataValue<'static> = DataValue::Object(&[]);
#[cfg(feature = "ext-control")]
static SINGLETON_TYPE_NULL: DataValue<'static> = DataValue::String("null");
#[cfg(feature = "ext-control")]
static SINGLETON_TYPE_BOOL: DataValue<'static> = DataValue::String("boolean");
#[cfg(feature = "ext-control")]
static SINGLETON_TYPE_NUMBER: DataValue<'static> = DataValue::String("number");
#[cfg(feature = "ext-control")]
static SINGLETON_TYPE_STRING: DataValue<'static> = DataValue::String("string");
#[cfg(feature = "ext-control")]
static SINGLETON_TYPE_ARRAY: DataValue<'static> = DataValue::String("array");
#[cfg(feature = "ext-control")]
static SINGLETON_TYPE_OBJECT: DataValue<'static> = DataValue::String("object");
#[cfg(all(feature = "ext-control", feature = "datetime"))]
static SINGLETON_TYPE_DATETIME: DataValue<'static> = DataValue::String("datetime");
#[cfg(all(feature = "ext-control", feature = "datetime"))]
static SINGLETON_TYPE_DURATION: DataValue<'static> = DataValue::String("duration");
#[inline]
pub(crate) fn singleton_null<'a>() -> &'a DataValue<'a> {
&SINGLETON_NULL
}
#[inline]
pub(crate) fn singleton_true<'a>() -> &'a DataValue<'a> {
&SINGLETON_TRUE
}
#[inline]
pub(crate) fn singleton_false<'a>() -> &'a DataValue<'a> {
&SINGLETON_FALSE
}
#[inline]
pub(crate) fn singleton_bool<'a>(b: bool) -> &'a DataValue<'a> {
if b { &SINGLETON_TRUE } else { &SINGLETON_FALSE }
}
#[inline]
pub(crate) fn singleton_empty_string<'a>() -> &'a DataValue<'a> {
&SINGLETON_EMPTY_STRING
}
#[inline]
pub(crate) fn singleton_empty_array<'a>() -> &'a DataValue<'a> {
&SINGLETON_EMPTY_ARRAY
}
#[inline]
pub(crate) fn singleton_empty_object<'a>() -> &'a DataValue<'a> {
&SINGLETON_EMPTY_OBJECT
}
const SMALL_INT_MAX: i64 = 32;
static SINGLETON_SMALL_INTS: [DataValue<'static>; (SMALL_INT_MAX + 1) as usize] = {
let mut arr = [DataValue::Number(NumberValue::Integer(0)); (SMALL_INT_MAX + 1) as usize];
let mut i: usize = 0;
while i < arr.len() {
arr[i] = DataValue::Number(NumberValue::Integer(i as i64));
i += 1;
}
arr
};
#[inline]
pub(crate) fn singleton_small_int<'a>(i: i64) -> Option<&'a DataValue<'a>> {
if (0..=SMALL_INT_MAX).contains(&i) {
Some(&SINGLETON_SMALL_INTS[i as usize])
} else {
None
}
}
#[cfg(feature = "ext-control")]
#[inline]
pub(crate) fn singleton_type_name<'a>(name: &'static str) -> &'a DataValue<'a> {
match name {
"null" => &SINGLETON_TYPE_NULL,
"boolean" => &SINGLETON_TYPE_BOOL,
"number" => &SINGLETON_TYPE_NUMBER,
"string" => &SINGLETON_TYPE_STRING,
"array" => &SINGLETON_TYPE_ARRAY,
"object" => &SINGLETON_TYPE_OBJECT,
#[cfg(feature = "datetime")]
"datetime" => &SINGLETON_TYPE_DATETIME,
#[cfg(feature = "datetime")]
"duration" => &SINGLETON_TYPE_DURATION,
_ => &SINGLETON_NULL,
}
}