jupyter_protocol/kernelspec.rs
1//! Defines structures for working with Jupyter kernel specifications.
2//!
3//! This module provides types for representing and working with Jupyter kernel
4//! specifications, which describe the properties and launch parameters for
5//! Jupyter kernels.
6//!
7//! The main struct in this module is `JupyterKernelspec`, which corresponds to
8//! the contents of a Jupyter JSON kernelspec file.
9//!
10//! # Examples
11//!
12//! ```rust
13//! use jupyter_protocol::JupyterKernelspec;
14//! use std::collections::HashMap;
15//!
16//! let kernelspec = JupyterKernelspec {
17//! argv: vec!["python3", "-m", "ipykernel_launcher", "-f", "{connection_file}"].into_iter().map(String::from).collect(),
18//! display_name: "Python 3".to_string(),
19//! language: "python".to_string(),
20//! metadata: None,
21//! interrupt_mode: Some("signal".to_string()),
22//! env: Some(HashMap::new()),
23//! };
24//! ```
25use std::collections::HashMap;
26
27use serde::{Deserialize, Serialize};
28use serde_json::Value;
29
30/// Represents the contents of a Jupyter JSON kernelspec file.
31///
32/// A kernelspec file defines the properties and launch parameters for a Jupyter kernel.
33/// This struct is used to serialize and deserialize kernelspec data.
34///
35/// # Examples
36///
37/// ```rust
38/// use jupyter_protocol::JupyterKernelspec;
39/// use std::collections::HashMap;
40///
41/// let kernelspec = JupyterKernelspec {
42/// argv: vec![
43/// "python3".to_string(),
44/// "-m".to_string(),
45/// "ipykernel_launcher".to_string(),
46/// "-f".to_string(),
47/// "{connection_file}".to_string()
48/// ],
49/// display_name: "Python 3".to_string(),
50/// language: "python".to_string(),
51/// metadata: None,
52/// interrupt_mode: Some("signal".to_string()),
53/// env: Some(HashMap::new()),
54/// };
55/// ```
56#[derive(Serialize, Deserialize, Clone, Debug)]
57pub struct JupyterKernelspec {
58 /// The command line arguments used to launch the kernel.
59 ///
60 /// This vector must contain `{connection_file}` as a placeholder, which will be
61 /// replaced by the actual connection file path when the client launches the kernel.
62 #[serde(default)]
63 pub argv: Vec<String>,
64 /// The human-readable name for the kernel.
65 ///
66 /// This name is typically displayed in the Jupyter interface when selecting a kernel.
67 pub display_name: String,
68 /// The programming language supported by the kernel.
69 ///
70 /// This should be a string identifier for the language, such as "python", "r", or "julia".
71 pub language: String,
72 /// Additional metadata associated with the kernel.
73 ///
74 /// This field can contain arbitrary key-value pairs for kernel-specific information.
75 /// The values can be of any JSON-compatible type.
76 pub metadata: Option<HashMap<String, Value>>,
77 /// Specifies how the kernel should be interrupted.
78 ///
79 /// Common values are "signal" (use SIGINT) or "message" (use kernel protocol).
80 /// If not specified, the client will use a default interrupt method.
81 pub interrupt_mode: Option<String>,
82 /// Environment variables to set for the kernel process.
83 ///
84 /// These key-value pairs will be added to the environment when launching the kernel.
85 pub env: Option<HashMap<String, String>>,
86}