use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::time::Duration;
use crate::commands::env_manager::EnvConfig;
#[derive(Debug, Clone, PartialEq)]
pub enum ExecutionMode {
Local,
Remote { server_url: String, token: String },
}
#[derive(Debug, Clone)]
pub struct ExecutionConfig {
pub mode: ExecutionMode,
pub timeout: Duration,
pub kernel_name: Option<String>,
pub allow_errors: bool,
pub notebook_path: Option<String>,
pub env_config: Option<EnvConfig>,
pub restart_kernel: bool,
}
impl Default for ExecutionConfig {
fn default() -> Self {
Self {
mode: ExecutionMode::Local,
timeout: Duration::from_secs(30),
kernel_name: None,
allow_errors: false,
notebook_path: None,
env_config: None,
restart_kernel: false,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionResult {
pub success: bool,
pub outputs: Vec<nbformat::v4::Output>,
pub execution_count: Option<i64>,
pub error: Option<ExecutionError>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionError {
pub ename: String,
pub evalue: String,
pub traceback: Vec<String>,
}
impl ExecutionResult {
pub fn success(outputs: Vec<nbformat::v4::Output>, execution_count: Option<i64>) -> Self {
Self {
success: true,
outputs,
execution_count,
error: None,
}
}
pub fn error(
outputs: Vec<nbformat::v4::Output>,
execution_count: Option<i64>,
error: ExecutionError,
) -> Self {
Self {
success: false,
outputs,
execution_count,
error: Some(error),
}
}
}
#[allow(dead_code)]
#[derive(Debug)]
pub enum MessageOutput {
Stream { name: String, text: String },
DisplayData {
data: serde_json::Value,
metadata: serde_json::Value,
},
ExecuteResult {
data: serde_json::Value,
metadata: serde_json::Value,
execution_count: i64,
},
Error {
ename: String,
evalue: String,
traceback: Vec<String>,
},
}
impl MessageOutput {
#[allow(dead_code)]
pub fn to_nbformat_output(&self) -> Result<nbformat::v4::Output> {
match self {
MessageOutput::Stream { name, text } => Ok(nbformat::v4::Output::Stream {
name: name.clone(),
text: nbformat::v4::MultilineString(text.clone()),
}),
MessageOutput::DisplayData { data, metadata } => {
let json = serde_json::json!({
"output_type": "display_data",
"data": data,
"metadata": metadata
});
Ok(serde_json::from_value(json)?)
}
MessageOutput::ExecuteResult {
data,
metadata,
execution_count,
} => {
let json = serde_json::json!({
"output_type": "execute_result",
"execution_count": execution_count,
"data": data,
"metadata": metadata
});
Ok(serde_json::from_value(json)?)
}
MessageOutput::Error {
ename,
evalue,
traceback,
} => Ok(nbformat::v4::Output::Error(nbformat::v4::ErrorOutput {
ename: ename.clone(),
evalue: evalue.clone(),
traceback: traceback.clone(),
})),
}
}
}