forked from SpaceManiac/SpacemanDMM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacros.rs
153 lines (141 loc) · 5.34 KB
/
macros.rs
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
//! Utility macros.
pub mod all_methods {
pub use lsp_types::request::*;
pub use crate::extras::*;
}
pub mod all_notifications {
pub use lsp_types::notification::*;
pub use crate::extras::*;
}
macro_rules! handle_method_call {
($($(#[$attr:meta])* on $what:ident(&mut $self:ident, $p:pat) $b:block)*) => {
impl<'a> Engine<'a> {
fn handle_method_call_table(method: &str) -> Option<fn(&mut Self, serde_json::Value) -> Result<serde_json::Value, jsonrpc::Error>> {
use macros::all_methods::*;
$(if method == <$what>::METHOD {
Some(|this, params_value| {
let params: <$what as Request>::Params = serde_json::from_value(params_value).map_err(invalid_request)?;
let result: <$what as Request>::Result = this.$what(params)?;
Ok(serde_json::to_value(result).expect("encode problem"))
})
} else)* {
None
}
}
$(
#[allow(non_snake_case)]
$(#[$attr])*
fn $what(&mut $self, $p: <macros::all_methods::$what as lsp_types::request::Request>::Params)
-> Result<<macros::all_methods::$what as lsp_types::request::Request>::Result, jsonrpc::Error>
{
#[allow(unused_imports)]
use lsp_types::*;
#[allow(unused_imports)]
use lsp_types::request::*;
let _v = $b;
#[allow(unreachable_code)] { Ok(_v) }
}
)*
}
}
}
macro_rules! handle_notification {
($(on $what:ident(&mut $self:ident, $p:pat) $b:block)*) => {
impl<'a> Engine<'a> {
fn handle_notification_table(method: &str) -> Option<fn(&mut Self, serde_json::Value) -> Result<(), jsonrpc::Error>> {
use macros::all_notifications::*;
$(if method == <$what>::METHOD {
Some(|this, params_value| {
let params: <$what as Notification>::Params = serde_json::from_value(params_value).map_err(invalid_request)?;
this.$what(params)
})
} else)* {
None
}
}
$(
#[allow(non_snake_case)]
fn $what(&mut $self, $p: <macros::all_notifications::$what as lsp_types::notification::Notification>::Params)
-> Result<(), jsonrpc::Error>
{
#[allow(unused_imports)]
use lsp_types::*;
#[allow(unused_imports)]
use lsp_types::notification::*;
let _v = $b;
#[allow(unreachable_code)] { Ok(_v) }
}
)*
}
}
}
macro_rules! handle_request {
($(on $what:ident(&mut $self:ident, $p:pat) $b:block)*) => {
impl Debugger {
fn handle_request_table(command: &str) -> Option<fn(&mut Self, serde_json::Value) -> Result<serde_json::Value, Box<dyn Error>>> {
use crate::debugger::dap_types::*;
$(if command == <$what>::COMMAND {
Some(|this, arguments| {
let params: <$what as Request>::Params = serde_json::from_value(arguments)?;
let result: <$what as Request>::Result = this.$what(params)?;
Ok(serde_json::to_value(result).expect("encode problem"))
})
} else)* {
None
}
}
$(
#[allow(non_snake_case)]
fn $what(&mut $self, $p: <$what as crate::debugger::dap_types::Request>::Params)
-> Result<<$what as crate::debugger::dap_types::Request>::Result, Box<dyn Error>>
{
let _v = $b;
#[allow(unreachable_code)] { Ok(_v) }
}
)*
}
}
}
macro_rules! handle_extools {
($(on $what:ident(&mut $self:ident, $p:pat) $b:block)*) => {
impl ExtoolsThread {
fn handle_response_table(type_: &str) -> Option<fn(&mut Self, serde_json::Value) -> Result<(), Box<dyn Error>>> {
$(if type_ == <$what as Response>::TYPE {
Some(|this, content| {
let deserialized: $what = serde_json::from_value(content)?;
this.$what(deserialized)
})
} else)* {
None
}
}
$(
#[allow(non_snake_case)]
fn $what(&mut $self, $p: $what) -> Result<(), Box<dyn Error>> {
let _v = $b;
#[allow(unreachable_code)] { Ok(_v) }
}
)*
}
}
}
macro_rules! if_annotation {
($p:pat in $a:expr; $b:block) => {
for (_, thing) in $a.clone() {
if let $p = thing {
$b
break
}
}
}
}
macro_rules! match_annotation {
($a:expr; $($($p:pat)|* => $b:block,)*) => {
for (_, thing) in $a.clone() {
match thing {
$($($p)|* => $b,)*
_ => {}
}
}
}
}