#[cfg(feature = "compiler")]
use crate::compiler::error::SpannedCompilerError;
#[cfg(feature = "parser")]
use crate::parser::errors::SpannedParserError;
use crate::{
core_compiler::core_compilation_context::DXBWithSharedValues,
prelude::*,
runtime::{
Runtime,
execution::{
ExecutionError,
context::{ExecutionContext, ScriptExecutionError},
},
},
traits::{
classification::Classification,
convert_parts::{FromParts, HasPartsKind, IntoParts, PartsKind},
},
values::value_container::{
ValueContainer,
borrowed_value_container::{
BorrowedValueContainer, BorrowedValueContainerMut,
},
},
};
#[derive(Debug, PartialEq, Eq)]
pub enum DeserializationError {
NoValue,
InvalidValue,
ExecutionError(Box<ExecutionError>),
CanNotReadFile(String),
#[cfg(feature = "parser")]
ParserError(SpannedParserError),
#[cfg(feature = "compiler")]
CompilerError(Box<SpannedCompilerError>),
NoStaticValueFound,
}
impl From<ExecutionError> for DeserializationError {
fn from(err: ExecutionError) -> DeserializationError {
DeserializationError::ExecutionError(Box::new(err))
}
}
#[cfg(feature = "parser")]
impl From<SpannedParserError> for DeserializationError {
fn from(err: SpannedParserError) -> DeserializationError {
DeserializationError::ParserError(err)
}
}
impl From<ScriptExecutionError> for DeserializationError {
fn from(err: ScriptExecutionError) -> DeserializationError {
match err {
ScriptExecutionError::ExecutionError(e) => {
DeserializationError::ExecutionError(e)
}
#[cfg(feature = "compiler")]
ScriptExecutionError::CompilerError(e) => {
DeserializationError::CompilerError(e)
}
}
}
}
pub trait ConvertValueContainer {
fn to_value_container(self) -> ValueContainer;
fn as_borrowed_value_container(&self) -> BorrowedValueContainer<'_>;
fn as_borrowed_value_container_mut(
&mut self,
) -> BorrowedValueContainerMut<'_>;
fn try_from_value_container(
value_container: ValueContainer,
) -> Result<Self, ValueContainer>
where
Self: Sized;
fn try_borrow_from_value_container(
value_container: &ValueContainer,
) -> Result<&Self, ()>
where
Self: Sized;
fn try_borrow_mut_from_value_container(
value_container: &mut ValueContainer,
) -> Result<&mut Self, ()>
where
Self: Sized;
fn try_from_bytes(
dxb: Vec<u8>,
runtime: &Runtime,
) -> Result<Self, DeserializationError>
where
Self: Sized + FromParts,
{
let value = runtime.execute_dxb_sync(
DXBWithSharedValues::new(dxb, vec![]),
None,
None::<ExecutionContext>,
)?;
if let Some(value) = value {
let config = Self::try_convert_from_value_container(value)
.map_err(|_| DeserializationError::InvalidValue)?;
Ok(config)
} else {
Err(DeserializationError::NoValue)
}
}
#[cfg(feature = "compiler")]
fn try_from_script(
script: &str,
runtime: &Runtime,
) -> Result<Self, DeserializationError>
where
Self: Sized + FromParts,
{
let value =
runtime.execute_sync(script, &[], None::<ExecutionContext>)?;
if let Some(value) = value {
let config = Self::try_convert_from_value_container(value)
.map_err(|_| DeserializationError::InvalidValue)?;
Ok(config)
} else {
Err(DeserializationError::NoValue)
}
}
#[cfg(all(feature = "std", feature = "compiler"))]
fn try_from_dx_file(
path: &std::path::Path,
runtime: &Runtime,
) -> Result<Self, DeserializationError>
where
Self: Sized + FromParts,
{
let script = std::fs::read_to_string(path)
.map_err(|e| DeserializationError::CanNotReadFile(e.to_string()))?;
Self::try_from_script(&script, runtime)
}
#[cfg(feature = "compiler")]
fn try_from_static_script(
script: &str,
) -> Result<Self, DeserializationError>
where
Self: Sized,
{
let value = crate::compiler::extract_static_value_from_script(script)?
.ok_or(DeserializationError::NoStaticValueFound)?;
Self::try_from_value_container(value)
.map_err(|_| DeserializationError::InvalidValue)
}
fn try_convert_from_value_container(
value_container: ValueContainer,
) -> Result<Self, ()>
where
Self: FromParts + Sized,
{
let val = Self::try_from_value_container(value_container);
match val {
Ok(value) => Ok(value),
Err(value_container) => {
let classification =
value_container.unresolved_classification();
let tag = classification.tag_str();
match value_container.parts_kind() {
PartsKind::Map => {
let map_parts =
Box::new(value_container).try_into_map_parts();
if let Ok(map) = map_parts {
Self::try_from_map_parts_with_tag(map, tag)
} else {
Err(())
}
}
PartsKind::List => {
let list_parts =
Box::new(value_container).try_into_list_parts();
if let Ok(list) = list_parts {
Self::try_from_list_parts_with_tag(list, tag)
} else {
Err(())
}
}
PartsKind::SingleValue => {
let single_value_parts =
Box::new(value_container).try_into_single_value();
if let Ok(single_value) = single_value_parts {
Self::try_from_single_value_with_tag(
single_value,
tag,
)
} else {
Err(())
}
}
PartsKind::None => Err(()),
}
}
}
}
}