2024-03-18 01:07:03 +00:00
|
|
|
use crate::{
|
|
|
|
context::Context,
|
|
|
|
error::{RuntimeError, ValidationError},
|
2024-04-22 05:51:34 +00:00
|
|
|
identifier::Identifier,
|
2024-03-18 01:07:03 +00:00
|
|
|
value::ValueInner,
|
|
|
|
};
|
|
|
|
|
2024-03-20 04:28:28 +00:00
|
|
|
use super::{AbstractNode, Action, Expression, Type, ValueNode, WithPosition};
|
2024-03-18 01:07:03 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
|
|
|
pub struct MapIndex {
|
2024-04-22 05:51:34 +00:00
|
|
|
collection: Expression,
|
|
|
|
index: WithPosition<Identifier>,
|
2024-03-18 01:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MapIndex {
|
2024-04-22 05:51:34 +00:00
|
|
|
pub fn new(left: Expression, right: WithPosition<Identifier>) -> Self {
|
|
|
|
Self {
|
|
|
|
collection: left,
|
|
|
|
index: right,
|
|
|
|
}
|
2024-03-18 01:07:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 04:28:28 +00:00
|
|
|
impl AbstractNode for MapIndex {
|
2024-03-19 22:31:52 +00:00
|
|
|
fn expected_type(&self, context: &Context) -> Result<Type, ValidationError> {
|
2024-04-22 05:51:34 +00:00
|
|
|
if let (Expression::Identifier(collection_identifier), index) =
|
|
|
|
(&self.collection, &self.index)
|
2024-03-18 01:07:03 +00:00
|
|
|
{
|
2024-03-25 04:16:55 +00:00
|
|
|
let collection =
|
2024-04-22 07:41:21 +00:00
|
|
|
if let Some(collection) = context.use_value(&collection_identifier.item)? {
|
2024-03-25 04:16:55 +00:00
|
|
|
collection
|
|
|
|
} else {
|
|
|
|
return Err(ValidationError::VariableNotFound {
|
2024-04-22 07:41:21 +00:00
|
|
|
identifier: collection_identifier.item.clone(),
|
2024-03-25 04:16:55 +00:00
|
|
|
position: collection_identifier.position,
|
|
|
|
});
|
|
|
|
};
|
2024-03-18 01:07:03 +00:00
|
|
|
|
|
|
|
if let ValueInner::Map(map) = collection.inner().as_ref() {
|
2024-04-22 07:41:21 +00:00
|
|
|
return if let Some(value) = map.get(&index.item) {
|
2024-03-19 22:31:52 +00:00
|
|
|
Ok(value.r#type(context)?)
|
2024-03-18 01:07:03 +00:00
|
|
|
} else {
|
2024-03-18 09:39:09 +00:00
|
|
|
Err(ValidationError::PropertyNotFound {
|
2024-04-22 07:41:21 +00:00
|
|
|
identifier: index.item.clone(),
|
2024-04-22 05:51:34 +00:00
|
|
|
position: index.position,
|
2024-03-18 09:39:09 +00:00
|
|
|
})
|
2024-03-18 01:07:03 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-03-25 04:16:55 +00:00
|
|
|
if let (
|
|
|
|
Expression::Value(WithPosition {
|
2024-04-22 07:41:21 +00:00
|
|
|
item: ValueNode::Map(properties),
|
2024-03-25 04:16:55 +00:00
|
|
|
..
|
|
|
|
}),
|
2024-04-22 05:51:34 +00:00
|
|
|
index,
|
|
|
|
) = (&self.collection, &self.index)
|
2024-03-18 01:07:03 +00:00
|
|
|
{
|
|
|
|
return if let Some(type_result) =
|
|
|
|
properties
|
|
|
|
.iter()
|
|
|
|
.find_map(|(property, type_option, expression)| {
|
2024-04-22 07:41:21 +00:00
|
|
|
if property == &index.item {
|
2024-03-18 01:07:03 +00:00
|
|
|
if let Some(r#type) = type_option {
|
2024-04-22 07:41:21 +00:00
|
|
|
Some(r#type.item.expected_type(context))
|
2024-03-18 01:07:03 +00:00
|
|
|
} else {
|
2024-03-25 04:16:55 +00:00
|
|
|
Some(expression.expected_type(context))
|
2024-03-18 01:07:03 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
{
|
|
|
|
type_result
|
|
|
|
} else {
|
|
|
|
Ok(Type::None)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-03-24 21:34:36 +00:00
|
|
|
if let (
|
2024-03-25 04:16:55 +00:00
|
|
|
Expression::Value(WithPosition {
|
2024-04-22 07:41:21 +00:00
|
|
|
item: ValueNode::Structure { fields, .. },
|
2024-03-25 04:16:55 +00:00
|
|
|
..
|
|
|
|
}),
|
2024-04-22 05:51:34 +00:00
|
|
|
index,
|
|
|
|
) = (&self.collection, &self.index)
|
2024-03-24 21:34:36 +00:00
|
|
|
{
|
|
|
|
return if let Some(type_result) = fields.iter().find_map(|(property, expression)| {
|
2024-04-22 07:41:21 +00:00
|
|
|
if property == &index.item {
|
2024-03-25 04:16:55 +00:00
|
|
|
Some(expression.expected_type(context))
|
2024-03-24 21:34:36 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}) {
|
|
|
|
type_result
|
|
|
|
} else {
|
|
|
|
Ok(Type::None)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-04-22 05:51:34 +00:00
|
|
|
Err(ValidationError::CannotIndex {
|
|
|
|
r#type: self.collection.expected_type(context)?,
|
|
|
|
position: self.collection.position(),
|
2024-03-18 01:07:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn validate(&self, context: &Context) -> Result<(), ValidationError> {
|
2024-04-22 05:51:34 +00:00
|
|
|
self.collection.validate(context)
|
2024-03-18 01:07:03 +00:00
|
|
|
}
|
|
|
|
|
2024-04-22 05:51:34 +00:00
|
|
|
fn run(self, context: &mut Context, _clear_variables: bool) -> Result<Action, RuntimeError> {
|
|
|
|
let collection_position = self.collection.position();
|
|
|
|
let action = self.collection.run(context, _clear_variables)?;
|
2024-03-18 09:39:09 +00:00
|
|
|
let collection = if let Action::Return(value) = action {
|
|
|
|
value
|
|
|
|
} else {
|
|
|
|
return Err(RuntimeError::ValidationFailure(
|
2024-03-25 04:16:55 +00:00
|
|
|
ValidationError::InterpreterExpectedReturn(collection_position),
|
2024-03-18 09:39:09 +00:00
|
|
|
));
|
|
|
|
};
|
2024-03-18 01:07:03 +00:00
|
|
|
|
2024-04-22 05:51:34 +00:00
|
|
|
if let ValueInner::Map(map) = collection.inner().as_ref() {
|
2024-03-18 01:07:03 +00:00
|
|
|
let action = map
|
2024-04-22 07:41:21 +00:00
|
|
|
.get(&self.index.item)
|
2024-03-18 01:07:03 +00:00
|
|
|
.map(|value| Action::Return(value.clone()))
|
|
|
|
.unwrap_or(Action::None);
|
|
|
|
|
|
|
|
Ok(action)
|
|
|
|
} else {
|
|
|
|
Err(RuntimeError::ValidationFailure(
|
2024-04-22 05:51:34 +00:00
|
|
|
ValidationError::CannotIndex {
|
|
|
|
r#type: collection.r#type(context)?,
|
|
|
|
position: collection_position,
|
2024-03-18 01:07:03 +00:00
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|