use crate::error::HedlResult;
use crate::value::{Reference, Value};
pub(super) fn convert_tensor_value(tv: Vec<crate::lex::TensorValue>) -> Vec<crate::lex::Tensor> {
tv.into_iter()
.map(|item| match item {
crate::lex::TensorValue::Number(n) => crate::lex::Tensor::Scalar(n),
crate::lex::TensorValue::Array(arr) => {
crate::lex::Tensor::Array(convert_tensor_value(arr))
}
})
.collect()
}
pub(super) fn convert_lex_value_to_value(lex_val: crate::lex::Value) -> HedlResult<Value> {
match lex_val {
crate::lex::Value::Null => Ok(Value::Null),
crate::lex::Value::Bool(b) => Ok(Value::Bool(b)),
crate::lex::Value::Int(i) => Ok(Value::Int(i)),
crate::lex::Value::Float(f) => Ok(Value::Float(f)),
crate::lex::Value::String(s) => Ok(Value::String(s.into_boxed_str())),
crate::lex::Value::Reference(r) => Ok(Value::Reference(Reference {
type_name: r.type_name.map(|t| t.into_boxed_str()),
id: r.id.into_boxed_str(),
})),
crate::lex::Value::Expression(e) => Ok(Value::Expression(Box::new(e))),
crate::lex::Value::Tensor(tv) => {
let tensors = convert_tensor_value(tv);
let tensor = if tensors.len() == 1 {
tensors.into_iter().next().expect("single-element vec")
} else {
crate::lex::Tensor::Array(tensors)
};
Ok(Value::Tensor(Box::new(tensor)))
}
crate::lex::Value::List(items) => {
let converted_items: Result<Vec<Value>, crate::error::HedlError> =
items.into_iter().map(convert_lex_value_to_value).collect();
Ok(Value::List(Box::new(converted_items?)))
}
}
}