datex-core 0.0.14

The DATEX Core Rust implementation
Documentation
#[cfg(feature = "compiler")]
use crate::compiler::scope::CompilationScope;
use crate::runtime::{
    Runtime,
    execution::{
        ExecutionOptions, context::ExecutionContext,
        execution_loop::state::ExecutionLoopState,
    },
};

use crate::runtime::execution::execution_input::ExecutionCallerMetadata;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ExecutionMode {
    /// A single execution of a program that is completely known at compile time.
    #[default]
    Static,
    /// An execution of a program that may be extended at runtime.
    /// This mode is used for REPLs and dynamic remote executions with persistent contexts.
    Unbounded { has_next: bool },
}

impl ExecutionMode {
    pub fn unbounded() -> Self {
        ExecutionMode::Unbounded { has_next: true }
    }

    pub fn is_unbounded(&self) -> bool {
        matches!(self, ExecutionMode::Unbounded { .. })
    }
}

#[derive(Debug)]
pub struct LocalExecutionContext {
    #[cfg(feature = "compiler")]
    pub compile_scope: CompilationScope,
    pub loop_state: Option<ExecutionLoopState>,
    pub runtime: Runtime,
    pub execution_options: ExecutionOptions,
    pub verbose: bool,
    pub execution_mode: ExecutionMode,
    pub caller_metadata: ExecutionCallerMetadata,
}

impl LocalExecutionContext {
    pub fn new(
        execution_mode: ExecutionMode,
        runtime: Runtime,
        caller_metadata: ExecutionCallerMetadata,
    ) -> Self {
        LocalExecutionContext {
            #[cfg(feature = "compiler")]
            compile_scope: CompilationScope::new(execution_mode),
            loop_state: None,
            runtime,
            execution_options: ExecutionOptions::default(),
            verbose: false,
            execution_mode,
            caller_metadata,
        }
    }

    /// Creates a new local execution context with the given compile scope.
    pub fn debug(
        execution_mode: ExecutionMode,
        runtime: Runtime,
        caller_metadata: ExecutionCallerMetadata,
    ) -> Self {
        LocalExecutionContext {
            #[cfg(feature = "compiler")]
            compile_scope: CompilationScope::new(execution_mode),
            execution_options: ExecutionOptions { verbose: true },
            loop_state: None,
            runtime,
            verbose: true,
            execution_mode,
            caller_metadata,
        }
    }

    pub fn set_runtime_internal(&mut self, runtime_internal: Runtime) {
        self.runtime = runtime_internal;
    }
}

impl ExecutionContext {
    pub fn local(
        execution_mode: ExecutionMode,
        runtime: Runtime,
        caller_metadata: ExecutionCallerMetadata,
    ) -> Self {
        ExecutionContext::Local(LocalExecutionContext::new(
            execution_mode,
            runtime,
            caller_metadata,
        ))
    }

    /// Creates a new local static execution context (can only be used once).
    pub fn local_static(
        runtime: Runtime,
        caller_metadata: ExecutionCallerMetadata,
    ) -> Self {
        ExecutionContext::Local(LocalExecutionContext::new(
            ExecutionMode::Static,
            runtime,
            caller_metadata,
        ))
    }

    /// Creates a new local execution context (can be used multiple times).
    pub fn local_unbounded(
        runtime: Runtime,
        caller_metadata: ExecutionCallerMetadata,
    ) -> Self {
        ExecutionContext::Local(LocalExecutionContext::new(
            ExecutionMode::Unbounded { has_next: true },
            runtime,
            caller_metadata,
        ))
    }

    /// Creates a new local execution context with verbose mode enabled,
    /// providing more log outputs for debugging purposes.
    pub fn local_debug(
        execution_mode: ExecutionMode,
        runtime: Runtime,
        caller_metadata: ExecutionCallerMetadata,
    ) -> Self {
        ExecutionContext::Local(LocalExecutionContext::debug(
            execution_mode,
            runtime,
            caller_metadata,
        ))
    }
}