cmake_file_api/objects/codemodel_v2/
backtrace_graph.rs

1use serde::{Deserialize, Serialize};
2use std::path::PathBuf;
3
4/// The backtraceGraph member of a "codemodel" version 2 "directory" object, or "codemodel" version 2 "target" object.
5/// Describes a graph of backtraces.
6/// Its nodes are referenced from backtrace members elsewhere in the containing object.
7#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
8#[serde(rename_all = "camelCase")]
9#[non_exhaustive]
10pub struct BacktraceGraph {
11    /// Backtrace nodes.
12    pub nodes: Vec<Node>,
13
14    /// Command names referenced by backtrace nodes.
15    /// Each entry is a string specifying a command name.
16    pub commands: Vec<String>,
17
18    /// CMake's language files referenced by backtrace nodes
19    /// Each entry is a path to a file, represented with forward slashes.
20    /// If the file is inside the top-level source directory then the path is specified relative to that directory.
21    /// Otherwise, the path is absolute.
22    pub files: Vec<PathBuf>,
23}
24
25#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
26#[serde(rename_all = "camelCase")]
27#[non_exhaustive]
28pub struct Node {
29    /// An unsigned integer 0-based index into the backtrace files array.
30    pub file: usize,
31
32    /// An optional member present when the node represents a line within the file.
33    /// The value is an unsigned integer 1-based line number.
34    pub line: Option<usize>,
35
36    /// An optional member present when the node represents a command invocation within the file.
37    /// The value is an unsigned integer 0-based index into the backtrace commands array.
38    pub command: Option<usize>,
39
40    /// An optional member present when the node is not the bottom of the call stack.
41    /// The value is an unsigned integer 0-based index of another entry in the backtrace nodes array.
42    pub parent: Option<usize>,
43}
44
45#[cfg(test)]
46mod tests {
47    use crate::objects::codemodel_v2::backtrace_graph::*;
48    use serde_json::json;
49
50    #[test]
51    fn test_backtrace_graph() {
52        let json = json!({
53            "commands" :
54            [
55                "add_executable",
56                "target_link_libraries"
57            ],
58            "files" :
59            [
60                "CMakeLists.txt"
61            ],
62            "nodes" :
63            [
64                {
65                    "file" : 0
66                },
67                {
68                    "command" : 0,
69                    "file" : 0,
70                    "line" : 4,
71                    "parent" : 0
72                },
73                {
74                    "command" : 1,
75                    "file" : 0,
76                    "line" : 9,
77                    "parent" : 0
78                }
79            ]
80        });
81
82        let graph = serde_json::from_value::<BacktraceGraph>(json).unwrap();
83        assert_eq!(
84            graph,
85            BacktraceGraph {
86                commands: vec![
87                    "add_executable".to_string(),
88                    "target_link_libraries".to_string()
89                ],
90                files: vec![PathBuf::from("CMakeLists.txt")],
91                nodes: vec![
92                    Node {
93                        file: 0,
94                        ..Default::default()
95                    },
96                    Node {
97                        file: 0,
98                        command: Some(0),
99                        line: Some(4),
100                        parent: Some(0)
101                    },
102                    Node {
103                        file: 0,
104                        command: Some(1),
105                        line: Some(9),
106                        parent: Some(0)
107                    }
108                ]
109            }
110        );
111    }
112}