|
@@ -0,0 +1,375 @@
|
|
|
|
+use std::{collections::HashMap, fmt::Display};
|
|
|
|
+use tokio::select;
|
|
|
|
+
|
|
|
|
+use crate::Shared;
|
|
|
|
+
|
|
|
|
+#[derive(Debug, PartialEq, Clone)]
|
|
|
|
+pub enum Data {
|
|
|
|
+ None,
|
|
|
|
+ String(String),
|
|
|
|
+ I8(i8),
|
|
|
|
+ I16(i16),
|
|
|
|
+ I32(i32),
|
|
|
|
+ I64(i64),
|
|
|
|
+ I128(i128),
|
|
|
|
+ U8(u8),
|
|
|
|
+ U16(u16),
|
|
|
|
+ U32(u32),
|
|
|
|
+ U64(u64),
|
|
|
|
+ U128(u128),
|
|
|
|
+ F32(f32),
|
|
|
|
+ F64(f64),
|
|
|
|
+ Bool(bool),
|
|
|
|
+ Vec(Vec<Data>),
|
|
|
|
+ HashMap(HashMap<String, Data>),
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl Default for Data {
|
|
|
|
+ fn default() -> Self {
|
|
|
|
+ Self::None
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl Display for Data {
|
|
|
|
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
+ match self {
|
|
|
|
+ Self::None => f.write_str("None"),
|
|
|
|
+ Self::String(s) => f.write_str(&s),
|
|
|
|
+ Self::I8(i) => f.write_str(format!("{}", i).as_str()),
|
|
|
|
+ Self::I16(i) => f.write_str(format!("{}", i).as_str()),
|
|
|
|
+ Self::I32(i) => f.write_str(format!("{}", i).as_str()),
|
|
|
|
+ Self::I64(i) => f.write_str(format!("{}", i).as_str()),
|
|
|
|
+ Self::I128(i) => f.write_str(format!("{}", i).as_str()),
|
|
|
|
+ Self::U8(i) => f.write_str(format!("{}", i).as_str()),
|
|
|
|
+ Self::U16(i) => f.write_str(format!("{}", i).as_str()),
|
|
|
|
+ Self::U32(i) => f.write_str(format!("{}", i).as_str()),
|
|
|
|
+ Self::U64(i) => f.write_str(format!("{}", i).as_str()),
|
|
|
|
+ Self::U128(i) => f.write_str(format!("{}", i).as_str()),
|
|
|
|
+ Self::F32(v) => f.write_str(format!("{}", v).as_str()),
|
|
|
|
+ Self::F64(v) => f.write_str(format!("{}", v).as_str()),
|
|
|
|
+ Self::Bool(b) => f.write_str(format!("{}", b).as_str()),
|
|
|
|
+ Self::Vec(v) => {
|
|
|
|
+ let mut out: String = "[".to_string();
|
|
|
|
+ for d in v.iter() {
|
|
|
|
+ if out != "[" {
|
|
|
|
+ out.push_str(", ");
|
|
|
|
+ }
|
|
|
|
+ out.push_str(format!("{}", d).as_str());
|
|
|
|
+ }
|
|
|
|
+ out.push_str("]");
|
|
|
|
+ f.write_str(out.as_str())
|
|
|
|
+ },
|
|
|
|
+ Self::HashMap(hm) => {
|
|
|
|
+ let mut out: String = "{".to_string();
|
|
|
|
+ for (k, v) in hm.iter() {
|
|
|
|
+ if out != "{" {
|
|
|
|
+ out.push_str(", ");
|
|
|
|
+ }
|
|
|
|
+ out.push_str(format!("{}: {}", k, v).as_str());
|
|
|
|
+ }
|
|
|
|
+ out.push_str("}");
|
|
|
|
+ f.write_str(out.as_str())
|
|
|
|
+ },
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl From<String> for Data {
|
|
|
|
+ fn from(value: String) -> Self {
|
|
|
|
+ Self::String(value)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl From<i8> for Data {
|
|
|
|
+ fn from(value: i8) -> Self {
|
|
|
|
+ Self::I8(value)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl From<i16> for Data {
|
|
|
|
+ fn from(value: i16) -> Self {
|
|
|
|
+ Self::I16(value)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl From<i32> for Data {
|
|
|
|
+ fn from(value: i32) -> Self {
|
|
|
|
+ Self::I32(value)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl From<i64> for Data {
|
|
|
|
+ fn from(value: i64) -> Self {
|
|
|
|
+ Self::I64(value)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl From<i128> for Data {
|
|
|
|
+ fn from(value: i128) -> Self {
|
|
|
|
+ Self::I128(value)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl From<u8> for Data {
|
|
|
|
+ fn from(value: u8) -> Self {
|
|
|
|
+ Self::U8(value)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl From<u16> for Data {
|
|
|
|
+ fn from(value: u16) -> Self {
|
|
|
|
+ Self::U16(value)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl From<u32> for Data {
|
|
|
|
+ fn from(value: u32) -> Self {
|
|
|
|
+ Self::U32(value)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl From<u64> for Data {
|
|
|
|
+ fn from(value: u64) -> Self {
|
|
|
|
+ Self::U64(value)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl From<u128> for Data {
|
|
|
|
+ fn from(value: u128) -> Self {
|
|
|
|
+ Self::U128(value)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl From<f32> for Data {
|
|
|
|
+ fn from(value: f32) -> Self {
|
|
|
|
+ Self::F32(value)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl From<f64> for Data {
|
|
|
|
+ fn from(value: f64) -> Self {
|
|
|
|
+ Self::F64(value)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl From<bool> for Data {
|
|
|
|
+ fn from(value: bool) -> Self {
|
|
|
|
+ Self::Bool(value)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl From<Vec<Data>> for Data {
|
|
|
|
+ fn from(value: Vec<Data>) -> Self {
|
|
|
|
+ Self::Vec(value)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl From<HashMap<String, Data>> for Data {
|
|
|
|
+ fn from(value: HashMap<String, Data>) -> Self {
|
|
|
|
+ Self::HashMap(value)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl TryInto<String> for Data {
|
|
|
|
+ type Error = ();
|
|
|
|
+ fn try_into(self) -> Result<String, Self::Error> {
|
|
|
|
+ match self {
|
|
|
|
+ Self::String(s) => Ok(s),
|
|
|
|
+ _ => Err(()),
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl TryInto<i8> for Data {
|
|
|
|
+ type Error = ();
|
|
|
|
+ fn try_into(self) -> Result<i8, Self::Error> {
|
|
|
|
+ match self {
|
|
|
|
+ Self::I8(v) => Ok(v),
|
|
|
|
+ _ => Err(()),
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl TryInto<i16> for Data {
|
|
|
|
+ type Error = ();
|
|
|
|
+ fn try_into(self) -> Result<i16, Self::Error> {
|
|
|
|
+ match self {
|
|
|
|
+ Self::I16(v) => Ok(v),
|
|
|
|
+ _ => Err(()),
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl TryInto<i32> for Data {
|
|
|
|
+ type Error = ();
|
|
|
|
+ fn try_into(self) -> Result<i32, Self::Error> {
|
|
|
|
+ match self {
|
|
|
|
+ Self::I32(v) => Ok(v),
|
|
|
|
+ _ => Err(()),
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl TryInto<i64> for Data {
|
|
|
|
+ type Error = ();
|
|
|
|
+ fn try_into(self) -> Result<i64, Self::Error> {
|
|
|
|
+ match self {
|
|
|
|
+ Self::I64(v) => Ok(v),
|
|
|
|
+ _ => Err(()),
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl TryInto<i128> for Data {
|
|
|
|
+ type Error = ();
|
|
|
|
+ fn try_into(self) -> Result<i128, Self::Error> {
|
|
|
|
+ match self {
|
|
|
|
+ Self::I128(v) => Ok(v),
|
|
|
|
+ _ => Err(()),
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl TryInto<u8> for Data {
|
|
|
|
+ type Error = ();
|
|
|
|
+ fn try_into(self) -> Result<u8, Self::Error> {
|
|
|
|
+ match self {
|
|
|
|
+ Self::U8(v) => Ok(v),
|
|
|
|
+ _ => Err(()),
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl TryInto<u16> for Data {
|
|
|
|
+ type Error = ();
|
|
|
|
+ fn try_into(self) -> Result<u16, Self::Error> {
|
|
|
|
+ match self {
|
|
|
|
+ Self::U16(v) => Ok(v),
|
|
|
|
+ _ => Err(()),
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl TryInto<u32> for Data {
|
|
|
|
+ type Error = ();
|
|
|
|
+ fn try_into(self) -> Result<u32, Self::Error> {
|
|
|
|
+ match self {
|
|
|
|
+ Self::U32(v) => Ok(v),
|
|
|
|
+ _ => Err(()),
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl TryInto<u64> for Data {
|
|
|
|
+ type Error = ();
|
|
|
|
+ fn try_into(self) -> Result<u64, Self::Error> {
|
|
|
|
+ match self {
|
|
|
|
+ Self::U64(v) => Ok(v),
|
|
|
|
+ _ => Err(()),
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl TryInto<u128> for Data {
|
|
|
|
+ type Error = ();
|
|
|
|
+ fn try_into(self) -> Result<u128, Self::Error> {
|
|
|
|
+ match self {
|
|
|
|
+ Self::U128(v) => Ok(v),
|
|
|
|
+ _ => Err(()),
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl TryInto<f32> for Data {
|
|
|
|
+ type Error = ();
|
|
|
|
+ fn try_into(self) -> Result<f32, Self::Error> {
|
|
|
|
+ match self {
|
|
|
|
+ Self::F32(v) => Ok(v),
|
|
|
|
+ _ => Err(()),
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl TryInto<f64> for Data {
|
|
|
|
+ type Error = ();
|
|
|
|
+ fn try_into(self) -> Result<f64, Self::Error> {
|
|
|
|
+ match self {
|
|
|
|
+ Self::F64(v) => Ok(v),
|
|
|
|
+ _ => Err(()),
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl TryInto<bool> for Data {
|
|
|
|
+ type Error = ();
|
|
|
|
+ fn try_into(self) -> Result<bool, Self::Error> {
|
|
|
|
+ match self {
|
|
|
|
+ Self::Bool(v) => Ok(v),
|
|
|
|
+ _ => Err(()),
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl TryInto<Vec<Data>> for Data {
|
|
|
|
+ type Error = ();
|
|
|
|
+ fn try_into(self) -> Result<Vec<Data>, Self::Error> {
|
|
|
|
+ match self {
|
|
|
|
+ Self::Vec(v) => Ok(v),
|
|
|
|
+ _ => Err(()),
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl TryInto<HashMap<String, Data>> for Data {
|
|
|
|
+ type Error = ();
|
|
|
|
+ fn try_into(self) -> Result<HashMap<String, Data>, Self::Error> {
|
|
|
|
+ match self {
|
|
|
|
+ Self::HashMap(v) => Ok(v),
|
|
|
|
+ _ => Err(()),
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl Into<Shared<Data>> for Data {
|
|
|
|
+ fn into(self) -> Shared<Data> {
|
|
|
|
+ Shared::new(self)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl Data {
|
|
|
|
+ pub fn is_some(&self) -> bool {
|
|
|
|
+ !matches!(self, Self::None)
|
|
|
|
+ }
|
|
|
|
+ pub fn is_none(&self) -> bool {
|
|
|
|
+ matches!(self, Self::None)
|
|
|
|
+ }
|
|
|
|
+ pub fn is_string(&self) -> bool {
|
|
|
|
+ matches!(self, Self::String(_))
|
|
|
|
+ }
|
|
|
|
+ pub fn is_number(&self) -> bool {
|
|
|
|
+ match self {
|
|
|
|
+ Self::I8(_) | Self::I16(_) | Self::I32(_) | Self::I64(_) | Self::I128(_) => true,
|
|
|
|
+ Self::U8(_) | Self::U16(_) | Self::U32(_) | Self::U64(_) | Self::U128(_) => true,
|
|
|
|
+ Self::F32(_) | Self::F64(_) => true,
|
|
|
|
+ _ => false
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ pub fn is_int(&self) -> bool {
|
|
|
|
+ match self {
|
|
|
|
+ Self::I8(_) | Self::I16(_) | Self::I32(_) | Self::I64(_) | Self::I128(_) => true,
|
|
|
|
+ _ => false
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ pub fn is_uint(&self) -> bool {
|
|
|
|
+ match self {
|
|
|
|
+ Self::U8(_) | Self::U16(_) | Self::U32(_) | Self::U64(_) | Self::U128(_) => true,
|
|
|
|
+ _ => false
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ pub fn is_float(&self) -> bool {
|
|
|
|
+ matches!(self, Self::F32(_) | Self::F64(_))
|
|
|
|
+ }
|
|
|
|
+ pub fn is_nested(&self) -> bool {
|
|
|
|
+ matches!(self, Self::Vec(_) | Self::HashMap(_))
|
|
|
|
+ }
|
|
|
|
+}
|