use std::borrow::Cow;
use std::sync::Arc;
use flow_types::{EnumShape, JsonValueCtor, OptionCtor, RecordShape, StdEnum};
use crate::catalog::{Catalog, StdForm};
use flow_types::{Body, Type};
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Scalar {
Unit,
Bool,
Int,
Decimal,
Duration,
Instant,
}
#[derive(Clone, Debug)]
pub enum Form<'t> {
Scalar(Scalar),
Text,
Bytes,
Host,
List(&'t Type),
Set(&'t Type),
Dict(&'t Type, &'t Type),
Tuple(&'t [Type]),
Record(RecordForm<'t>),
Enum(EnumForm<'t>),
Newtype(Type, Option<StdForm>),
Task(&'t Type),
Never,
NoEncoding,
}
impl<'t> Form<'t> {
pub fn record(self) -> Option<RecordForm<'t>> {
if let Form::Record(record) = self {
Some(record)
} else {
None
}
}
pub fn enumeration(self) -> Option<EnumForm<'t>> {
if let Form::Enum(form) = self {
Some(form)
} else {
None
}
}
pub fn list(self) -> Option<&'t Type> {
if let Form::List(item) = self {
Some(item)
} else {
None
}
}
}
#[derive(Clone, Debug)]
pub struct RecordForm<'t> {
pub shape: &'t Arc<RecordShape>,
pub fields: Vec<Type>,
}
#[derive(Clone, Debug)]
pub struct EnumForm<'t> {
pub shape: &'t Arc<EnumShape>,
parts: Parts<'t>,
}
#[derive(Clone, Debug)]
enum Parts<'t> {
Declared {
ctors: &'t [Vec<Type>],
args: &'t [Type],
},
Option { item: &'t Type, some: usize },
Json(&'t StdEnum<JsonValueCtor>),
}
pub(crate) fn json_parts(ctor: JsonValueCtor) -> Vec<Type> {
match ctor {
JsonValueCtor::Null => Vec::new(),
JsonValueCtor::Bool => vec![Type::Bool],
JsonValueCtor::Number => vec![Type::Decimal],
JsonValueCtor::Text => vec![Type::Text],
JsonValueCtor::List => vec![Type::list(Type::Json)],
JsonValueCtor::Object => vec![Type::dict(Type::Text, Type::Json)],
}
}
impl<'t> EnumForm<'t> {
pub fn parts(&self, ctor: usize) -> Option<Vec<Cow<'t, Type>>> {
self.shape.constructor(ctor)?;
Some(match &self.parts {
Parts::Declared { ctors, args } => ctors
.get(ctor)?
.iter()
.map(|part| Cow::Owned(part.subst(args)))
.collect(),
Parts::Option { item, some } if ctor == *some => vec![Cow::Borrowed(*item)],
Parts::Option { .. } => Vec::new(),
Parts::Json(ctors) => ctors
.which(ctor)
.map(json_parts)
.unwrap_or_default()
.into_iter()
.map(Cow::Owned)
.collect(),
})
}
}
pub(crate) fn part_types<'a>(ty: &Cow<'a, Type>) -> Vec<Cow<'a, Type>> {
fn held(ty: &Type) -> Vec<&Type> {
match ty {
Type::List(item) | Type::Set(item) | Type::Option(item) => vec![&**item],
Type::Dict(key, item) => vec![&**key, &**item],
Type::Tuple(parts) => parts.iter().collect(),
Type::Unit
| Type::Bool
| Type::Int
| Type::Decimal
| Type::Text
| Type::Bytes
| Type::Duration
| Type::Instant
| Type::Json
| Type::Named(..)
| Type::Task(_)
| Type::Never
| Type::Param(_)
| Type::Function { .. }
| Type::NotData => Vec::new(),
}
}
match ty {
Cow::Borrowed(ty) => held(ty).into_iter().map(Cow::Borrowed).collect(),
Cow::Owned(ty) => held(ty).into_iter().cloned().map(Cow::Owned).collect(),
}
}
pub(crate) fn reborrow<'a>(ty: &Cow<'a, Type>) -> Cow<'a, Type> {
match ty {
Cow::Borrowed(ty) => Cow::Borrowed(ty),
Cow::Owned(ty) => Cow::Owned(ty.clone()),
}
}
pub fn form<'t>(catalog: &'t Catalog, ty: &'t Type) -> Form<'t> {
match ty {
Type::Unit => Form::Scalar(Scalar::Unit),
Type::Bool => Form::Scalar(Scalar::Bool),
Type::Int => Form::Scalar(Scalar::Int),
Type::Decimal => Form::Scalar(Scalar::Decimal),
Type::Duration => Form::Scalar(Scalar::Duration),
Type::Instant => Form::Scalar(Scalar::Instant),
Type::Text => Form::Text,
Type::Bytes => Form::Bytes,
Type::List(item) => Form::List(item),
Type::Set(item) => Form::Set(item),
Type::Dict(key, value) => Form::Dict(key, value),
Type::Tuple(parts) => Form::Tuple(parts),
Type::Option(item) => {
let option = catalog.option();
Form::Enum(EnumForm {
shape: &option.shape,
parts: Parts::Option {
item,
some: option.ctors.index(OptionCtor::Some),
},
})
}
Type::Json => {
let json = catalog.json();
Form::Enum(EnumForm {
shape: &json.shape,
parts: Parts::Json(&json.ctors),
})
}
Type::Named(decl, args) => match &catalog.decl(*decl).body {
Body::Record(shape, fields) => Form::Record(RecordForm {
shape,
fields: fields.iter().map(|field| field.subst(args)).collect(),
}),
Body::Enum(shape, ctors) => Form::Enum(EnumForm {
shape,
parts: Parts::Declared { ctors, args },
}),
Body::Newtype(base) => {
Form::Newtype(base.subst(args), StdForm::of(catalog.decl(*decl)))
}
Body::Host => Form::Host,
Body::NonData => Form::NoEncoding,
},
Type::Task(result) => Form::Task(result),
Type::Never => Form::Never,
Type::Param(_) | Type::Function { .. } | Type::NotData => Form::NoEncoding,
}
}