use super::{DataModelType, DataModelVariant, NamedType, NamedValue, NamedVariant};
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, vec::Vec};
#[cfg(all(not(feature = "use-std"), feature = "alloc"))]
use alloc::{
boxed::Box,
string::{String, ToString},
vec::Vec,
};
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct OwnedNamedType {
pub name: String,
pub ty: OwnedDataModelType,
}
impl OwnedNamedType {
pub fn to_pseudocode(&self) -> String {
let mut buf = String::new();
super::fmt::fmt_owned_nt_to_buf(self, &mut buf, true);
buf
}
#[cfg(feature = "use-std")]
pub fn all_used_types(&self) -> HashSet<OwnedNamedType> {
let mut buf = HashSet::new();
super::fmt::discover_tys(self, &mut buf);
buf
}
}
impl core::fmt::Display for OwnedNamedType {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let pc = self.to_pseudocode();
f.write_str(&pc)
}
}
impl From<&NamedType> for OwnedNamedType {
fn from(value: &NamedType) -> Self {
Self {
name: value.name.to_string(),
ty: value.ty.into(),
}
}
}
impl crate::Schema for OwnedNamedType {
const SCHEMA: &'static NamedType = &NamedType {
name: "OwnedNamedType",
ty: &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<OwnedNamedType>),
Unit,
UnitStruct,
NewtypeStruct(Box<OwnedNamedType>),
Seq(Box<OwnedNamedType>),
Tuple(Vec<OwnedNamedType>),
TupleStruct(Vec<OwnedNamedType>),
Map {
key: Box<OwnedNamedType>,
val: Box<OwnedNamedType>,
},
Struct(Vec<OwnedNamedValue>),
Enum(Vec<OwnedNamedVariant>),
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::UnitStruct => Self::UnitStruct,
DataModelType::NewtypeStruct(nts) => Self::NewtypeStruct(Box::new((*nts).into())),
DataModelType::Seq(s) => Self::Seq(Box::new((*s).into())),
DataModelType::Tuple(t) => Self::Tuple(t.iter().map(|i| (*i).into()).collect()),
DataModelType::TupleStruct(ts) => {
Self::TupleStruct(ts.iter().map(|i| (*i).into()).collect())
}
DataModelType::Map { key, val } => Self::Map {
key: Box::new((*key).into()),
val: Box::new((*val).into()),
},
DataModelType::Struct(s) => Self::Struct(s.iter().map(|i| (*i).into()).collect()),
DataModelType::Enum(e) => Self::Enum(e.iter().map(|i| (*i).into()).collect()),
DataModelType::Schema => Self::Schema,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum OwnedDataModelVariant {
UnitVariant,
NewtypeVariant(Box<OwnedNamedType>),
TupleVariant(Vec<OwnedNamedType>),
StructVariant(Vec<OwnedNamedValue>),
}
impl From<&DataModelVariant> for OwnedDataModelVariant {
fn from(value: &DataModelVariant) -> Self {
match value {
DataModelVariant::UnitVariant => Self::UnitVariant,
DataModelVariant::NewtypeVariant(d) => Self::NewtypeVariant(Box::new((*d).into())),
DataModelVariant::TupleVariant(d) => {
Self::TupleVariant(d.iter().map(|i| (*i).into()).collect())
}
DataModelVariant::StructVariant(d) => {
Self::StructVariant(d.iter().map(|i| (*i).into()).collect())
}
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct OwnedNamedValue {
pub name: String,
pub ty: OwnedNamedType,
}
impl From<&NamedValue> for OwnedNamedValue {
fn from(value: &NamedValue) -> Self {
Self {
name: value.name.to_string(),
ty: value.ty.into(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct OwnedNamedVariant {
pub name: String,
pub ty: OwnedDataModelVariant,
}
impl From<&NamedVariant> for OwnedNamedVariant {
fn from(value: &NamedVariant) -> Self {
Self {
name: value.name.to_string(),
ty: value.ty.into(),
}
}
}