use crate::expression::TableData;
use super::{Environment, Expression, Int};
use std::{
collections::{BTreeMap, BTreeSet, HashMap},
rc::Rc,
};
impl From<u8> for Expression {
fn from(x: u8) -> Self {
Self::Integer(x as Int)
}
}
impl From<Int> for Expression {
fn from(x: Int) -> Self {
Self::Integer(x)
}
}
impl From<f64> for Expression {
fn from(x: f64) -> Self {
Self::Float(x)
}
}
impl From<&str> for Expression {
fn from(x: &str) -> Self {
Self::String(x.to_string())
}
}
impl From<String> for Expression {
fn from(x: String) -> Self {
Self::String(x)
}
}
impl From<bool> for Expression {
fn from(x: bool) -> Self {
Self::Boolean(x)
}
}
impl<T> From<HashMap<String, T>> for Expression
where
T: Into<Self>,
{
fn from(map: HashMap<String, T>) -> Self {
Self::HMap(Rc::new(
map.into_iter()
.map(|(name, item)| (name, item.into()))
.collect::<HashMap<String, Self>>(),
))
}
}
impl<T> From<BTreeMap<String, T>> for Expression
where
T: Into<Self>,
{
fn from(map: BTreeMap<String, T>) -> Self {
Self::Map(Rc::new(
map.into_iter()
.map(|(name, item)| (name, item.into()))
.collect::<BTreeMap<String, Self>>(),
))
}
}
impl<T> From<Vec<T>> for Expression
where
T: Into<Self>,
{
fn from(list: Vec<T>) -> Self {
Self::List(Rc::new(
list.into_iter()
.map(|item| item.into())
.collect::<Vec<Self>>(),
))
}
}
impl<T> From<BTreeSet<T>> for Expression
where
T: Into<Self>,
{
fn from(set: BTreeSet<T>) -> Self {
Self::BSet(Rc::new(
set.into_iter()
.map(|item| item.into())
.collect::<BTreeSet<Self>>(),
))
}
}
impl From<TableData> for Expression {
fn from(table: TableData) -> Self {
Self::Table(table)
}
}
impl From<Environment> for Expression {
fn from(env: Environment) -> Self {
Self::from(env.get_bindings_map())
}
}