2024-06-04 18:47:15 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2024-06-22 03:37:25 +00:00
|
|
|
use crate::{
|
|
|
|
context::Context,
|
|
|
|
error::{RuntimeError, ValidationError},
|
|
|
|
identifier::Identifier,
|
|
|
|
};
|
2024-03-19 21:49:24 +00:00
|
|
|
|
2024-06-22 00:59:38 +00:00
|
|
|
use super::{AbstractNode, Evaluation, Type, TypeConstructor};
|
2024-03-19 21:49:24 +00:00
|
|
|
|
2024-06-04 18:47:15 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
|
2024-03-19 21:49:24 +00:00
|
|
|
pub struct StructureDefinition {
|
|
|
|
name: Identifier,
|
2024-06-17 14:50:06 +00:00
|
|
|
fields: Vec<(Identifier, TypeConstructor)>,
|
2024-03-19 21:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl StructureDefinition {
|
2024-06-17 14:50:06 +00:00
|
|
|
pub fn new(name: Identifier, fields: Vec<(Identifier, TypeConstructor)>) -> Self {
|
2024-03-19 21:49:24 +00:00
|
|
|
Self { name, fields }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-22 00:59:38 +00:00
|
|
|
impl AbstractNode for StructureDefinition {
|
2024-06-22 03:37:25 +00:00
|
|
|
fn define_types(&self, context: &Context) -> Result<(), ValidationError> {
|
|
|
|
let mut fields = Vec::with_capacity(self.fields.len());
|
|
|
|
|
|
|
|
for (identifier, constructor) in self.fields {
|
|
|
|
let r#type = constructor.construct(&context)?;
|
|
|
|
|
|
|
|
fields.push((identifier, r#type));
|
|
|
|
}
|
|
|
|
|
|
|
|
let struct_type = Type::Structure {
|
|
|
|
name: self.name.clone(),
|
|
|
|
fields,
|
|
|
|
};
|
|
|
|
|
|
|
|
context.set_type(self.name, struct_type)?;
|
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn validate(&self, context: &Context, manage_memory: bool) -> Result<(), ValidationError> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-06-22 00:59:38 +00:00
|
|
|
fn evaluate(
|
2024-06-17 14:10:06 +00:00
|
|
|
self,
|
2024-06-22 03:37:25 +00:00
|
|
|
context: &Context,
|
2024-06-17 14:10:06 +00:00
|
|
|
_manage_memory: bool,
|
2024-06-21 22:28:12 +00:00
|
|
|
) -> Result<Option<Evaluation>, RuntimeError> {
|
2024-06-17 14:10:06 +00:00
|
|
|
let mut fields = Vec::with_capacity(self.fields.len());
|
|
|
|
|
|
|
|
for (identifier, constructor) in self.fields {
|
2024-06-17 14:50:06 +00:00
|
|
|
let r#type = constructor.construct(&context)?;
|
2024-06-17 14:10:06 +00:00
|
|
|
|
|
|
|
fields.push((identifier, r#type));
|
|
|
|
}
|
|
|
|
|
2024-03-19 21:49:24 +00:00
|
|
|
let struct_type = Type::Structure {
|
|
|
|
name: self.name.clone(),
|
2024-06-17 14:10:06 +00:00
|
|
|
fields,
|
2024-03-19 21:49:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
context.set_type(self.name, struct_type)?;
|
|
|
|
|
2024-06-21 22:28:12 +00:00
|
|
|
Ok(None)
|
2024-03-19 21:49:24 +00:00
|
|
|
}
|
2024-06-22 00:59:38 +00:00
|
|
|
|
2024-06-22 03:37:25 +00:00
|
|
|
fn expected_type(&self, context: &Context) -> Result<Option<Type>, ValidationError> {
|
2024-06-22 00:59:38 +00:00
|
|
|
Ok(None)
|
|
|
|
}
|
2024-03-19 21:49:24 +00:00
|
|
|
}
|