use super::{Data, DataModelType, NamedField, Variant};
use serde::{Deserialize, Serialize};
#[cfg(all(not(feature = "use-std"), feature = "alloc"))]
extern crate alloc;
#[cfg(feature = "use-std")]
use std::{boxed::Box, collections::HashSet, string::String};
#[cfg(all(not(feature = "use-std"), feature = "alloc"))]
use alloc::{boxed::Box, string::String};
impl OwnedDataModelType {
pub fn to_pseudocode(&self) -> String {
let mut buf = String::new();
super::fmt::fmt_owned_dmt_to_buf(self, &mut buf, true);
buf
}
#[cfg(feature = "use-std")]
pub fn all_used_types(&self) -> HashSet<Self> {
let mut buf = HashSet::new();
super::fmt::discover_tys(self, &mut buf);
buf
}
}
impl core::fmt::Display for OwnedDataModelType {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let pc = self.to_pseudocode();
f.write_str(&pc)
}
}
impl crate::Schema for OwnedDataModelType {
const SCHEMA: &'static DataModelType = &DataModelType::Schema;
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum OwnedDataModelType {
Bool,
I8,
U8,
I16,
I32,
I64,
I128,
U16,
U32,
U64,
U128,
Usize,
Isize,
F32,
F64,
Char,
String,
ByteArray,
Option(Box<Self>),
Unit,
Seq(Box<Self>),
Tuple(Box<[Self]>),
Map {
key: Box<Self>,
val: Box<Self>,
},
Struct {
name: Box<str>,
data: OwnedData,
},
Enum {
name: Box<str>,
variants: Box<[OwnedVariant]>,
},
Schema,
}
impl From<&DataModelType> for OwnedDataModelType {
fn from(other: &DataModelType) -> Self {
match other {
DataModelType::Bool => Self::Bool,
DataModelType::I8 => Self::I8,
DataModelType::U8 => Self::U8,
DataModelType::I16 => Self::I16,
DataModelType::I32 => Self::I32,
DataModelType::I64 => Self::I64,
DataModelType::I128 => Self::I128,
DataModelType::U16 => Self::U16,
DataModelType::U32 => Self::U32,
DataModelType::U64 => Self::U64,
DataModelType::U128 => Self::U128,
DataModelType::Usize => Self::Usize,
DataModelType::Isize => Self::Isize,
DataModelType::F32 => Self::F32,
DataModelType::F64 => Self::F64,
DataModelType::Char => Self::Char,
DataModelType::String => Self::String,
DataModelType::ByteArray => Self::ByteArray,
DataModelType::Option(o) => Self::Option(Box::new((*o).into())),
DataModelType::Unit => Self::Unit,
DataModelType::Seq(s) => Self::Seq(Box::new((*s).into())),
DataModelType::Tuple(t) => Self::Tuple(t.iter().map(|i| (*i).into()).collect()),
DataModelType::Map { key, val } => Self::Map {
key: Box::new((*key).into()),
val: Box::new((*val).into()),
},
DataModelType::Struct { name, data } => Self::Struct {
name: (*name).into(),
data: data.into(),
},
DataModelType::Enum { name, variants } => Self::Enum {
name: (*name).into(),
variants: variants.iter().map(|i| (*i).into()).collect(),
},
DataModelType::Schema => Self::Schema,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum OwnedData {
Unit,
Newtype(Box<OwnedDataModelType>),
Tuple(Box<[OwnedDataModelType]>),
Struct(Box<[OwnedNamedField]>),
}
impl From<&Data> for OwnedData {
fn from(data: &Data) -> Self {
match data {
Data::Unit => Self::Unit,
Data::Newtype(d) => Self::Newtype(Box::new((*d).into())),
Data::Tuple(d) => Self::Tuple(d.iter().map(|i| (*i).into()).collect()),
Data::Struct(d) => Self::Struct(d.iter().map(|i| (*i).into()).collect()),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct OwnedNamedField {
pub name: Box<str>,
pub ty: OwnedDataModelType,
}
impl From<&NamedField> for OwnedNamedField {
fn from(value: &NamedField) -> Self {
Self {
name: value.name.into(),
ty: value.ty.into(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct OwnedVariant {
pub name: Box<str>,
pub data: OwnedData,
}
impl From<&Variant> for OwnedVariant {
fn from(value: &Variant) -> Self {
Self {
name: value.name.into(),
data: (&value.data).into(),
}
}
}