use std::fmt::{Display, Formatter};
use purs_constructor::*;
#[derive(Clone, Debug, PartialEq)]
pub enum PursType {
Struct(PursConstructor, Vec<(String, PursConstructor)>),
TupleStruct(PursConstructor, Vec<PursConstructor>),
Enum(PursConstructor, Vec<PursConstructor>),
}
impl Display for PursType {
fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
use PursType::*;
match *self {
Struct(ref type_, ref fields) => {
write!(f, "data {} ", type_.name)?;
for param in &type_.parameters {
write!(f, "{} ", ¶m.name)?;
}
write!(f, "= {} {{ ", type_.name)?;
for (idx, &(ref name, ref constructor)) in fields.iter().enumerate() {
write!(f, "{} :: {}", name, constructor)?;
if idx < (fields.len() - 1) {
write!(f, ",")?;
}
write!(f, " ")?;
}
write!(f, "}}")
}
TupleStruct(ref type_, ref fields) => {
write!(f, "data {} ", type_.name)?;
for param in &type_.parameters {
write!(f, "{} ", ¶m.name)?;
}
write!(f, "= {}", type_.name)?;
for field in fields.iter() {
if field.parameters.is_empty() {
write!(f, " {}", field)?;
} else {
write!(f, " ({})", field)?;
}
}
Ok(())
}
Enum(ref type_, ref constructors) => {
write!(f, "data {} ", type_.name)?;
for param in &type_.parameters {
write!(f, "{} ", ¶m.name)?;
}
write!(f, "= ")?;
for (idx, constructor) in constructors.iter().enumerate() {
write!(f, "{}", constructor)?;
if idx < constructors.len() - 1 {
write!(f, " | ")?;
}
}
Ok(())
}
}
}
}
pub trait AsPursType: AsPursConstructor {
fn as_purs_type() -> PursType;
}