kernel_sidecar/jupyter/shell_content/
execute.rs

1/*
2
3Ref: https://jupyter-client.readthedocs.io/en/latest/messaging.html#execute
4*/
5
6use std::collections::HashMap;
7
8use crate::jupyter::header::Header;
9use crate::jupyter::message::Message;
10use crate::jupyter::request::Request;
11use bytes::Bytes;
12use serde::{Deserialize, Serialize};
13
14#[derive(Serialize, Deserialize, Debug, Clone)]
15pub struct ExecuteRequest {
16    code: String,
17    silent: bool,
18    store_history: bool,
19    user_expressions: HashMap<String, String>,
20    allow_stdin: bool,
21    stop_on_error: bool,
22}
23
24impl ExecuteRequest {
25    pub fn new(code: String) -> Self {
26        ExecuteRequest {
27            code,
28            silent: false,
29            store_history: true,
30            user_expressions: HashMap::new(),
31            allow_stdin: true,
32            stop_on_error: true,
33        }
34    }
35}
36
37impl From<ExecuteRequest> for Request {
38    fn from(req: ExecuteRequest) -> Self {
39        let msg = Message {
40            header: Header::new("execute_request".to_owned()),
41            parent_header: None,
42            metadata: None,
43            content: req,
44        };
45        Request::Execute(msg)
46    }
47}
48
49#[allow(dead_code)]
50#[derive(Deserialize, Debug)]
51pub struct ExecuteReply {
52    status: String,
53    execution_count: u32,
54}
55
56impl From<Bytes> for ExecuteReply {
57    fn from(bytes: Bytes) -> Self {
58        serde_json::from_slice(&bytes).expect("Failed to deserialize ExecuteReply")
59    }
60}