use std::collections::HashMap;
use std::convert::TryFrom;
use std::convert::TryInto;
use bounded_vec::BoundedVec;
use bounded_vec::BoundedVecOutOfBounds;
use super::stype::SType;
use super::stype_param::STypeVar;
pub type TupleItems<T> = BoundedVec<T, 2, 255>;
impl TryFrom<Vec<SType>> for STuple {
type Error = BoundedVecOutOfBounds;
fn try_from(value: Vec<SType>) -> Result<Self, Self::Error> {
Ok(STuple {
items: value.try_into()?,
})
}
}
#[derive(PartialEq, Eq, Clone)]
pub struct STuple {
pub items: TupleItems<SType>,
}
impl std::fmt::Debug for STuple {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.items.clone().to_vec().fmt(f)
}
}
impl std::fmt::Display for STuple {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "(")?;
for (i, item) in self.items.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
item.fmt(f)?;
}
write!(f, ")")
}
}
impl STuple {
pub fn pair(t1: SType, t2: SType) -> Self {
STuple {
items: [t1, t2].into(),
}
}
pub fn triple(t1: SType, t2: SType, t3: SType) -> Self {
#[allow(clippy::unwrap_used)]
STuple {
items: vec![t1, t2, t3].try_into().unwrap(),
}
}
pub fn quadruple(t1: SType, t2: SType, t3: SType, t4: SType) -> Self {
#[allow(clippy::unwrap_used)]
STuple {
items: vec![t1, t2, t3, t4].try_into().unwrap(),
}
}
pub(crate) fn with_subst(&self, subst: &HashMap<STypeVar, SType>) -> Self {
STuple {
items: self.items.clone().mapped(|a| a.with_subst(subst)),
}
}
}