#[allow(unused_imports)]
use crate::nostd_prelude::*;
#[derive(Clone, Debug, PartialEq)]
pub enum Value {
Integer(i128),
Bytes(Vec<u8>),
Text(String),
Array(Vec<Value>),
Map(Vec<(Value, Value)>),
Tag(u64, Box<Value>),
Bool(bool),
Null,
Float(f64),
}
impl Value {
pub fn into_integer(self) -> Option<i128> {
match self {
Value::Integer(n) => Some(n),
_ => None,
}
}
pub fn into_bytes(self) -> Option<Vec<u8>> {
match self {
Value::Bytes(b) => Some(b),
_ => None,
}
}
pub fn into_text(self) -> Option<String> {
match self {
Value::Text(t) => Some(t),
_ => None,
}
}
pub fn into_array(self) -> Option<Vec<Value>> {
match self {
Value::Array(a) => Some(a),
_ => None,
}
}
pub fn into_tag(self) -> Option<(u64, Value)> {
match self {
Value::Tag(t, v) => Some((t, *v)),
_ => None,
}
}
}
mod minimal_value_serde;
pub use minimal_value_serde::*;
#[derive(Clone, Debug, PartialEq)]
pub struct Tagged<T> {
pub tag: u64,
pub value: T,
}
impl<T> Tagged<T> {
pub fn new(tag: u64, value: T) -> Self {
Self { tag, value }
}
}
pub fn to_value<T: serde::Serialize>(value: &T) -> Result<Value, String> {
let bytes = crate::cbor::encode(value).map_err(|e| e.to_string())?;
let val: Value = crate::cbor::decode(&bytes).map_err(|e| e.to_string())?;
Ok(val)
}
pub fn from_value<T: serde::de::DeserializeOwned>(value: &Value) -> Result<T, String> {
let bytes = crate::cbor::encode(value).map_err(|e| e.to_string())?;
crate::cbor::decode(&bytes).map_err(|e| e.to_string())
}