-
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathutils.rs
More file actions
216 lines (180 loc) · 5.05 KB
/
Copy pathutils.rs
File metadata and controls
216 lines (180 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use std::{collections::VecDeque, error::Error, fmt};
use petgraph::visit::{EdgeRef, IntoEdgeReferences, IntoNodeReferences};
use wasm_bindgen::prelude::*;
use web_time::Instant;
#[cfg(not(feature = "test"))]
#[wasm_bindgen(module = "src/logger/index.ts")]
extern "C" {
#[wasm_bindgen(thread_local_v2, js_name = log)]
pub static LOGGER: Logger;
pub type Logger;
#[wasm_bindgen(method)]
pub fn debug(this: &Logger, message: &str);
#[wasm_bindgen(method)]
pub fn error(this: &Logger, message: &str);
}
#[cfg(feature = "test")]
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(thread_local_v2, js_name = console)]
pub static LOGGER: Logger;
pub type Logger;
#[wasm_bindgen(method)]
pub fn debug(this: &Logger, message: &str);
#[wasm_bindgen(method)]
pub fn error(this: &Logger, message: &str);
}
pub struct PerfLogger {
name: String,
start: Instant,
elapsed: Option<u128>,
splits: Vec<PerfLogger>,
}
impl PerfLogger {
pub fn new(name: String) -> Self {
PerfLogger {
name,
start: Instant::now(),
splits: Vec::new(),
elapsed: None,
}
}
pub fn start_split(&mut self, name: String) -> &mut PerfLogger {
// stop the last split
self.stop_split();
// create a new split
self.splits.push(PerfLogger::new(name));
self.splits.last_mut().unwrap()
}
pub fn stop_split(&mut self) {
if let Some(split) = self.splits.last_mut() {
if !split.stopped() {
split.stop()
}
}
}
pub fn stop(&mut self) {
if self.stopped() {
LOGGER.with(|l| l.debug(&format!("PerfLogger {} is already stopped", self.name)));
} else {
self.elapsed = Some(self.start.elapsed().as_micros());
self.stop_split();
}
}
pub fn stopped(&self) -> bool {
self.elapsed.is_some()
}
fn get_log_message(&mut self) -> Vec<String> {
if !self.stopped() {
self.stop();
}
let mut message = vec![format!(
"{}ms > {}",
self.elapsed.unwrap() as f64 / 1000f64,
self.name
)];
for split in self.splits.iter_mut() {
let mut sub_message = split
.get_log_message()
.iter()
.map(|s| format!(" | {s}"))
.collect::<Vec<String>>();
message.append(&mut sub_message);
}
message
}
pub fn log(&mut self) {
LOGGER.with(|l| l.debug(&self.get_log_message().join("\n")));
}
}
pub type Result<T> = std::result::Result<T, NoteGraphError>;
#[wasm_bindgen]
#[derive(Clone, Debug)]
pub struct NoteGraphError {
message: String,
}
#[wasm_bindgen]
impl NoteGraphError {
#[wasm_bindgen(constructor)]
pub fn new(message: &str) -> NoteGraphError {
NoteGraphError {
message: message.to_string(),
}
}
#[wasm_bindgen(getter)]
pub fn message(&self) -> String {
self.message.clone()
}
#[wasm_bindgen(js_name = toString)]
pub fn to_fancy_string(&self) -> String {
format!("{self:#?}")
}
}
impl fmt::Display for NoteGraphError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.message)
}
}
impl Error for NoteGraphError {}
pub struct DepthFirstTraversalDataStructure<T> {
stack: Vec<T>,
}
pub struct BreadthFirstTraversalDataStructure<T> {
queue: VecDeque<T>,
}
impl<T> GraphTraversalDataStructure<T> for DepthFirstTraversalDataStructure<T> {
fn new() -> Self {
DepthFirstTraversalDataStructure { stack: Vec::new() }
}
fn push(&mut self, value: T) {
self.stack.push(value);
}
fn pop(&mut self) -> Option<T> {
self.stack.pop()
}
fn is_empty(&self) -> bool {
self.stack.is_empty()
}
}
impl<T> GraphTraversalDataStructure<T> for BreadthFirstTraversalDataStructure<T> {
fn new() -> Self {
BreadthFirstTraversalDataStructure {
queue: VecDeque::new(),
}
}
fn push(&mut self, value: T) {
self.queue.push_back(value);
}
fn pop(&mut self) -> Option<T> {
self.queue.pop_front()
}
fn is_empty(&self) -> bool {
self.queue.is_empty()
}
}
pub trait GraphTraversalDataStructure<T> {
fn new() -> Self;
fn push(&mut self, value: T);
fn pop(&mut self) -> Option<T>;
fn is_empty(&self) -> bool;
}
pub fn graph_eq<N, E, Ty, Ix>(
a: &petgraph::stable_graph::StableGraph<N, E, Ty, Ix>,
b: &petgraph::stable_graph::StableGraph<N, E, Ty, Ix>,
) -> bool
where
N: PartialEq,
E: PartialEq,
Ty: petgraph::EdgeType,
Ix: petgraph::graph::IndexType + PartialEq,
{
let a_ns = a.node_references().map(|n| n.1);
let b_ns = b.node_references().map(|n| n.1);
let a_es = a
.edge_references()
.map(|e| (e.source(), e.target(), e.weight()));
let b_es = b
.edge_references()
.map(|e| (e.source(), e.target(), e.weight()));
a_ns.eq(b_ns) && a_es.eq(b_es)
}