firedbg_protocol/
info.rs

1//! Data structures for Debugger Info
2
3use crate::util::impl_serde_with_str;
4use serde::{Deserialize, Serialize};
5use std::{fmt::Display, str::FromStr};
6
7pub const FIRE_DBG_FOR_RUST: &str = "FireDBG.for.Rust";
8pub const INFO_STREAM: &str = "info";
9pub const FILE_STREAM: &str = "file";
10pub const BREAKPOINT_STREAM: &str = "breakpoint";
11pub const EVENT_STREAM: &str = "event";
12
13#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
14#[serde(tag = "type")]
15/// Information of the debugger run.
16pub enum InfoMessage {
17    Debugger(DebuggerInfo),
18    Exit(ProgExitInfo),
19}
20
21#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
22/// Debugger Info
23pub struct DebuggerInfo {
24    /// The debugger engine
25    pub debugger: FireDbgForRust,
26    /// FireDBG version
27    pub version: String,
28    pub workspace_root: String,
29    pub package_name: String,
30    /// The target executable
31    pub target: String,
32    /// Arguments to the executable
33    pub arguments: Vec<String>,
34}
35
36#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
37/// Program Exit Info
38pub struct ProgExitInfo {
39    pub exit_code: i32,
40}
41
42#[derive(Debug, Clone, Copy, PartialEq)]
43/// Our magic pass phrase.
44pub struct FireDbgForRust;
45
46impl Display for FireDbgForRust {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        write!(f, "{}", FIRE_DBG_FOR_RUST)
49    }
50}
51
52impl FromStr for FireDbgForRust {
53    type Err = &'static str;
54
55    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
56        if s == FIRE_DBG_FOR_RUST {
57            Ok(Self)
58        } else {
59            Err("Invalid FireDbgForRust marker")
60        }
61    }
62}
63
64impl InfoMessage {
65    pub fn redacted(&mut self) {
66        match self {
67            InfoMessage::Debugger(ref mut debugger_info) => {
68                debugger_info.redacted();
69            }
70            InfoMessage::Exit(_) => (),
71        }
72    }
73}
74
75impl DebuggerInfo {
76    pub fn redacted(&mut self) {
77        self.version = "<redacted>".into();
78
79        let path = std::path::Path::new(&self.workspace_root);
80        let file_name = path.file_name().expect("file").to_str().expect("str");
81        self.workspace_root = format!("<redacted>/{file_name}");
82
83        let path = std::path::Path::new(&self.target);
84        let file_name = path.file_name().expect("file").to_str().expect("str");
85        self.target = format!("<redacted>/{file_name}");
86    }
87}
88
89impl_serde_with_str!(FireDbgForRust);