pub mod serde_compat;
pub mod shared;
#[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,
cache::shared_references_cache::SharedReferencesCache,
execution::{ExecutionError, context::ScriptExecutionError},
},
shared_values::errors::KeyNotFoundError,
types::r#type::Type,
values::{value::Value, value_container::ValueContainer},
};
#[cfg(feature = "decompiler")]
use crate::decompiler::{DecompileOptions, decompile_value};
#[derive(Debug, Clone)]
pub struct TryFromDatexValueError(pub String);
#[derive(Debug, Clone)]
pub struct TryToDatexValueError(pub String);
#[derive(Debug)]
pub enum DeserializationError {
NoValue,
InvalidValue(TryFromDatexValueError),
ExecutionError(ExecutionError),
CanNotReadFile(String),
#[cfg(feature = "parser")]
ParserError(SpannedParserError),
#[cfg(feature = "compiler")]
CompilerError(SpannedCompilerError),
NoStaticValueFound,
}
impl From<ExecutionError> for DeserializationError {
fn from(err: ExecutionError) -> DeserializationError {
DeserializationError::ExecutionError(err)
}
}
#[cfg(feature = "parser")]
impl From<SpannedParserError> for DeserializationError {
fn from(err: SpannedParserError) -> DeserializationError {
DeserializationError::ParserError(err)
}
}
impl From<TryFromDatexValueError> for DeserializationError {
fn from(err: TryFromDatexValueError) -> DeserializationError {
DeserializationError::InvalidValue(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 DatexValueContainerProxy:
Sized
+ DatexValueContainerProxyDeserialize
+ DatexValueContainerProxySerialize
+ DatexProxyTypes
{
}
pub trait DatexValueProxy:
Sized + DatexValueProxyDeserialize + DatexValueProxySerialize + DatexProxyTypes
{
}
pub trait DatexProxyTypes {
fn datex_type(memory: &mut SharedReferencesCache) -> Type;
}
pub trait DatexValueContainerProxyDeserialize: Sized {
fn try_from_value_container(
value: ValueContainer,
) -> Result<Self, TryFromDatexValueError>;
fn try_from_map_property(
value: Result<ValueContainer, KeyNotFoundError>,
) -> Result<Self, TryFromDatexValueError> {
let value =
value.map_err(|err| TryFromDatexValueError(err.to_string()))?;
Self::try_from_value_container(value)
}
fn try_from_bytes(
dxb: Vec<u8>,
runtime: &Runtime,
) -> Result<Self, DeserializationError> {
let value = runtime.execute_dxb_sync(
DXBWithSharedValues::new(dxb, vec![]),
None,
true,
)?;
if let Some(value) = value {
let config = Self::try_from_value_container(value)?;
Ok(config)
} else {
Err(DeserializationError::NoValue)
}
}
#[cfg(feature = "compiler")]
fn try_from_script(
script: &str,
runtime: &Runtime,
) -> Result<Self, DeserializationError> {
let value = runtime.execute_sync(script, &[], None)?;
if let Some(value) = value {
let config = Self::try_from_value_container(value)?;
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> {
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> {
let value = crate::compiler::extract_static_value_from_script(script)?
.ok_or(DeserializationError::NoStaticValueFound)?;
Ok(Self::try_from_value_container(value)?)
}
}
pub trait DatexValueProxyDeserialize: Sized {
fn try_from_value(value: Value) -> Result<Self, TryFromDatexValueError>;
fn try_from_map_property(
value: Result<Value, KeyNotFoundError>,
) -> Result<Self, TryFromDatexValueError> {
let value =
value.map_err(|err| TryFromDatexValueError(err.to_string()))?;
Self::try_from_value(value)
}
}
pub trait DatexValueContainerProxySerialize {
fn try_to_value_container(
self,
) -> Result<ValueContainer, TryToDatexValueError>;
#[cfg(feature = "decompiler")]
fn try_to_datex_string(
self,
decompile_options: DecompileOptions,
) -> Result<String, TryToDatexValueError>
where
Self: Sized,
{
Ok(decompile_value(
&self.try_to_value_container()?,
decompile_options,
))
}
}
pub trait DatexValueProxySerialize {
fn try_to_value(self) -> Result<Value, TryToDatexValueError>;
}
pub trait DatexValueContainerProxyInfallibleSerialize {
fn to_value_container(self) -> ValueContainer;
#[cfg(feature = "decompiler")]
fn to_datex_string(self, decompile_options: DecompileOptions) -> String
where
Self: Sized,
{
decompile_value(&self.to_value_container(), decompile_options)
}
}
pub trait DatexValueProxyInfallibleSerialize {
fn to_value(self) -> Value;
}
impl<T: DatexValueProxyDeserialize> DatexValueContainerProxyDeserialize for T {
fn try_from_value_container(
value: ValueContainer,
) -> Result<Self, TryFromDatexValueError> {
match value {
ValueContainer::Local(val) => DatexValueProxyDeserialize::try_from_value(val),
_ => Err(TryFromDatexValueError("Cannot cast from ValueContainer::Shared, expected ValueContainer::Local".to_string())),
}
}
fn try_from_map_property(
value: Result<ValueContainer, KeyNotFoundError>,
) -> Result<Self, TryFromDatexValueError> {
match value {
Ok(ValueContainer::Local(value)) => {
DatexValueProxyDeserialize::try_from_map_property(Ok(value))
}
Ok(_) => Err(TryFromDatexValueError("Cannot cast from ValueContainer::Shared, expected ValueContainer::Local".to_string())),
Err(err) => {
DatexValueProxyDeserialize::try_from_map_property(Err(err))
}
}
}
}
impl<T: DatexValueProxySerialize> DatexValueContainerProxySerialize for T {
fn try_to_value_container(
self,
) -> Result<ValueContainer, TryToDatexValueError> {
DatexValueProxySerialize::try_to_value(self).map(ValueContainer::from)
}
}
impl<T: DatexValueProxyInfallibleSerialize>
DatexValueContainerProxyInfallibleSerialize for T
{
fn to_value_container(self) -> ValueContainer {
ValueContainer::from(DatexValueProxyInfallibleSerialize::to_value(self))
}
}
impl<T: DatexValueProxy> DatexValueContainerProxy for T {}