#![deny(missing_docs)]
use std::sync::Arc;
mod cmp_impl;
mod from_impl;
mod map;
mod serde_impl;
pub use map::Map;
#[derive(Clone, Debug, Default)]
pub enum GcValue {
Text(Arc<str>),
Int(i128),
Float(f64),
Boolean(bool),
Array(Arc<[GcValue]>),
Map(Arc<map::Map>),
Tag(Arc<(GcValue, GcValue)>),
Bytes(Arc<[u8]>),
#[default]
Null,
}
impl GcValue {
pub fn new_text<T: Into<Arc<str>>>(v: T) -> Self {
Self::Text(v.into())
}
pub fn new_int<T: Into<i128>>(v: T) -> Self {
Self::Int(v.into())
}
pub fn new_float<T: Into<f64>>(v: T) -> Self {
Self::Float(v.into())
}
pub fn new_boolean<T: Into<bool>>(v: T) -> Self {
Self::Boolean(v.into())
}
pub fn new_array<T: Into<Arc<[GcValue]>>>(v: T) -> Self {
Self::Array(v.into())
}
pub fn new_map<T: Into<Arc<Map>>>(v: T) -> Self {
Self::Map(v.into())
}
pub fn new_tag<T: Into<Arc<(GcValue, GcValue)>>>(v: T) -> Self {
Self::Tag(v.into())
}
pub fn new_bytes<T: Into<Arc<[u8]>>>(v: T) -> Self {
Self::Bytes(v.into())
}
pub fn new_null() -> Self {
Self::Null
}
pub fn collect_array<T, U>(values: T) -> Self
where
T: IntoIterator<Item = U>,
GcValue: From<U>,
{
Self::Array(values.into_iter().map(From::from).collect())
}
pub fn collect_map<T, K, V>(pairs: T) -> Self
where
T: IntoIterator<Item = (K, V)>,
GcValue: From<K> + From<V>,
{
Self::Map(Arc::new(pairs.into_iter().collect()))
}
pub fn collect_tag<T, U>(tag: T, value: U) -> Self
where
GcValue: From<T> + From<U>,
{
Self::Tag(Arc::new((tag.into(), value.into())))
}
pub fn collect_bytes<T>(values: T) -> Self
where
T: IntoIterator<Item = u8>,
{
Self::Bytes(values.into_iter().collect())
}
pub fn as_text(&self) -> Option<&Arc<str>> {
if let Self::Text(ret) = self {
Some(ret)
} else {
None
}
}
pub fn as_int(&self) -> Option<i128> {
if let Self::Int(ret) = self {
Some(*ret)
} else {
None
}
}
pub fn as_float(&self) -> Option<f64> {
if let Self::Float(ret) = self {
Some(*ret)
} else {
None
}
}
pub fn as_boolean(&self) -> Option<bool> {
if let Self::Boolean(ret) = self {
Some(*ret)
} else {
None
}
}
pub fn as_array(&self) -> Option<&[GcValue]> {
if let Self::Array(ret) = self {
Some(ret.as_ref())
} else {
None
}
}
pub fn as_map(&self) -> Option<&Arc<Map>> {
if let Self::Map(ret) = self {
Some(ret)
} else {
None
}
}
pub fn as_tag(&self) -> Option<(&GcValue, &GcValue)> {
if let Self::Tag(ret) = self {
let (t, v) = ret.as_ref();
Some((t, v))
} else {
None
}
}
pub fn as_bytes(&self) -> Option<&[u8]> {
if let Self::Bytes(ret) = self {
Some(ret.as_ref())
} else {
None
}
}
pub fn as_null(&self) -> Option<()> {
if let Self::Null = self {
Some(())
} else {
None
}
}
pub fn is_text(&self) -> bool {
self.as_text().is_some()
}
pub fn is_int(&self) -> bool {
self.as_int().is_some()
}
pub fn is_float(&self) -> bool {
self.as_float().is_some()
}
pub fn is_boolean(&self) -> bool {
self.as_boolean().is_some()
}
pub fn is_array(&self) -> bool {
self.as_array().is_some()
}
pub fn is_map(&self) -> bool {
self.as_map().is_some()
}
pub fn is_tag(&self) -> bool {
self.as_tag().is_some()
}
pub fn is_bytes(&self) -> bool {
self.as_bytes().is_some()
}
pub fn is_null(&self) -> bool {
self.as_null().is_some()
}
pub fn is_text_and<F: FnOnce(&str) -> bool>(&self, p: F) -> bool {
self.as_text().map(|txt| p(txt)).is_some()
}
pub fn is_int_and<F: FnOnce(i128) -> bool>(&self, p: F) -> bool {
self.as_int().map(p).is_some()
}
pub fn is_float_and<F: FnOnce(f64) -> bool>(&self, p: F) -> bool {
self.as_float().map(p).is_some()
}
pub fn is_boolean_and<F: FnOnce(bool) -> bool>(&self, p: F) -> bool {
self.as_boolean().map(p).is_some()
}
pub fn is_array_and<F: FnOnce(&[GcValue]) -> bool>(&self, p: F) -> bool {
self.as_array().map(p).is_some()
}
pub fn is_map_and<F: FnOnce(&Map) -> bool>(&self, p: F) -> bool {
self.as_map().map(|m| p(m)).is_some()
}
pub fn is_tag_and<F: FnOnce((&GcValue, &GcValue)) -> bool>(&self, p: F) -> bool {
self.as_tag().map(p).is_some()
}
pub fn is_bytes_and<F: FnOnce(&[u8]) -> bool>(&self, p: F) -> bool {
self.as_bytes().map(p).is_some()
}
pub fn is_null_and<F: FnOnce() -> bool>(&self, p: F) -> bool {
self.as_null().map(|_| p()).is_some()
}
pub fn is_json(&self) -> bool {
match self {
GcValue::Text(_) => true,
GcValue::Int(n) => n.abs() < 1_000_000_000_000_000_000_000,
GcValue::Float(_) => true,
GcValue::Boolean(_) => true,
GcValue::Array(gc_values) => gc_values.iter().all(GcValue::is_json),
GcValue::Map(map) => map.iter().all(|(k, v)| k.is_text() && v.is_json()),
GcValue::Tag(_) => false,
GcValue::Bytes(_) => false,
GcValue::Null => true,
}
}
pub fn is_cbor(&self) -> bool {
match self {
GcValue::Text(_) => true,
GcValue::Int(n) => i64::MIN as i128 <= *n && *n <= i64::MAX as i128,
GcValue::Float(_) => true,
GcValue::Boolean(_) => true,
GcValue::Array(gc_values) => gc_values.iter().all(GcValue::is_cbor),
GcValue::Map(map) => map.iter().all(|(k, v)| k.is_cbor() && v.is_cbor()),
GcValue::Tag(pair) => pair.0.is_u64() && pair.1.is_cbor(),
GcValue::Bytes(_) => true,
GcValue::Null => true,
}
}
fn is_u64(&self) -> bool {
self.is_int_and(|n| 0 <= n && n <= u64::MAX as i128)
}
pub fn to_text(self) -> Option<Arc<str>> {
if let Self::Text(ret) = self {
Some(ret)
} else {
None
}
}
pub fn to_int(self) -> Option<i128> {
if let Self::Int(ret) = self {
Some(ret)
} else {
None
}
}
pub fn to_float(self) -> Option<f64> {
if let Self::Float(ret) = self {
Some(ret)
} else {
None
}
}
pub fn to_boolean(self) -> Option<bool> {
if let Self::Boolean(ret) = self {
Some(ret)
} else {
None
}
}
pub fn to_array(self) -> Option<Arc<[GcValue]>> {
if let Self::Array(ret) = self {
Some(ret)
} else {
None
}
}
pub fn to_map(self) -> Option<Arc<Map>> {
if let Self::Map(ret) = self {
Some(ret)
} else {
None
}
}
pub fn to_tag(self) -> Option<Arc<(GcValue, GcValue)>> {
if let Self::Tag(ret) = self {
Some(ret)
} else {
None
}
}
pub fn to_bytes(self) -> Option<Arc<[u8]>> {
if let Self::Bytes(ret) = self {
Some(ret)
} else {
None
}
}
pub fn to_null(self) -> Option<()> {
if let Self::Null = self {
Some(())
} else {
None
}
}
}