mod array;
mod bytes;
mod default_eq_ord_hash;
mod float;
mod index;
mod int;
mod map;
mod simple_value;
mod string;
use std::{
cmp,
collections::BTreeMap,
fmt,
hash::{Hash, Hasher},
time::{Duration, SystemTime},
};
use crate::{
Array, DataType, DateTime, EpochTime, Error, Float, IntegerBytes, Map, Result, SimpleValue,
codec::{Argument, Head, Major},
limits, tag,
util::u128_from_slice,
value_key::AsValueKey,
};
#[derive(Clone)]
pub enum Value {
SimpleValue(SimpleValue),
Unsigned(u64),
Negative(u64),
Float(Float),
ByteString(Vec<u8>),
TextString(String),
Array(Vec<Value>),
Map(BTreeMap<Value, Value>),
Tag(u64, Box<Value>),
}
impl fmt::Debug for Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::SimpleValue(sv) => match *sv {
SimpleValue::FALSE => f.write_str("false"),
SimpleValue::TRUE => f.write_str("true"),
SimpleValue::NULL => f.write_str("null"),
other => write!(f, "simple({})", other.0),
},
Self::Unsigned(n) => write!(f, "{n}"),
Self::Negative(n) => write!(f, "{actual}", actual = -i128::from(*n) - 1),
Self::Float(float) => {
let value = float.to_f64();
if value.is_nan() {
use crate::float::Inner;
match float.0 {
Inner::F16(0x7e00) => f.write_str("NaN"), Inner::F16(bits) => write!(f, "float'{bits:04x}'"),
Inner::F32(bits) => write!(f, "float'{bits:08x}'"),
Inner::F64(bits) => write!(f, "float'{bits:016x}'"),
}
} else if value.is_infinite() {
if value.is_sign_positive() {
f.write_str("Infinity")
} else {
f.write_str("-Infinity")
}
} else {
format_ecmascript_float(f, value)
}
}
Self::ByteString(bytes) => {
f.write_str("h'")?;
for b in bytes {
write!(f, "{b:02x}")?;
}
f.write_str("'")
}
Self::TextString(s) => {
f.write_str("\"")?;
for c in s.chars() {
match c {
'"' => f.write_str("\\\"")?,
'\\' => f.write_str("\\\\")?,
'\u{08}' => f.write_str("\\b")?,
'\u{0C}' => f.write_str("\\f")?,
'\n' => f.write_str("\\n")?,
'\r' => f.write_str("\\r")?,
'\t' => f.write_str("\\t")?,
c if c.is_control() => write!(f, "\\u{:04x}", c as u32)?,
c => write!(f, "{c}")?,
}
}
f.write_str("\"")
}
Self::Array(items) => {
let mut list = f.debug_list();
for item in items {
list.entry(item);
}
list.finish()
}
Self::Map(map) => {
let mut m = f.debug_map();
for (key, value) in map {
m.entry(key, value);
}
m.finish()
}
Self::Tag(tag, content) => {
if self.data_type().is_integer() {
if let Ok(n) = self.to_u128() {
return write!(f, "{n}");
}
if let Ok(n) = self.to_i128() {
return write!(f, "{n}");
}
}
if f.alternate() {
write!(f, "{tag}({content:#?})")
} else {
write!(f, "{tag}({content:?})")
}
}
}
}
}
fn format_ecmascript_float(f: &mut fmt::Formatter<'_>, value: f64) -> fmt::Result {
if value == 0.0 {
return f.write_str(if value.is_sign_negative() { "-0.0" } else { "0.0" });
}
let sign = if value.is_sign_negative() { "-" } else { "" };
let scientific = format!("{:e}", value.abs());
let (mantissa, exponent) = scientific.split_once('e').unwrap();
let rust_exp: i32 = exponent.parse().unwrap();
let digits: String = mantissa.chars().filter(|c| *c != '.').collect();
let k = digits.len() as i32;
let e = rust_exp + 1;
f.write_str(sign)?;
if 0 < e && e <= 21 {
if e >= k {
f.write_str(&digits)?;
for _ in 0..(e - k) {
f.write_str("0")?;
}
f.write_str(".0")
} else {
let (int_part, frac_part) = digits.split_at(e as usize);
write!(f, "{int_part}.{frac_part}")
}
} else if -6 < e && e <= 0 {
f.write_str("0.")?;
for _ in 0..(-e) {
f.write_str("0")?;
}
f.write_str(&digits)
} else {
let exp_val = e - 1;
let (first, rest) = digits.split_at(1);
if rest.is_empty() {
write!(f, "{first}.0")?;
} else {
write!(f, "{first}.{rest}")?;
}
if exp_val >= 0 {
write!(f, "e+{exp_val}")
} else {
write!(f, "e{exp_val}")
}
}
}
impl Value {
pub fn take(&mut self) -> Self {
std::mem::take(self)
}
pub fn replace(&mut self, value: Self) -> Self {
std::mem::replace(self, value)
}
#[must_use]
pub fn encode(&self) -> Vec<u8> {
let len = self.encoded_len();
let mut bytes = Vec::with_capacity(len);
self.write_to(&mut bytes).unwrap();
debug_assert_eq!(bytes.len(), len);
bytes
}
#[must_use]
pub fn encode_hex(&self) -> String {
let len2 = self.encoded_len() * 2;
let mut hex = Vec::with_capacity(len2);
self.write_hex_to(&mut hex).unwrap();
debug_assert_eq!(hex.len(), len2);
String::from_utf8(hex).unwrap()
}
pub fn decode(bytes: impl AsRef<[u8]>) -> crate::Result<Self> {
let mut reader = crate::io::SliceReader(bytes.as_ref());
Self::do_read(&mut reader, limits::RECURSION_LIMIT, limits::OOM_MITIGATION)
}
pub fn decode_hex(hex: impl AsRef<[u8]>) -> Result<Self> {
let mut reader = crate::io::HexSliceReader(hex.as_ref());
Self::do_read(&mut reader, limits::RECURSION_LIMIT, limits::OOM_MITIGATION)
}
pub fn read_from(mut reader: impl std::io::Read) -> crate::IoResult<Self> {
Self::do_read(&mut reader, limits::RECURSION_LIMIT, limits::OOM_MITIGATION)
}
pub fn read_hex_from(reader: impl std::io::Read) -> crate::IoResult<Self> {
let mut hex_reader = crate::io::HexReader(reader);
Self::do_read(&mut hex_reader, limits::RECURSION_LIMIT, limits::OOM_MITIGATION)
}
fn do_read<R>(reader: &mut R, recursion_limit: u16, oom_mitigation: usize) -> std::result::Result<Self, R::Error>
where
R: crate::io::MyReader,
R::Error: From<crate::Error>,
{
let head = Head::read_from(reader)?;
let is_float = head.initial_byte.major() == Major::SimpleOrFloat
&& matches!(head.argument, Argument::U16(_) | Argument::U32(_) | Argument::U64(_));
if !is_float && !head.argument.is_deterministic() {
return Err(Error::NonDeterministic.into());
}
let this = match head.initial_byte.major() {
Major::Unsigned => Self::Unsigned(head.value()),
Major::Negative => Self::Negative(head.value()),
Major::ByteString => {
let len = head.value();
if len > limits::LENGTH_LIMIT {
return Err(Error::LengthTooLarge.into());
}
Self::ByteString(reader.read_vec(len)?)
}
Major::TextString => {
let len = head.value();
if len > limits::LENGTH_LIMIT {
return Err(Error::LengthTooLarge.into());
}
let bytes = reader.read_vec(len)?;
let string = String::from_utf8(bytes).map_err(crate::Error::from)?;
Self::TextString(string)
}
Major::Array => {
let value = head.value();
if value > limits::LENGTH_LIMIT {
return Err(Error::LengthTooLarge.into());
}
let Some(recursion_limit) = recursion_limit.checked_sub(1) else {
return Err(Error::LengthTooLarge.into());
};
let request: usize = value.try_into().or(Err(Error::LengthTooLarge))?;
let granted = request.min(oom_mitigation / size_of::<Self>());
let oom_mitigation = oom_mitigation - granted * size_of::<Self>();
let mut vec = Vec::with_capacity(granted);
for _ in 0..value {
vec.push(Self::do_read(reader, recursion_limit, oom_mitigation)?);
}
Self::Array(vec)
}
Major::Map => {
let value = head.value();
if value > limits::LENGTH_LIMIT {
return Err(Error::LengthTooLarge.into());
}
let Some(recursion_limit) = recursion_limit.checked_sub(1) else {
return Err(Error::LengthTooLarge.into());
};
let mut map = BTreeMap::new();
let mut prev = None;
for _ in 0..value {
let key = Self::do_read(reader, recursion_limit, oom_mitigation)?;
let value = Self::do_read(reader, recursion_limit, oom_mitigation)?;
if let Some((prev_key, prev_value)) = prev.take() {
if prev_key >= key {
return Err(Error::NonDeterministic.into());
}
map.insert(prev_key, prev_value);
}
prev = Some((key, value));
}
if let Some((key, value)) = prev.take() {
map.insert(key, value);
}
Self::Map(map)
}
Major::Tag => {
let Some(recursion_limit) = recursion_limit.checked_sub(1) else {
return Err(Error::LengthTooLarge.into());
};
let tag_number = head.value();
let tag_content = Box::new(Self::do_read(reader, recursion_limit, oom_mitigation)?);
let this = Self::Tag(tag_number, tag_content);
if this.data_type() == DataType::BigInt {
let bytes = this.as_bytes().unwrap();
let valid = bytes.len() >= 8 && bytes[0] != 0;
if !valid {
return Err(Error::NonDeterministic.into());
}
}
this
}
Major::SimpleOrFloat => match head.argument {
Argument::None => Self::SimpleValue(SimpleValue(head.initial_byte.info())),
Argument::U8(n) if n >= 32 => Self::SimpleValue(SimpleValue(n)),
Argument::U16(bits) => Self::Float(Float::from_u16(bits)),
Argument::U32(bits) => Self::Float(Float::from_u32(bits)?),
Argument::U64(bits) => Self::Float(Float::from_u64(bits)?),
_ => return Err(Error::Malformed.into()),
},
};
Ok(this)
}
pub fn write_to(&self, mut writer: impl std::io::Write) -> crate::IoResult<()> {
self.do_write(&mut writer)
}
fn do_write(&self, writer: &mut impl std::io::Write) -> crate::IoResult<()> {
self.cbor_head().write_to(writer)?;
match self {
Value::ByteString(bytes) => writer.write_all(bytes)?,
Value::TextString(string) => writer.write_all(string.as_bytes())?,
Value::Tag(_number, content) => content.do_write(writer)?,
Value::Array(values) => {
for value in values {
value.do_write(writer)?;
}
}
Value::Map(map) => {
for (key, value) in map {
key.do_write(writer)?;
value.do_write(writer)?;
}
}
_ => (),
}
Ok(())
}
pub fn write_hex_to(&self, writer: impl std::io::Write) -> crate::IoResult<()> {
struct HexWriter<W>(W);
impl<W: std::io::Write> std::io::Write for HexWriter<W> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
for &byte in buf {
write!(self.0, "{byte:02x}")?;
}
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
self.do_write(&mut HexWriter(writer))
}
pub(crate) fn cbor_head(&self) -> Head {
match self {
Value::SimpleValue(sv) => Head::from_value(Major::SimpleOrFloat, sv.0.into()),
Value::Unsigned(n) => Head::from_value(Major::Unsigned, *n),
Value::Negative(n) => Head::from_value(Major::Negative, *n),
Value::Float(float) => float.cbor_head(),
Value::ByteString(bytes) => Head::from_value(Major::ByteString, bytes.len().try_into().unwrap()),
Value::TextString(text) => Head::from_value(Major::TextString, text.len().try_into().unwrap()),
Value::Array(vec) => Head::from_value(Major::Array, vec.len().try_into().unwrap()),
Value::Map(map) => Head::from_value(Major::Map, map.len().try_into().unwrap()),
Value::Tag(number, _content) => Head::from_value(Major::Tag, *number),
}
}
fn encoded_len(&self) -> usize {
let data_len = match self {
Self::ByteString(bytes) => bytes.len(),
Self::TextString(text) => text.len(),
Self::Array(vec) => vec.iter().map(Self::encoded_len).sum(),
Self::Map(map) => map.iter().map(|(k, v)| k.encoded_len() + v.encoded_len()).sum(),
Self::Tag(_, content) => content.encoded_len(),
_ => 0,
};
self.cbor_head().encoded_len() + data_len
}
#[must_use]
pub const fn null() -> Self {
Self::SimpleValue(SimpleValue::NULL)
}
pub fn simple_value(value: impl TryInto<SimpleValue>) -> Self {
match value.try_into() {
Ok(sv) => Self::SimpleValue(sv),
Err(_) => panic!("Invalid simple value"),
}
}
pub fn date_time(value: impl TryInto<DateTime>) -> Self {
match value.try_into() {
Ok(dt) => dt.into(),
Err(_) => panic!("Invalid date/time"),
}
}
pub fn epoch_time(value: impl TryInto<EpochTime>) -> Self {
match value.try_into() {
Ok(et) => et.into(),
Err(_) => panic!("Invalid epoch time"),
}
}
pub fn float(value: impl Into<Float>) -> Self {
Self::Float(value.into())
}
pub fn array(array: impl Into<Array>) -> Self {
Self::Array(array.into().0)
}
pub fn map(map: impl Into<Map>) -> Self {
Self::Map(map.into().0)
}
pub fn tag(number: u64, content: impl Into<Value>) -> Self {
Self::Tag(number, Box::new(content.into()))
}
#[must_use]
pub const fn data_type(&self) -> DataType {
match self {
Self::SimpleValue(sv) => sv.data_type(),
Self::Unsigned(_) | Self::Negative(_) => DataType::Int,
Self::Float(float) => float.data_type(),
Self::TextString(_) => DataType::Text,
Self::ByteString(_) => DataType::Bytes,
Self::Array(_) => DataType::Array,
Self::Map(_) => DataType::Map,
Self::Tag(tag::DATE_TIME, content) if content.data_type().is_text() => DataType::DateTime,
Self::Tag(tag::EPOCH_TIME, content) if content.data_type().is_numeric() => DataType::EpochTime,
Self::Tag(tag::POS_BIG_INT | tag::NEG_BIG_INT, content) if content.data_type().is_bytes() => {
DataType::BigInt
}
Self::Tag(_, _) => DataType::Tag,
}
}
pub(crate) const fn is_bytes(&self) -> bool {
self.data_type().is_bytes()
}
pub const fn to_bool(&self) -> Result<bool> {
match self {
Self::SimpleValue(sv) => sv.to_bool(),
Self::Tag(_number, content) => content.untagged().to_bool(),
_ => Err(Error::IncompatibleType(self.data_type())),
}
}
pub const fn to_simple_value(&self) -> Result<u8> {
match self {
Self::SimpleValue(sv) => Ok(sv.0),
Self::Tag(_number, content) => content.untagged().to_simple_value(),
_ => Err(Error::IncompatibleType(self.data_type())),
}
}
fn to_uint<T>(&self) -> Result<T>
where
T: TryFrom<u64> + TryFrom<u128>,
{
match self {
Self::Unsigned(x) => T::try_from(*x).or(Err(Error::Overflow)),
Self::Negative(_) => Err(Error::NegativeUnsigned),
Self::Tag(tag::POS_BIG_INT, content) if content.is_bytes() => {
T::try_from(u128_from_slice(self.as_bytes()?)?).or(Err(Error::Overflow))
}
Self::Tag(tag::NEG_BIG_INT, content) if content.is_bytes() => Err(Error::NegativeUnsigned),
Self::Tag(_other_number, content) => content.peeled().to_uint(),
_ => Err(Error::IncompatibleType(self.data_type())),
}
}
pub fn to_u8(&self) -> Result<u8> {
self.to_uint()
}
pub fn to_u16(&self) -> Result<u16> {
self.to_uint()
}
pub fn to_u32(&self) -> Result<u32> {
self.to_uint()
}
pub fn to_u64(&self) -> Result<u64> {
self.to_uint()
}
pub fn to_u128(&self) -> Result<u128> {
self.to_uint()
}
pub fn to_usize(&self) -> Result<usize> {
self.to_uint()
}
#[allow(dead_code)]
pub(crate) fn as_integer_bytes(&self) -> Result<IntegerBytes<'_>> {
match self {
Self::Unsigned(x) => Ok(IntegerBytes::UnsignedOwned(x.to_be_bytes())),
Self::Negative(x) => Ok(IntegerBytes::NegativeOwned(x.to_be_bytes())),
Self::Tag(tag::POS_BIG_INT, content) if content.is_bytes() => {
Ok(IntegerBytes::UnsignedBorrowed(content.as_bytes()?))
}
Self::Tag(tag::NEG_BIG_INT, content) if content.is_bytes() => {
Ok(IntegerBytes::NegativeBorrowed(content.as_bytes()?))
}
Self::Tag(_other_number, content) => content.peeled().as_integer_bytes(),
_ => Err(Error::IncompatibleType(self.data_type())),
}
}
fn to_sint<T>(&self) -> Result<T>
where
T: TryFrom<u64> + TryFrom<u128> + std::ops::Not<Output = T>,
{
match self {
Self::Unsigned(x) => T::try_from(*x).or(Err(Error::Overflow)),
Self::Negative(x) => T::try_from(*x).map(T::not).or(Err(Error::Overflow)),
Self::Tag(tag::POS_BIG_INT, content) if content.is_bytes() => {
T::try_from(u128_from_slice(self.as_bytes()?)?).or(Err(Error::Overflow))
}
Self::Tag(tag::NEG_BIG_INT, content) if content.is_bytes() => {
T::try_from(u128_from_slice(self.as_bytes()?)?)
.map(T::not)
.or(Err(Error::Overflow))
}
Self::Tag(_other_number, content) => content.peeled().to_sint(),
_ => Err(Error::IncompatibleType(self.data_type())),
}
}
pub fn to_i8(&self) -> Result<i8> {
self.to_sint()
}
pub fn to_i16(&self) -> Result<i16> {
self.to_sint()
}
pub fn to_i32(&self) -> Result<i32> {
self.to_sint()
}
pub fn to_i64(&self) -> Result<i64> {
self.to_sint()
}
pub fn to_i128(&self) -> Result<i128> {
self.to_sint()
}
pub fn to_isize(&self) -> Result<isize> {
self.to_sint()
}
pub fn to_f32(&self) -> Result<f32> {
match self {
Self::Float(float) => float.to_f32(),
Self::Tag(_number, content) => content.untagged().to_f32(),
_ => Err(Error::IncompatibleType(self.data_type())),
}
}
pub fn to_f64(&self) -> Result<f64> {
match self {
Self::Float(float) => Ok(float.to_f64()),
Self::Tag(_number, content) => content.untagged().to_f64(),
_ => Err(Error::IncompatibleType(self.data_type())),
}
}
pub fn to_system_time(&self) -> Result<SystemTime> {
if let Ok(s) = self.as_str() {
Ok(s.parse::<crate::iso3339::Timestamp>()?.try_into()?)
} else if let Ok(f) = self.to_f64() {
if f.is_finite() && (0.0..=253402300799.0).contains(&f) {
Ok(SystemTime::UNIX_EPOCH + Duration::from_secs_f64(f))
} else {
Err(Error::InvalidValue)
}
} else {
match self.to_u64() {
Ok(secs) if secs <= 253402300799 => Ok(SystemTime::UNIX_EPOCH + Duration::from_secs(secs)),
Ok(_) | Err(Error::NegativeUnsigned) => Err(Error::InvalidValue),
Err(error) => Err(error),
}
}
}
pub fn as_bytes(&self) -> Result<&[u8]> {
match self {
Self::ByteString(vec) => Ok(vec.as_slice()),
Self::Tag(_number, content) => content.untagged().as_bytes(),
_ => Err(Error::IncompatibleType(self.data_type())),
}
}
pub const fn as_bytes_mut(&mut self) -> Result<&mut Vec<u8>> {
match self {
Self::ByteString(vec) => Ok(vec),
Self::Tag(_number, content) => content.untagged_mut().as_bytes_mut(),
_ => Err(Error::IncompatibleType(self.data_type())),
}
}
pub fn into_bytes(self) -> Result<Vec<u8>> {
match self {
Self::ByteString(vec) => Ok(vec),
Self::Tag(_number, content) => content.into_untagged().into_bytes(),
_ => Err(Error::IncompatibleType(self.data_type())),
}
}
pub fn as_str(&self) -> Result<&str> {
match self {
Self::TextString(s) => Ok(s.as_str()),
Self::Tag(_number, content) => content.untagged().as_str(),
_ => Err(Error::IncompatibleType(self.data_type())),
}
}
pub const fn as_string_mut(&mut self) -> Result<&mut String> {
match self {
Self::TextString(s) => Ok(s),
Self::Tag(_number, content) => content.untagged_mut().as_string_mut(),
_ => Err(Error::IncompatibleType(self.data_type())),
}
}
pub fn into_string(self) -> Result<String> {
match self {
Self::TextString(s) => Ok(s),
Self::Tag(_number, content) => content.into_untagged().into_string(),
_ => Err(Error::IncompatibleType(self.data_type())),
}
}
pub fn as_array(&self) -> Result<&[Value]> {
match self {
Self::Array(v) => Ok(v.as_slice()),
Self::Tag(_number, content) => content.untagged().as_array(),
_ => Err(Error::IncompatibleType(self.data_type())),
}
}
pub const fn as_array_mut(&mut self) -> Result<&mut Vec<Value>> {
match self {
Self::Array(v) => Ok(v),
Self::Tag(_number, content) => content.untagged_mut().as_array_mut(),
_ => Err(Error::IncompatibleType(self.data_type())),
}
}
pub fn into_array(self) -> Result<Vec<Value>> {
match self {
Self::Array(v) => Ok(v),
Self::Tag(_number, content) => content.into_untagged().into_array(),
_ => Err(Error::IncompatibleType(self.data_type())),
}
}
pub const fn as_map(&self) -> Result<&BTreeMap<Value, Value>> {
match self {
Self::Map(m) => Ok(m),
Self::Tag(_number, content) => content.untagged().as_map(),
_ => Err(Error::IncompatibleType(self.data_type())),
}
}
pub const fn as_map_mut(&mut self) -> Result<&mut BTreeMap<Value, Value>> {
match self {
Self::Map(m) => Ok(m),
Self::Tag(_number, content) => content.untagged_mut().as_map_mut(),
_ => Err(Error::IncompatibleType(self.data_type())),
}
}
pub fn into_map(self) -> Result<BTreeMap<Value, Value>> {
match self {
Self::Map(m) => Ok(m),
Self::Tag(_number, content) => content.into_untagged().into_map(),
_ => Err(Error::IncompatibleType(self.data_type())),
}
}
pub fn get<'a>(&self, index: impl Into<crate::ValueKey<'a>>) -> Option<&Value> {
let key = index.into();
match self.untagged() {
Value::Array(arr) => key.to_usize().and_then(|idx| arr.get(idx)),
Value::Map(map) => map.get(&key as &dyn AsValueKey),
_ => None,
}
}
pub fn get_mut<'a>(&mut self, index: impl Into<crate::ValueKey<'a>>) -> Option<&mut Value> {
let key = index.into();
match self.untagged_mut() {
Value::Array(arr) => key.to_usize().and_then(|idx| arr.get_mut(idx)),
Value::Map(map) => map.get_mut(&key as &dyn AsValueKey),
_ => None,
}
}
pub fn remove<'a>(&mut self, index: impl Into<crate::ValueKey<'a>>) -> Option<Value> {
let key = index.into();
match self.untagged_mut() {
Value::Array(arr) => {
let idx = key.to_usize().expect("array index must be a non-negative integer");
assert!(idx < arr.len(), "array index {idx} out of bounds (len {})", arr.len());
Some(arr.remove(idx))
}
Value::Map(map) => map.remove(&key as &dyn AsValueKey),
other => panic!("remove called on {:?}, expected array or map", other.data_type()),
}
}
pub fn insert(&mut self, key: impl Into<Value>, value: impl Into<Value>) -> Option<Value> {
let key = key.into();
let value = value.into();
match self.untagged_mut() {
Value::Array(arr) => {
let idx = key.to_usize().expect("array index must be a non-negative integer");
assert!(idx <= arr.len(), "array index {idx} out of bounds (len {})", arr.len());
arr.insert(idx, value);
None
}
Value::Map(map) => map.insert(key, value),
other => panic!("insert called on {:?}, expected array or map", other.data_type()),
}
}
pub fn append(&mut self, value: impl Into<Value>) {
match self.untagged_mut() {
Value::Array(arr) => arr.push(value.into()),
other => panic!("append called on {:?}, expected array", other.data_type()),
}
}
pub fn contains<'a>(&self, key: impl Into<crate::ValueKey<'a>>) -> bool {
let key = key.into();
match self.untagged() {
Value::Array(arr) => key.to_usize().is_some_and(|idx| idx < arr.len()),
Value::Map(map) => map.contains_key(&key as &dyn AsValueKey),
_ => false,
}
}
#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> Option<usize> {
match self.untagged() {
Value::Array(arr) => Some(arr.len()),
Value::Map(map) => Some(map.len()),
_ => None,
}
}
pub const fn tag_number(&self) -> Result<u64> {
match self {
Self::Tag(number, _content) => Ok(*number),
_ => Err(Error::IncompatibleType(self.data_type())),
}
}
pub const fn tag_content(&self) -> Result<&Self> {
match self {
Self::Tag(_tag, content) => Ok(content),
_ => Err(Error::IncompatibleType(self.data_type())),
}
}
pub const fn tag_content_mut(&mut self) -> Result<&mut Self> {
match self {
Self::Tag(_, value) => Ok(value),
_ => Err(Error::IncompatibleType(self.data_type())),
}
}
pub fn as_tag(&self) -> Result<(u64, &Value)> {
match self {
Self::Tag(number, content) => Ok((*number, content)),
_ => Err(Error::IncompatibleType(self.data_type())),
}
}
pub fn as_tag_mut(&mut self) -> Result<(u64, &mut Value)> {
match self {
Self::Tag(number, content) => Ok((*number, content)),
_ => Err(Error::IncompatibleType(self.data_type())),
}
}
pub fn into_tag(self) -> Result<(u64, Value)> {
match self {
Self::Tag(number, content) => Ok((number, *content)),
_ => Err(Error::IncompatibleType(self.data_type())),
}
}
pub fn remove_tag(&mut self) -> Option<u64> {
let mut result = None;
if let Self::Tag(number, content) = self {
result = Some(*number);
*self = std::mem::take(content);
}
result
}
pub fn remove_all_tags(&mut self) -> Vec<u64> {
let mut tags = Vec::new();
while let Self::Tag(number, content) = self {
tags.push(*number);
*self = std::mem::take(content);
}
tags
}
#[must_use]
pub(crate) const fn peeled(&self) -> &Self {
let mut result = self;
while let Self::Tag(_, content) = result
&& content.data_type().is_tag()
{
result = content;
}
result
}
#[must_use]
pub const fn untagged(&self) -> &Self {
let mut result = self;
while let Self::Tag(_, content) = result {
result = content;
}
result
}
pub const fn untagged_mut(&mut self) -> &mut Self {
let mut result = self;
while let Self::Tag(_, content) = result {
result = content;
}
result
}
#[must_use]
pub fn into_untagged(mut self) -> Self {
while let Self::Tag(_number, content) = self {
self = *content;
}
self
}
}