kernel_sidecar/jupyter/
header.rs

1/*
2Used building Message<T>, see messge.rs.
3
4Ref: https://jupyter-client.readthedocs.io/en/latest/messaging.html#message-header
5*/
6use bytes::Bytes;
7use chrono::{DateTime, Utc};
8use serde::{Deserialize, Serialize};
9
10#[derive(Serialize, Deserialize, Debug, Clone)]
11pub struct Header {
12    pub msg_id: String,
13    session: String,
14    username: String,
15    date: DateTime<Utc>,
16    pub msg_type: String,
17    version: String,
18}
19
20impl Header {
21    pub fn new(msg_type: String) -> Self {
22        Header {
23            msg_id: uuid::Uuid::new_v4().to_string(),
24            session: uuid::Uuid::new_v4().to_string(),
25            username: "kernel_sidecar".to_string(),
26            date: Utc::now(),
27            msg_type,
28            version: "5.3".to_string(),
29        }
30    }
31}
32
33impl From<Bytes> for Header {
34    fn from(bytes: Bytes) -> Self {
35        match serde_json::from_slice::<Header>(&bytes) {
36            Ok(header) => header,
37            Err(e) => {
38                // Print the error and the bytes
39                eprintln!("Failed to deserialize ZMQ bytes to Header: {}", e);
40                eprintln!("Bytes: {:?}", bytes);
41                // You can then decide to panic or handle the error differently
42                panic!("Deserialization error");
43            }
44        }
45    }
46}