mod.rs

  1mod native_kernel;
  2use std::{fmt::Debug, future::Future, path::PathBuf};
  3
  4use futures::{channel::mpsc, future::Shared};
  5use gpui::{App, Entity, Task, Window};
  6use language::LanguageName;
  7use log;
  8pub use native_kernel::*;
  9
 10mod remote_kernels;
 11use project::{Project, ProjectPath, Toolchains, WorktreeId};
 12use remote::RemoteConnectionOptions;
 13pub use remote_kernels::*;
 14
 15mod ssh_kernel;
 16pub use ssh_kernel::*;
 17
 18mod wsl_kernel;
 19pub use wsl_kernel::*;
 20
 21use std::collections::HashMap;
 22
 23use anyhow::Result;
 24use futures::{FutureExt, StreamExt};
 25use gpui::{AppContext, AsyncWindowContext, Context};
 26use jupyter_protocol::{JupyterKernelspec, JupyterMessageContent};
 27use runtimelib::{
 28    ClientControlConnection, ClientIoPubConnection, ClientShellConnection, ClientStdinConnection,
 29    ExecutionState, JupyterMessage, KernelInfoReply,
 30};
 31use ui::{Icon, IconName, SharedString};
 32use util::rel_path::RelPath;
 33
 34pub fn start_kernel_tasks<S: KernelSession + 'static>(
 35    session: Entity<S>,
 36    iopub_socket: ClientIoPubConnection,
 37    shell_socket: ClientShellConnection,
 38    control_socket: ClientControlConnection,
 39    stdin_socket: ClientStdinConnection,
 40    cx: &mut AsyncWindowContext,
 41) -> (
 42    futures::channel::mpsc::Sender<JupyterMessage>,
 43    futures::channel::mpsc::Sender<JupyterMessage>,
 44) {
 45    let (mut shell_send, shell_recv) = shell_socket.split();
 46    let (mut control_send, control_recv) = control_socket.split();
 47    let (mut stdin_send, stdin_recv) = stdin_socket.split();
 48
 49    let (request_tx, mut request_rx) = futures::channel::mpsc::channel::<JupyterMessage>(100);
 50    let (stdin_tx, mut stdin_rx) = futures::channel::mpsc::channel::<JupyterMessage>(100);
 51
 52    let recv_task = cx.spawn({
 53        let session = session.clone();
 54        let mut iopub = iopub_socket;
 55        let mut shell = shell_recv;
 56        let mut control = control_recv;
 57        let mut stdin = stdin_recv;
 58
 59        async move |cx| -> anyhow::Result<()> {
 60            loop {
 61                let (channel, result) = futures::select! {
 62                    msg = iopub.read().fuse() => ("iopub", msg),
 63                    msg = shell.read().fuse() => ("shell", msg),
 64                    msg = control.read().fuse() => ("control", msg),
 65                    msg = stdin.read().fuse() => ("stdin", msg),
 66                };
 67                match result {
 68                    Ok(message) => {
 69                        session
 70                            .update_in(cx, |session, window, cx| {
 71                                session.route(&message, window, cx);
 72                            })
 73                            .ok();
 74                    }
 75                    Err(
 76                        ref err @ (runtimelib::RuntimeError::ParseError { .. }
 77                        | runtimelib::RuntimeError::SerdeError(_)),
 78                    ) => {
 79                        let error_detail = format!("Kernel issue on {channel} channel\n\n{err}");
 80                        log::warn!("kernel: {error_detail}");
 81                        session
 82                            .update_in(cx, |session, _window, cx| {
 83                                session.kernel_errored(error_detail, cx);
 84                                cx.notify();
 85                            })
 86                            .ok();
 87                    }
 88                    Err(err) => {
 89                        log::warn!("kernel: error reading from {channel}: {err:?}");
 90                        anyhow::bail!("{channel} recv: {err}");
 91                    }
 92                }
 93            }
 94        }
 95    });
 96
 97    let routing_task = cx.background_spawn(async move {
 98        while let Some(message) = request_rx.next().await {
 99            match message.content {
100                JupyterMessageContent::DebugRequest(_)
101                | JupyterMessageContent::InterruptRequest(_)
102                | JupyterMessageContent::ShutdownRequest(_) => {
103                    control_send.send(message).await?;
104                }
105                _ => {
106                    shell_send.send(message).await?;
107                }
108            }
109        }
110        anyhow::Ok(())
111    });
112
113    let stdin_routing_task = cx.background_spawn(async move {
114        while let Some(message) = stdin_rx.next().await {
115            stdin_send.send(message).await?;
116        }
117        anyhow::Ok(())
118    });
119
120    cx.spawn({
121        async move |cx| {
122            async fn with_name(
123                name: &'static str,
124                task: Task<Result<()>>,
125            ) -> (&'static str, Result<()>) {
126                (name, task.await)
127            }
128
129            let mut tasks = futures::stream::FuturesUnordered::new();
130            tasks.push(with_name("recv task", recv_task));
131            tasks.push(with_name("routing task", routing_task));
132            tasks.push(with_name("stdin routing task", stdin_routing_task));
133
134            while let Some((name, result)) = tasks.next().await {
135                if let Err(err) = result {
136                    session.update(cx, |session, cx| {
137                        session.kernel_errored(format!("handling failed for {name}: {err}"), cx);
138                        cx.notify();
139                    });
140                }
141            }
142        }
143    })
144    .detach();
145
146    (request_tx, stdin_tx)
147}
148
149pub trait KernelSession: Sized {
150    fn route(&mut self, message: &JupyterMessage, window: &mut Window, cx: &mut Context<Self>);
151    fn kernel_errored(&mut self, error_message: String, cx: &mut Context<Self>);
152}
153
154#[derive(Debug, Clone)]
155pub struct PythonEnvKernelSpecification {
156    pub name: String,
157    pub path: PathBuf,
158    pub kernelspec: JupyterKernelspec,
159    pub has_ipykernel: bool,
160    /// Display label for the environment type: "venv", "Conda", "Pyenv", etc.
161    pub environment_kind: Option<String>,
162}
163
164impl PartialEq for PythonEnvKernelSpecification {
165    fn eq(&self, other: &Self) -> bool {
166        self.name == other.name && self.path == other.path
167    }
168}
169
170impl Eq for PythonEnvKernelSpecification {}
171
172impl PythonEnvKernelSpecification {
173    pub fn as_local_spec(&self) -> LocalKernelSpecification {
174        LocalKernelSpecification {
175            name: self.name.clone(),
176            path: self.path.clone(),
177            kernelspec: self.kernelspec.clone(),
178        }
179    }
180}
181
182#[derive(Debug, Clone, PartialEq, Eq)]
183pub enum KernelSpecification {
184    JupyterServer(RemoteKernelSpecification),
185    Jupyter(LocalKernelSpecification),
186    PythonEnv(PythonEnvKernelSpecification),
187    SshRemote(SshRemoteKernelSpecification),
188    WslRemote(WslKernelSpecification),
189}
190
191#[derive(Debug, Clone)]
192pub struct SshRemoteKernelSpecification {
193    pub name: String,
194    pub path: SharedString,
195    pub kernelspec: JupyterKernelspec,
196}
197
198#[derive(Debug, Clone)]
199pub struct WslKernelSpecification {
200    pub name: String,
201    pub kernelspec: JupyterKernelspec,
202    pub distro: String,
203}
204
205impl PartialEq for SshRemoteKernelSpecification {
206    fn eq(&self, other: &Self) -> bool {
207        self.name == other.name
208            && self.kernelspec.argv == other.kernelspec.argv
209            && self.path == other.path
210            && self.kernelspec.display_name == other.kernelspec.display_name
211            && self.kernelspec.language == other.kernelspec.language
212            && self.kernelspec.interrupt_mode == other.kernelspec.interrupt_mode
213            && self.kernelspec.env == other.kernelspec.env
214            && self.kernelspec.metadata == other.kernelspec.metadata
215    }
216}
217
218impl Eq for SshRemoteKernelSpecification {}
219
220impl PartialEq for WslKernelSpecification {
221    fn eq(&self, other: &Self) -> bool {
222        self.name == other.name
223            && self.kernelspec.argv == other.kernelspec.argv
224            && self.kernelspec.display_name == other.kernelspec.display_name
225            && self.kernelspec.language == other.kernelspec.language
226            && self.kernelspec.interrupt_mode == other.kernelspec.interrupt_mode
227            && self.kernelspec.env == other.kernelspec.env
228            && self.kernelspec.metadata == other.kernelspec.metadata
229            && self.distro == other.distro
230    }
231}
232
233impl Eq for WslKernelSpecification {}
234
235impl KernelSpecification {
236    pub fn name(&self) -> SharedString {
237        match self {
238            Self::Jupyter(spec) => spec.name.clone().into(),
239            Self::PythonEnv(spec) => spec.name.clone().into(),
240            Self::JupyterServer(spec) => spec.name.clone().into(),
241            Self::SshRemote(spec) => spec.name.clone().into(),
242            Self::WslRemote(spec) => spec.kernelspec.display_name.clone().into(),
243        }
244    }
245
246    pub fn type_name(&self) -> SharedString {
247        match self {
248            Self::Jupyter(_) => "Jupyter".into(),
249            Self::PythonEnv(spec) => SharedString::from(
250                spec.environment_kind
251                    .clone()
252                    .unwrap_or_else(|| "Python Environment".to_string()),
253            ),
254            Self::JupyterServer(_) => "Jupyter Server".into(),
255            Self::SshRemote(_) => "SSH Remote".into(),
256            Self::WslRemote(_) => "WSL Remote".into(),
257        }
258    }
259
260    pub fn path(&self) -> SharedString {
261        SharedString::from(match self {
262            Self::Jupyter(spec) => spec.path.to_string_lossy().into_owned(),
263            Self::PythonEnv(spec) => spec.path.to_string_lossy().into_owned(),
264            Self::JupyterServer(spec) => spec.url.to_string(),
265            Self::SshRemote(spec) => spec.path.to_string(),
266            Self::WslRemote(spec) => spec.distro.clone(),
267        })
268    }
269
270    pub fn language(&self) -> SharedString {
271        SharedString::from(match self {
272            Self::Jupyter(spec) => spec.kernelspec.language.clone(),
273            Self::PythonEnv(spec) => spec.kernelspec.language.clone(),
274            Self::JupyterServer(spec) => spec.kernelspec.language.clone(),
275            Self::SshRemote(spec) => spec.kernelspec.language.clone(),
276            Self::WslRemote(spec) => spec.kernelspec.language.clone(),
277        })
278    }
279
280    pub fn has_ipykernel(&self) -> bool {
281        match self {
282            Self::Jupyter(_) | Self::JupyterServer(_) | Self::SshRemote(_) | Self::WslRemote(_) => {
283                true
284            }
285            Self::PythonEnv(spec) => spec.has_ipykernel,
286        }
287    }
288
289    pub fn environment_kind_label(&self) -> Option<SharedString> {
290        match self {
291            Self::PythonEnv(spec) => spec
292                .environment_kind
293                .as_ref()
294                .map(|kind| SharedString::from(kind.clone())),
295            Self::Jupyter(_) => Some("Jupyter".into()),
296            Self::JupyterServer(_) => Some("Jupyter Server".into()),
297            Self::SshRemote(_) => Some("SSH Remote".into()),
298            Self::WslRemote(_) => Some("WSL Remote".into()),
299        }
300    }
301
302    pub fn icon(&self, cx: &App) -> Icon {
303        let lang_name = match self {
304            Self::Jupyter(spec) => spec.kernelspec.language.clone(),
305            Self::PythonEnv(spec) => spec.kernelspec.language.clone(),
306            Self::JupyterServer(spec) => spec.kernelspec.language.clone(),
307            Self::SshRemote(spec) => spec.kernelspec.language.clone(),
308            Self::WslRemote(spec) => spec.kernelspec.language.clone(),
309        };
310
311        file_icons::FileIcons::get(cx)
312            .get_icon_for_type(&lang_name.to_lowercase(), cx)
313            .map(Icon::from_path)
314            .unwrap_or(Icon::new(IconName::ReplNeutral))
315    }
316}
317
318fn extract_environment_kind(toolchain_json: &serde_json::Value) -> Option<String> {
319    let kind_str = toolchain_json.get("kind")?.as_str()?;
320    let label = match kind_str {
321        "Conda" => "Conda",
322        "Pixi" => "pixi",
323        "Homebrew" => "Homebrew",
324        "Pyenv" => "global (Pyenv)",
325        "GlobalPaths" => "global",
326        "PyenvVirtualEnv" => "Pyenv",
327        "Pipenv" => "Pipenv",
328        "Poetry" => "Poetry",
329        "MacPythonOrg" => "global (Python.org)",
330        "MacCommandLineTools" => "global (Command Line Tools for Xcode)",
331        "LinuxGlobal" => "global",
332        "MacXCode" => "global (Xcode)",
333        "Venv" => "venv",
334        "VirtualEnv" => "virtualenv",
335        "VirtualEnvWrapper" => "virtualenvwrapper",
336        "WindowsStore" => "global (Windows Store)",
337        "WindowsRegistry" => "global (Windows Registry)",
338        "Uv" => "uv",
339        "UvWorkspace" => "uv (Workspace)",
340        _ => kind_str,
341    };
342    Some(label.to_string())
343}
344
345pub fn python_env_kernel_specifications(
346    project: &Entity<Project>,
347    worktree_id: WorktreeId,
348    cx: &mut App,
349) -> impl Future<Output = Result<Vec<KernelSpecification>>> + use<> {
350    let python_language = LanguageName::new_static("Python");
351    let is_remote = project.read(cx).is_remote();
352    let wsl_distro = project
353        .read(cx)
354        .remote_connection_options(cx)
355        .and_then(|opts| {
356            if let RemoteConnectionOptions::Wsl(wsl) = opts {
357                Some(wsl.distro_name)
358            } else {
359                None
360            }
361        });
362
363    let toolchains = project.read(cx).available_toolchains(
364        ProjectPath {
365            worktree_id,
366            path: RelPath::empty().into(),
367        },
368        python_language,
369        cx,
370    );
371    #[allow(unused)]
372    let worktree_root_path: Option<std::sync::Arc<std::path::Path>> = project
373        .read(cx)
374        .worktree_for_id(worktree_id, cx)
375        .map(|w| w.read(cx).abs_path());
376
377    let background_executor = cx.background_executor().clone();
378
379    async move {
380        let (toolchains, user_toolchains) = if let Some(Toolchains {
381            toolchains,
382            root_path: _,
383            user_toolchains,
384        }) = toolchains.await
385        {
386            (toolchains, user_toolchains)
387        } else {
388            return Ok(Vec::new());
389        };
390
391        let kernelspecs = user_toolchains
392            .into_values()
393            .flatten()
394            .chain(toolchains.toolchains)
395            .map(|toolchain| {
396                let wsl_distro = wsl_distro.clone();
397                background_executor.spawn(async move {
398                    // For remote projects, we assume python is available assuming toolchain is reported.
399                    // We can skip the `ipykernel` check or run it remotely.
400                    // For MVP, lets trust the toolchain existence or do the check if it's cheap.
401                    // `new_smol_command` runs locally. We need to run remotely if `is_remote`.
402
403                    if is_remote {
404                        let default_kernelspec = JupyterKernelspec {
405                            argv: vec![
406                                toolchain.path.to_string(),
407                                "-m".to_string(),
408                                "ipykernel_launcher".to_string(),
409                                "-f".to_string(),
410                                "{connection_file}".to_string(),
411                            ],
412                            display_name: toolchain.name.to_string(),
413                            language: "python".to_string(),
414                            interrupt_mode: None,
415                            metadata: None,
416                            env: None,
417                        };
418
419                        if let Some(distro) = wsl_distro {
420                            log::debug!(
421                                "python_env_kernel_specifications: returning WslRemote for toolchain {}",
422                                toolchain.name
423                            );
424                            return Some(KernelSpecification::WslRemote(WslKernelSpecification {
425                                name: toolchain.name.to_string(),
426                                kernelspec: default_kernelspec,
427                                distro,
428                            }));
429                        }
430
431                        log::debug!(
432                            "python_env_kernel_specifications: returning SshRemote for toolchain {}",
433                            toolchain.name
434                        );
435                        return Some(KernelSpecification::SshRemote(
436                            SshRemoteKernelSpecification {
437                                name: format!("Remote {}", toolchain.name),
438                                path: toolchain.path.clone(),
439                                kernelspec: default_kernelspec,
440                            },
441                        ));
442                    }
443
444                    let python_path = toolchain.path.to_string();
445                    let environment_kind = extract_environment_kind(&toolchain.as_json);
446
447                    let has_ipykernel = util::command::new_command(&python_path)
448                        .args(&["-c", "import ipykernel"])
449                        .output()
450                        .await
451                        .map(|output| output.status.success())
452                        .unwrap_or(false);
453
454                    let mut env = HashMap::new();
455                    if let Some(python_bin_dir) = PathBuf::from(&python_path).parent() {
456                        if let Some(path_var) = std::env::var_os("PATH") {
457                            let mut paths = std::env::split_paths(&path_var).collect::<Vec<_>>();
458                            paths.insert(0, python_bin_dir.to_path_buf());
459                            if let Ok(new_path) = std::env::join_paths(paths) {
460                                env.insert("PATH".to_string(), new_path.to_string_lossy().to_string());
461                            }
462                        }
463
464                        if let Some(venv_root) = python_bin_dir.parent() {
465                            env.insert("VIRTUAL_ENV".to_string(), venv_root.to_string_lossy().to_string());
466                        }
467                    }
468
469                    log::info!("Preparing Python kernel for toolchain: {}", toolchain.name);
470                    log::info!("Python path: {}", python_path);
471                    if let Some(path) = env.get("PATH") {
472                         log::info!("Kernel PATH: {}", path);
473                    } else {
474                         log::info!("Kernel PATH not set in env");
475                    }
476                    if let Some(venv) = env.get("VIRTUAL_ENV") {
477                         log::info!("Kernel VIRTUAL_ENV: {}", venv);
478                    }
479
480                    let kernelspec = JupyterKernelspec {
481                        argv: vec![
482                            python_path.clone(),
483                            "-m".to_string(),
484                            "ipykernel_launcher".to_string(),
485                            "-f".to_string(),
486                            "{connection_file}".to_string(),
487                        ],
488                        display_name: toolchain.name.to_string(),
489                        language: "python".to_string(),
490                        interrupt_mode: None,
491                        metadata: None,
492                        env: Some(env),
493                    };
494
495                    Some(KernelSpecification::PythonEnv(PythonEnvKernelSpecification {
496                        name: toolchain.name.to_string(),
497                        path: PathBuf::from(&python_path),
498                        kernelspec,
499                        has_ipykernel,
500                        environment_kind,
501                    }))
502                })
503            });
504
505        #[allow(unused_mut)]
506        let mut kernel_specs: Vec<KernelSpecification> = futures::future::join_all(kernelspecs)
507            .await
508            .into_iter()
509            .flatten()
510            .collect();
511
512        #[cfg(target_os = "windows")]
513        if kernel_specs.is_empty() && !is_remote {
514            if let Some(root_path) = worktree_root_path {
515                let root_path_str: std::borrow::Cow<str> = root_path.to_string_lossy();
516                let (distro, internal_path) =
517                    if let Some(path_without_prefix) = root_path_str.strip_prefix(r"\\wsl$\") {
518                        if let Some((distro, path)) = path_without_prefix.split_once('\\') {
519                            let replaced_path: String = path.replace('\\', "/");
520                            (Some(distro), Some(format!("/{}", replaced_path)))
521                        } else {
522                            (Some(path_without_prefix), Some("/".to_string()))
523                        }
524                    } else if let Some(path_without_prefix) =
525                        root_path_str.strip_prefix(r"\\wsl.localhost\")
526                    {
527                        if let Some((distro, path)) = path_without_prefix.split_once('\\') {
528                            let replaced_path: String = path.replace('\\', "/");
529                            (Some(distro), Some(format!("/{}", replaced_path)))
530                        } else {
531                            (Some(path_without_prefix), Some("/".to_string()))
532                        }
533                    } else {
534                        (None, None)
535                    };
536
537                if let (Some(distro), Some(internal_path)) = (distro, internal_path) {
538                    let python_path = format!("{}/.venv/bin/python", internal_path);
539                    let check = util::command::new_command("wsl")
540                        .args(&["-d", distro, "test", "-f", &python_path])
541                        .output()
542                        .await;
543
544                    if check.is_ok() && check.unwrap().status.success() {
545                        let default_kernelspec = JupyterKernelspec {
546                            argv: vec![
547                                python_path.clone(),
548                                "-m".to_string(),
549                                "ipykernel_launcher".to_string(),
550                                "-f".to_string(),
551                                "{connection_file}".to_string(),
552                            ],
553                            display_name: format!("WSL: {} (.venv)", distro),
554                            language: "python".to_string(),
555                            interrupt_mode: None,
556                            metadata: None,
557                            env: None,
558                        };
559
560                        kernel_specs.push(KernelSpecification::WslRemote(WslKernelSpecification {
561                            name: format!("WSL: {} (.venv)", distro),
562                            kernelspec: default_kernelspec,
563                            distro: distro.to_string(),
564                        }));
565                    } else {
566                        let check_system = util::command::new_command("wsl")
567                            .args(&["-d", distro, "command", "-v", "python3"])
568                            .output()
569                            .await;
570
571                        if check_system.is_ok() && check_system.unwrap().status.success() {
572                            let default_kernelspec = JupyterKernelspec {
573                                argv: vec![
574                                    "python3".to_string(),
575                                    "-m".to_string(),
576                                    "ipykernel_launcher".to_string(),
577                                    "-f".to_string(),
578                                    "{connection_file}".to_string(),
579                                ],
580                                display_name: format!("WSL: {} (System)", distro),
581                                language: "python".to_string(),
582                                interrupt_mode: None,
583                                metadata: None,
584                                env: None,
585                            };
586
587                            kernel_specs.push(KernelSpecification::WslRemote(
588                                WslKernelSpecification {
589                                    name: format!("WSL: {} (System)", distro),
590                                    kernelspec: default_kernelspec,
591                                    distro: distro.to_string(),
592                                },
593                            ));
594                        }
595                    }
596                }
597            }
598        }
599
600        anyhow::Ok(kernel_specs)
601    }
602}
603
604pub trait RunningKernel: Send + Debug {
605    fn request_tx(&self) -> mpsc::Sender<JupyterMessage>;
606    fn stdin_tx(&self) -> mpsc::Sender<JupyterMessage>;
607    fn working_directory(&self) -> &PathBuf;
608    fn execution_state(&self) -> &ExecutionState;
609    fn set_execution_state(&mut self, state: ExecutionState);
610    fn kernel_info(&self) -> Option<&KernelInfoReply>;
611    fn set_kernel_info(&mut self, info: KernelInfoReply);
612    fn force_shutdown(&mut self, window: &mut Window, cx: &mut App) -> Task<anyhow::Result<()>>;
613    fn kill(&mut self);
614}
615
616#[derive(Debug, Clone)]
617pub enum KernelStatus {
618    Idle,
619    Busy,
620    Starting,
621    Error,
622    ShuttingDown,
623    Shutdown,
624    Restarting,
625}
626
627impl KernelStatus {
628    pub fn is_connected(&self) -> bool {
629        matches!(self, KernelStatus::Idle | KernelStatus::Busy)
630    }
631}
632
633impl ToString for KernelStatus {
634    fn to_string(&self) -> String {
635        match self {
636            KernelStatus::Idle => "Idle".to_string(),
637            KernelStatus::Busy => "Busy".to_string(),
638            KernelStatus::Starting => "Starting".to_string(),
639            KernelStatus::Error => "Error".to_string(),
640            KernelStatus::ShuttingDown => "Shutting Down".to_string(),
641            KernelStatus::Shutdown => "Shutdown".to_string(),
642            KernelStatus::Restarting => "Restarting".to_string(),
643        }
644    }
645}
646
647#[derive(Debug)]
648pub enum Kernel {
649    RunningKernel(Box<dyn RunningKernel>),
650    StartingKernel(Shared<Task<()>>),
651    ErroredLaunch(String),
652    ShuttingDown,
653    Shutdown,
654    Restarting,
655}
656
657impl From<&Kernel> for KernelStatus {
658    fn from(kernel: &Kernel) -> Self {
659        match kernel {
660            Kernel::RunningKernel(kernel) => match kernel.execution_state() {
661                ExecutionState::Idle => KernelStatus::Idle,
662                ExecutionState::Busy => KernelStatus::Busy,
663                ExecutionState::Unknown => KernelStatus::Error,
664                ExecutionState::Starting => KernelStatus::Starting,
665                ExecutionState::Restarting => KernelStatus::Restarting,
666                ExecutionState::Terminating => KernelStatus::ShuttingDown,
667                ExecutionState::AutoRestarting => KernelStatus::Restarting,
668                ExecutionState::Dead => KernelStatus::Error,
669                ExecutionState::Other(_) => KernelStatus::Error,
670            },
671            Kernel::StartingKernel(_) => KernelStatus::Starting,
672            Kernel::ErroredLaunch(_) => KernelStatus::Error,
673            Kernel::ShuttingDown => KernelStatus::ShuttingDown,
674            Kernel::Shutdown => KernelStatus::Shutdown,
675            Kernel::Restarting => KernelStatus::Restarting,
676        }
677    }
678}
679
680impl Kernel {
681    pub fn status(&self) -> KernelStatus {
682        self.into()
683    }
684
685    pub fn set_execution_state(&mut self, status: &ExecutionState) {
686        if let Kernel::RunningKernel(running_kernel) = self {
687            running_kernel.set_execution_state(status.clone());
688        }
689    }
690
691    pub fn set_kernel_info(&mut self, kernel_info: &KernelInfoReply) {
692        if let Kernel::RunningKernel(running_kernel) = self {
693            running_kernel.set_kernel_info(kernel_info.clone());
694        }
695    }
696
697    pub fn is_shutting_down(&self) -> bool {
698        match self {
699            Kernel::Restarting | Kernel::ShuttingDown => true,
700            Kernel::RunningKernel(_)
701            | Kernel::StartingKernel(_)
702            | Kernel::ErroredLaunch(_)
703            | Kernel::Shutdown => false,
704        }
705    }
706}