dust/src/abstract_tree/identifier.rs

147 lines
3.7 KiB
Rust
Raw Normal View History

2024-02-15 20:20:29 +00:00
use std::{
fmt::{self, Display, Formatter},
sync::Arc,
};
2024-01-06 10:00:36 +00:00
2024-02-15 20:20:29 +00:00
use serde::{de::Visitor, Deserialize, Serialize};
2023-10-06 17:32:58 +00:00
2024-01-31 18:51:48 +00:00
use crate::{
2024-02-15 20:20:29 +00:00
built_in_identifiers::all_built_in_identifiers,
2024-01-31 18:51:48 +00:00
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
/// 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
2024-02-15 21:02:27 +00:00
/// pair. Its inner value can be used to retrieve a Value instance from a Map.
2024-02-15 20:20:29 +00:00
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct Identifier(Arc<String>);
2023-10-06 17:32:58 +00:00
impl Identifier {
2024-02-15 20:20:29 +00:00
pub fn new(key: &str) -> Self {
for built_in_identifier in all_built_in_identifiers() {
let identifier = built_in_identifier.get();
2023-10-06 17:32:58 +00:00
2024-02-15 20:20:29 +00:00
if &key == identifier.inner().as_ref() {
return identifier.clone();
}
}
Identifier(Arc::new(key.to_string()))
2023-10-06 17:32:58 +00:00
}
2024-02-15 21:30:47 +00:00
pub fn from_raw_parts(arc: Arc<String>) -> Self {
Identifier(arc)
}
2024-02-15 20:20:29 +00:00
pub fn inner(&self) -> &Arc<String> {
2023-10-06 17:32:58 +00:00
&self.0
}
2024-02-15 20:20:29 +00:00
pub fn contains(&self, string: &str) -> bool {
self.0.as_ref() == string
}
2023-10-06 17:32:58 +00:00
}
impl AbstractTree for Identifier {
2024-02-11 00:31:47 +00:00
fn from_syntax(
node: SyntaxNode,
source: &str,
_context: &Context,
) -> Result<Self, SyntaxError> {
SyntaxError::expect_syntax_node("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
debug_assert!(!text.is_empty());
2024-02-15 20:20:29 +00:00
Ok(Identifier::new(text))
2023-10-06 17:32:58 +00:00
}
2024-02-16 18:23:58 +00:00
fn validate(&self, _source: &str, context: &Context) -> Result<(), ValidationError> {
if let Some(_) = context.get_type(self)? {
Ok(())
} else {
Err(ValidationError::VariableIdentifierNotFound(self.clone()))
}
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> {
2024-02-15 20:20:29 +00:00
if let Some(r#type) = context.get_type(self)? {
2024-02-11 00:31:47 +00:00
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> {
2024-02-15 20:20:29 +00:00
if let Some(value) = context.get_value(self)? {
2024-01-31 18:51:48 +00:00
Ok(value.clone())
} else {
2024-02-16 20:37:07 +00:00
return Err(RuntimeError::ValidationFailure(
ValidationError::VariableIdentifierNotFound(self.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);
}
}
impl From<String> for Identifier {
fn from(value: String) -> Self {
Identifier::from_raw_parts(Arc::new(value))
}
}
impl From<&str> for Identifier {
fn from(value: &str) -> Self {
Identifier::new(value)
}
}
2024-01-06 10:00:36 +00:00
impl Display for Identifier {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
2024-02-15 20:20:29 +00:00
impl Serialize for Identifier {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self.0.as_ref())
}
}
impl<'de> Deserialize<'de> for Identifier {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_string(IdentifierVisitor)
}
}
struct IdentifierVisitor;
impl<'de> Visitor<'de> for IdentifierVisitor {
type Value = Identifier;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("valid UFT-8 sequence")
}
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(Identifier(Arc::new(v)))
}
}