Skip to content

Commit

Permalink
Use builder pattern for identities
Browse files Browse the repository at this point in the history
  • Loading branch information
zph committed Apr 28, 2024
1 parent 619924d commit 018835c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
3 changes: 2 additions & 1 deletion cli/ops/jupyter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ pub async fn op_jupyter_input(
}

last_request
.new_message_with_identities("input_request")
.new_message("input_request")
.with_identities(&last_request)
.with_content(json!({
"prompt": prompt,
"password": is_password,
Expand Down
30 changes: 8 additions & 22 deletions cli/tools/jupyter/jupyter_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,28 +206,6 @@ impl JupyterMessage {
}
}

// TODO: extend ::new_message to optionally clone zmq identities?
// Creates a new child message of this message. ZMQ identities are transferred.
// Necessary for `input_request` to match the zmq identities or the frontend will
// not receive the message.
pub(crate) fn new_message_with_identities(&self, msg_type: &str) -> JupyterMessage {
let mut header = self.header.clone();
header["msg_type"] = serde_json::Value::String(msg_type.to_owned());
header["username"] = serde_json::Value::String("kernel".to_owned());
header["msg_id"] = serde_json::Value::String(Uuid::new_v4().to_string());
header["date"] = serde_json::Value::String(utc_now().to_rfc3339());


JupyterMessage {
zmq_identities: self.zmq_identities.clone(),
header,
parent_header: self.header.clone(),
metadata: json!({}),
content: json!({}),
buffers: vec![],
}
}

// Creates a reply to this message. This is a child with the message type determined
// automatically by replacing "request" with "reply". ZMQ identities are transferred.
pub(crate) fn new_reply(&self) -> JupyterMessage {
Expand Down Expand Up @@ -265,6 +243,14 @@ impl JupyterMessage {
self
}

pub(crate) fn with_identities(
mut self,
msg: &JupyterMessage,
) -> JupyterMessage {
self.zmq_identities = msg.zmq_identities.clone();
self
}

pub(crate) async fn send<S: zeromq::SocketSend>(
&self,
connection: &mut Connection<S>,
Expand Down

0 comments on commit 018835c

Please sign in to comment.