1
0
dust/src/abstract_tree/index.rs

101 lines
3.4 KiB
Rust
Raw Normal View History

2023-10-29 23:31:06 +00:00
use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{AbstractTree, Error, IndexExpression, List, Map, Result, Type, Value};
2023-10-29 23:31:06 +00:00
2023-12-06 19:13:22 +00:00
/// Abstract representation of an index expression.
///
/// An index is a means of accessing values stored in list, maps and strings.
2023-10-29 23:31:06 +00:00
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Index {
pub collection: IndexExpression,
pub index: IndexExpression,
pub index_end: Option<IndexExpression>,
2023-10-29 23:31:06 +00:00
}
impl AbstractTree for Index {
2023-11-30 03:54:46 +00:00
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> {
2023-12-30 02:15:03 +00:00
Error::expect_syntax_node(source, "index", node)?;
2023-10-29 23:31:06 +00:00
let collection_node = node.child(0).unwrap();
let collection = IndexExpression::from_syntax_node(source, collection_node, context)?;
2023-10-29 23:31:06 +00:00
let index_node = node.child(2).unwrap();
let index = IndexExpression::from_syntax_node(source, index_node, context)?;
2023-10-29 23:31:06 +00:00
let index_end_node = node.child(4);
2023-10-29 23:31:06 +00:00
let index_end = if let Some(index_end_node) = index_end_node {
Some(IndexExpression::from_syntax_node(
2023-11-30 03:54:46 +00:00
source,
index_end_node,
context,
)?)
2023-10-29 23:31:06 +00:00
} else {
None
};
Ok(Index {
collection,
index,
index_end,
})
}
2023-11-30 00:23:42 +00:00
fn run(&self, source: &str, context: &Map) -> Result<Value> {
2023-11-15 01:00:57 +00:00
let collection = self.collection.run(source, context)?;
2023-10-29 23:31:06 +00:00
2023-11-15 01:00:57 +00:00
match collection {
2023-10-29 23:31:06 +00:00
Value::List(list) => {
let index = self.index.run(source, context)?.as_integer()? as usize;
let item = if let Some(index_end) = &self.index_end {
let index_end = index_end.run(source, context)?.as_integer()? as usize;
let sublist = list.items()[index..=index_end].to_vec();
Value::List(List::with_items(sublist))
} else {
list.items().get(index).cloned().unwrap_or_default()
};
Ok(item)
}
2023-11-15 01:41:57 +00:00
Value::Map(map) => {
let value = if let IndexExpression::Identifier(identifier) = &self.index {
2023-11-10 21:24:19 +00:00
let key = identifier.inner();
2023-12-09 22:15:41 +00:00
map.variables()?
.get(key)
.map(|(value, _)| value.clone())
.unwrap_or_default()
2023-11-10 21:24:19 +00:00
} else {
2023-11-15 01:00:57 +00:00
let value = self.index.run(source, context)?;
2023-11-10 21:24:19 +00:00
let key = value.as_string()?;
2023-12-09 22:15:41 +00:00
map.variables()?
.get(key)
.map(|(value, _)| value.clone())
.unwrap_or_default()
2023-11-10 21:24:19 +00:00
};
2023-10-29 23:31:06 +00:00
Ok(value)
2023-10-29 23:31:06 +00:00
}
Value::String(string) => {
let index = self.index.run(source, context)?.as_integer()? as usize;
let item = string.chars().nth(index).unwrap_or_default();
Ok(Value::String(item.to_string()))
}
2023-11-15 01:00:57 +00:00
_ => Err(Error::ExpectedCollection { actual: collection }),
2023-10-29 23:31:06 +00:00
}
}
2023-11-30 00:23:42 +00:00
2023-12-05 22:08:22 +00:00
fn expected_type(&self, context: &Map) -> Result<Type> {
match self.collection.expected_type(context)? {
Type::List(item_type) => Ok(*item_type.clone()),
Type::Map => Ok(Type::Any),
2023-12-26 22:19:12 +00:00
Type::None => Ok(Type::None),
2024-01-01 02:46:45 +00:00
r#type => Ok(r#type),
}
2023-11-30 00:23:42 +00:00
}
2023-10-29 23:31:06 +00:00
}