1
0
dust/src/abstract_tree/identifier.rs

48 lines
1.2 KiB
Rust
Raw Normal View History

2023-10-06 17:32:58 +00:00
use serde::{Deserialize, Serialize};
use tree_sitter::Node;
2023-11-30 01:59:58 +00:00
use crate::{AbstractTree, Error, Map, Result, Type, Value};
2023-10-06 17:32:58 +00:00
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Identifier(String);
impl Identifier {
pub fn new(inner: String) -> Self {
Identifier(inner)
}
pub fn take_inner(self) -> String {
self.0
}
pub fn inner(&self) -> &String {
&self.0
}
}
impl AbstractTree for Identifier {
2023-10-10 17:29:11 +00:00
fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
2023-11-15 02:24:47 +00:00
Error::expect_syntax_node(source, "identifier", node)?;
2023-10-22 18:33:08 +00:00
2023-10-06 17:32:58 +00:00
let identifier = &source[node.byte_range()];
Ok(Identifier(identifier.to_string()))
}
2023-11-30 00:23:42 +00:00
fn run(&self, _source: &str, context: &Map) -> Result<Value> {
2023-11-05 18:54:29 +00:00
if let Some(value) = context.variables()?.get(&self.0) {
2023-10-29 23:31:06 +00:00
Ok(value.clone())
2023-10-10 21:12:38 +00:00
} else {
2023-10-29 23:31:06 +00:00
Err(Error::VariableIdentifierNotFound(self.inner().clone()))
}
2023-10-06 17:32:58 +00:00
}
2023-11-30 00:23:42 +00:00
2023-11-30 01:59:58 +00:00
fn expected_type(&self, context: &Map) -> Result<Type> {
2023-11-30 00:23:42 +00:00
if let Some(value) = context.variables()?.get(&self.0) {
Ok(value.r#type(context)?)
} else {
2023-11-30 01:59:58 +00:00
Ok(Type::Empty)
2023-11-30 00:23:42 +00:00
}
}
2023-10-06 17:32:58 +00:00
}