component_debug/
explore.rs

1// Copyright 2023 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use anyhow::{format_err, Result};
6use fidl_fuchsia_dash as fdash;
7use futures::prelude::*;
8use moniker::Moniker;
9use std::str::FromStr;
10
11#[derive(Debug, PartialEq)]
12pub enum DashNamespaceLayout {
13    NestAllInstanceDirs,
14    InstanceNamespaceIsRoot,
15}
16
17impl FromStr for DashNamespaceLayout {
18    type Err = anyhow::Error;
19
20    fn from_str(s: &str) -> Result<Self> {
21        match s {
22            "namespace" => Ok(Self::InstanceNamespaceIsRoot),
23            "nested" => Ok(Self::NestAllInstanceDirs),
24            _ => Err(format_err!("unknown layout (expected 'namespace' or 'nested')")),
25        }
26    }
27}
28
29impl Into<fdash::DashNamespaceLayout> for DashNamespaceLayout {
30    fn into(self) -> fdash::DashNamespaceLayout {
31        match self {
32            Self::NestAllInstanceDirs => fdash::DashNamespaceLayout::NestAllInstanceDirs,
33            Self::InstanceNamespaceIsRoot => fdash::DashNamespaceLayout::InstanceNamespaceIsRoot,
34        }
35    }
36}
37
38pub async fn explore_over_socket(
39    moniker: Moniker,
40    pty_server: fidl::Socket,
41    tools_urls: Vec<String>,
42    command: Option<String>,
43    ns_layout: DashNamespaceLayout,
44    launcher_proxy: &fdash::LauncherProxy,
45) -> Result<()> {
46    launcher_proxy
47        .explore_component_over_socket(
48            &moniker.to_string(),
49            pty_server,
50            &tools_urls,
51            command.as_deref(),
52            ns_layout.into(),
53        )
54        .await
55        .map_err(|e| format_err!("fidl error launching dash: {}", e))?
56        .map_err(|e| match e {
57            fdash::LauncherError::InstanceNotFound => {
58                format_err!("No instance was found matching the moniker '{}'.", moniker)
59            }
60            fdash::LauncherError::InstanceNotResolved => format_err!(
61                "{} is not resolved. Resolve the instance and retry this command",
62                moniker
63            ),
64            e => format_err!("Unexpected error launching dash: {:?}", e),
65        })?;
66    Ok(())
67}
68
69pub async fn wait_for_shell_exit(launcher_proxy: &fdash::LauncherProxy) -> Result<i32> {
70    // Report process errors and return the exit status.
71    let mut event_stream = launcher_proxy.take_event_stream();
72    match event_stream.next().await {
73        Some(Ok(fdash::LauncherEvent::OnTerminated { return_code })) => Ok(return_code),
74        Some(Err(e)) => Err(format_err!("OnTerminated event error: {:?}", e)),
75        None => Err(format_err!("didn't receive an expected OnTerminated event")),
76    }
77}