2024-01-06 10:00:36 +00:00
|
|
|
use std::fmt::{self, Display, Formatter};
|
|
|
|
|
2023-10-06 17:32:58 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2024-01-31 18:51:48 +00:00
|
|
|
use crate::{
|
|
|
|
error::{RuntimeError, SyntaxError, ValidationError},
|
2024-02-11 00:31:47 +00:00
|
|
|
AbstractTree, Context, Format, SyntaxNode, Type, Value,
|
2024-01-31 18:51:48 +00:00
|
|
|
};
|
2023-10-06 17:32:58 +00:00
|
|
|
|
2023-12-06 18:48:38 +00:00
|
|
|
/// A string by which a variable is known to a context.
|
|
|
|
///
|
|
|
|
/// Every variable is a key-value pair. An identifier holds the key part of that
|
|
|
|
/// pair. Its inner value can be used to retrieve a Value instance from a Map.
|
2024-02-10 23:29:11 +00:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord, Hash)]
|
2023-10-06 17:32:58 +00:00
|
|
|
pub struct Identifier(String);
|
|
|
|
|
|
|
|
impl Identifier {
|
2024-01-06 13:11:09 +00:00
|
|
|
pub fn new<T: Into<String>>(inner: T) -> Self {
|
|
|
|
Identifier(inner.into())
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn take_inner(self) -> String {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn inner(&self) -> &String {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AbstractTree for Identifier {
|
2024-02-11 00:31:47 +00:00
|
|
|
fn from_syntax(
|
|
|
|
node: SyntaxNode,
|
|
|
|
source: &str,
|
|
|
|
_context: &Context,
|
|
|
|
) -> Result<Self, SyntaxError> {
|
2024-02-01 00:35:27 +00:00
|
|
|
SyntaxError::expect_syntax_node(source, "identifier", node)?;
|
2023-10-22 18:33:08 +00:00
|
|
|
|
2023-11-30 03:54:46 +00:00
|
|
|
let text = &source[node.byte_range()];
|
2023-10-06 17:32:58 +00:00
|
|
|
|
2023-12-31 14:47:20 +00:00
|
|
|
debug_assert!(!text.is_empty());
|
|
|
|
|
2023-11-30 03:54:46 +00:00
|
|
|
Ok(Identifier(text.to_string()))
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
|
|
|
|
2024-02-11 00:31:47 +00:00
|
|
|
fn validate(&self, _source: &str, _context: &Context) -> Result<(), ValidationError> {
|
2024-01-10 01:38:40 +00:00
|
|
|
Ok(())
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
2023-11-30 00:23:42 +00:00
|
|
|
|
2024-02-11 00:31:47 +00:00
|
|
|
fn expected_type(&self, context: &Context) -> Result<Type, ValidationError> {
|
|
|
|
if let Some(r#type) = context.get_type(&self.0)? {
|
|
|
|
Ok(r#type)
|
2023-11-30 00:23:42 +00:00
|
|
|
} else {
|
2024-02-01 00:07:18 +00:00
|
|
|
Err(ValidationError::VariableIdentifierNotFound(self.clone()))
|
2023-11-30 00:23:42 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-31 18:51:48 +00:00
|
|
|
|
2024-02-11 00:31:47 +00:00
|
|
|
fn run(&self, _source: &str, context: &Context) -> Result<Value, RuntimeError> {
|
|
|
|
if let Some(value) = context.get_value(&self.0)? {
|
2024-01-31 18:51:48 +00:00
|
|
|
Ok(value.clone())
|
|
|
|
} else {
|
2024-02-01 00:07:18 +00:00
|
|
|
Err(RuntimeError::VariableIdentifierNotFound(self.0.clone()))
|
2024-01-31 18:51:48 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
2024-01-06 10:00:36 +00:00
|
|
|
|
2024-01-06 13:11:09 +00:00
|
|
|
impl Format for Identifier {
|
2024-01-06 13:53:31 +00:00
|
|
|
fn format(&self, output: &mut String, _indent_level: u8) {
|
2024-01-06 13:11:09 +00:00
|
|
|
output.push_str(&self.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-06 10:00:36 +00:00
|
|
|
impl Display for Identifier {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.0)
|
|
|
|
}
|
|
|
|
}
|