rs_parse_snapshot/
define.rs

1pub mod define {
2    use serde::{Deserialize, Serialize};
3    use std::cell::RefCell;
4    use std::cmp::Ordering;
5    use std::rc::Rc;
6    use std::sync::{Arc, Mutex};
7    pub const NODE_TYPES_PROPERTY: [&str; 14] = [
8        "hidden",
9        "array",
10        "string",
11        "object",
12        "code",
13        "closure",
14        "regexp",
15        "number",
16        "native",
17        "synthetic",
18        "concatenated string",
19        "sliced string",
20        "symbol",
21        "bigint",
22    ];
23    pub const NODE_OTHERS_PROPERTY: [&str; 6] =
24        ["string", "number", "number", "number", "number", "number"];
25
26    pub const NODE_FIELDS: [&str; 6] = [
27        "type",
28        "name",
29        "id",
30        "self_size",
31        "edge_count",
32        "trace_node_id",
33    ];
34    pub const NODE_FIELDS_NODE16: [&str; 7] = [
35        "type",
36        "name",
37        "id",
38        "self_size",
39        "edge_count",
40        "trace_node_id",
41        "detachedness",
42    ];
43    pub const EDGE_FIELDS: [&str; 3] = ["type", "name_or_index", "to_node"];
44    pub const EDGE_TYPES_PROPERTY: [&str; 7] = [
45        "context", "element", "property", "internal", "hidden", "shortcut", "weak",
46    ];
47    pub const EDGE_OTHERS_PROPERTY: [&str; 2] = ["string_or_number", "node"];
48    pub const FORBIDDEN_EDGE_TYPE: [&str; 4] = ["weak", "internal", "invisible", "hidden"];
49    #[derive(Serialize, Deserialize, Debug)]
50    pub struct Meta {
51        pub node_fields: Vec<String>,
52        pub edge_fields: [String; 3],
53        pub trace_function_info_fields: [String; 6],
54        pub trace_node_fields: [String; 5],
55        pub sample_fields: [String; 2],
56        pub location_fields: [String; 4],
57    }
58    #[derive(Serialize, Deserialize, Debug)]
59    pub struct SnapShot {
60        pub meta: Meta,
61        pub node_count: usize,
62        pub edge_count: usize,
63        pub trace_function_count: usize,
64    }
65    #[derive(Deserialize, Debug)]
66    pub struct Heapsnapshot {
67        pub snapshot: SnapShot,
68        pub nodes: Vec<usize>,
69        pub edges: Vec<usize>,
70        pub strings: Vec<String>,
71        #[serde(skip_deserializing)]
72        pub locations: Vec<usize>,
73        #[serde(skip_deserializing)]
74        pub trace_function_infos: Vec<usize>,
75        #[serde(skip_deserializing)]
76        pub trace_tree: Vec<usize>,
77        #[serde(skip_deserializing)]
78        pub samples: Vec<usize>,
79    }
80
81    pub enum NodeParam<'a> {
82        RcNode(&'a RcNode),
83        Node(&'a mut Node),
84    }
85    #[derive(Debug, Serialize, Deserialize, Clone)]
86    pub struct Node {
87        pub nt: String,   // node_type
88        pub name: String, // name
89        pub id: usize,    // id
90        pub size: usize,  // self_size
91        #[serde(skip_serializing)]
92        pub ec: usize, // edge_count
93        // pub trace_node_id: JsValueType,
94        pub rs: usize, // retained_size
95        pub edges: Vec<Edge>,
96        pub p: Vec<usize>,
97        pub c: String,
98        #[serde(skip_serializing_if = "Option::is_none")]
99        pub se: Option<String>, // source
100    }
101
102    pub type RcNode = Rc<RefCell<Node>>;
103    pub type ArcNode = Arc<Mutex<Node>>;
104
105    impl PartialOrd for Node {
106        fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
107            if self.size > other.size {
108                Some(Ordering::Greater)
109            } else if self.size == other.size {
110                Some(Ordering::Equal)
111            } else {
112                Some(Ordering::Less)
113            }
114        }
115    }
116
117    impl PartialEq for Node {
118        fn eq(&self, other: &Self) -> bool {
119            self.size == other.size
120        }
121    }
122
123    #[derive(Debug, Serialize, Deserialize, Clone)]
124    pub struct Edge {
125        pub et: String, // edge_type
126        pub tn: usize,  // to_node
127        pub ni: String, // name_or_index
128        #[serde(skip_serializing)]
129        pub isw: usize, // is_weak_retainer 0 false 1 true
130        pub isr: usize, // is_retainer 0 false 1 true
131    }
132
133    pub enum NodePropertyType {
134        Arr([&'static str; 14]),
135        Str(&'static str),
136    }
137    pub enum EdgePropertyType {
138        Arr([&'static str; 7]),
139        Str(&'static str),
140    }
141    #[derive(Debug, Serialize, Deserialize, Clone)]
142    #[serde(untagged)]
143    pub enum JsValueType {
144        JsString(String),
145        JsNumber(usize),
146    }
147    impl PartialEq for JsValueType {
148        fn eq(&self, other: &Self) -> bool {
149            use JsValueType::*;
150            match (self, other) {
151                (&JsString(ref a), &JsString(ref b)) => a == b,
152                (&JsNumber(ref a), &JsNumber(ref b)) => a == b,
153                _ => false,
154            }
155        }
156    }
157    impl PartialOrd for JsValueType {
158        fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
159            use JsValueType::*;
160            match (self, other) {
161                (&JsNumber(ref a), &JsNumber(ref b)) => {
162                    if a - b > 0 {
163                        Some(Ordering::Greater)
164                    } else if a == b {
165                        Some(Ordering::Equal)
166                    } else {
167                        Some(Ordering::Less)
168                    }
169                }
170                _ => None,
171            }
172        }
173    }
174    impl From<JsValueType> for usize {
175        fn from(s: JsValueType) -> usize {
176            use JsValueType::*;
177            match s {
178                JsNumber(val) => val,
179                JsString(val) => panic!("JsString {:?} cannot convert to usize", val),
180            }
181        }
182    }
183    impl From<&JsValueType> for String {
184        fn from(s: &JsValueType) -> String {
185            use JsValueType::*;
186            match s {
187                JsNumber(val) => panic!("JsNumber {:?} cannot convert to String", val),
188                JsString(val) => val.clone(),
189            }
190        }
191    }
192    impl From<JsValueType> for String {
193        fn from(s: JsValueType) -> String {
194            use JsValueType::*;
195            match s {
196                JsNumber(val) => panic!("JsNumber {:?} cannot convert to String", val),
197                JsString(val) => val,
198            }
199        }
200    }
201}