-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtokens.rs
More file actions
257 lines (227 loc) · 6.5 KB
/
tokens.rs
File metadata and controls
257 lines (227 loc) · 6.5 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
use regex::Regex;
use std::{collections::HashMap, fmt};
use tree_sitter::Range;
use crate::document_store::DocumentStore;
#[derive(Debug)]
pub struct Token {
pub range: Range,
pub data: TokenData,
}
impl Token {
pub fn new(data: TokenData, range: Range) -> Self {
Self { data, range }
}
}
#[derive(Debug)]
pub enum TokenData {
PhpClassReference(PhpClassName),
PhpClassDefinition(PhpClass),
PhpMethodReference(PhpMethod),
PhpMethodDefinition(PhpMethod),
DrupalRouteReference(String),
DrupalRouteDefinition(DrupalRoute),
DrupalServiceReference(String),
DrupalServiceDefinition(DrupalService),
DrupalHookReference(String),
DrupalHookDefinition(DrupalHook),
DrupalPermissionDefinition(DrupalPermission),
DrupalPermissionReference(String),
DrupalPluginReference(DrupalPluginReference),
DrupalTranslationString(DrupalTranslationString),
}
#[derive(Debug, PartialEq, Clone)]
pub struct PhpClassName {
value: String,
}
impl fmt::Display for PhpClassName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.value)
}
}
impl From<&str> for PhpClassName {
fn from(value: &str) -> Self {
Self {
// Trim single quotes and backslashes to ensure the most trimmed down version of a
// fully qualified class name.
value: value.trim_matches(['\'', '\\']).to_string(),
}
}
}
#[derive(Debug)]
pub enum ClassAttribute {
Plugin(DrupalPlugin),
}
#[derive(Debug)]
pub struct PhpClass {
pub name: PhpClassName,
pub attribute: Option<ClassAttribute>,
pub methods: HashMap<String, Box<Token>>,
}
#[derive(Debug)]
pub struct PhpMethod {
pub name: String,
pub class_name: Option<PhpClassName>,
pub service_name: Option<String>,
}
impl PhpMethod {
pub fn get_class(&self, store: &DocumentStore) -> Option<PhpClassName> {
if let Some(class_name) = &self.class_name {
return Some(class_name.clone());
} else if let Some(service_name) = &self.service_name {
if let Some((_, token)) = store.get_service_definition(service_name) {
if let TokenData::DrupalServiceDefinition(service) = &token.data {
return Some(service.class.clone());
}
}
}
None
}
}
impl TryFrom<&str> for PhpMethod {
type Error = &'static str;
fn try_from(value: &str) -> Result<Self, Self::Error> {
if let Some((class, method)) = value.trim_matches(['\'', '\\']).split_once("::") {
return Ok(Self {
name: method.to_string(),
class_name: Some(PhpClassName::from(class)),
service_name: None,
});
}
Err("Unable to convert string to PhpMethod")
}
}
#[derive(Debug)]
pub struct DrupalRoute {
pub name: String,
pub path: String,
pub _defaults: DrupalRouteDefaults,
}
impl DrupalRoute {
pub fn get_route_parameters(&self) -> Vec<&str> {
let re = Regex::new(r"\{([^{}]+)\}");
match re {
Ok(re) => re
.captures_iter(&self.path)
.map(|c| c.get(1).unwrap().as_str())
.collect(),
Err(_) => vec![],
}
}
}
#[derive(Debug)]
pub struct DrupalRouteDefaults {
pub _controller: Option<PhpMethod>,
pub _form: Option<PhpClassName>,
pub _entity_form: Option<String>,
pub _title: Option<String>,
}
#[derive(Debug)]
pub struct DrupalService {
pub name: String,
pub class: PhpClassName,
}
#[derive(Debug)]
pub struct DrupalHook {
pub name: String,
pub parameters: Option<String>,
}
#[derive(Debug)]
pub struct DrupalPermission {
pub name: String,
pub title: String,
}
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum DrupalPluginType {
EntityType,
QueueWorker,
FieldType,
DataType,
FormElement,
RenderElement,
}
impl TryFrom<&str> for DrupalPluginType {
type Error = &'static str;
fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"ContentEntityType" | "ConfigEntityType" => Ok(DrupalPluginType::EntityType),
"QueueWorker" => Ok(DrupalPluginType::QueueWorker),
"FieldType" => Ok(DrupalPluginType::FieldType),
"DataType" => Ok(DrupalPluginType::DataType),
"FormElement" => Ok(DrupalPluginType::FormElement),
"RenderElement" => Ok(DrupalPluginType::RenderElement),
_ => Err("Unable to convert string to DrupalPluginType"),
}
}
}
impl fmt::Display for DrupalPluginType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
#[derive(Debug)]
pub struct DrupalPlugin {
pub plugin_type: DrupalPluginType,
pub plugin_id: String,
pub usage_example: Option<String>,
}
#[derive(Debug)]
pub struct DrupalPluginReference {
pub plugin_type: DrupalPluginType,
pub plugin_id: String,
}
#[derive(Debug)]
pub struct DrupalTranslationString {
pub string: String,
pub placeholders: Option<String>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn create_php_class_from_string() {
assert_eq!(
"Drupal\\test\\TestClass",
PhpClassName::from("Drupal\\test\\TestClass").to_string()
);
assert_eq!(
"Drupal\\test\\TestClass",
PhpClassName::from("\\Drupal\\test\\TestClass").to_string()
);
assert_eq!(
"Drupal\\test\\TestClass",
PhpClassName::from("'\\Drupal\\test\\TestClass\\'").to_string()
);
}
#[test]
fn create_php_method_from_string() {
assert_eq!(
"myMethod",
PhpMethod::try_from("Drupal\\test\\TestClass::myMethod")
.unwrap()
.name
);
assert_eq!(
"Drupal\\test\\TestClass",
PhpMethod::try_from("Drupal\\test\\TestClass::myMethod")
.unwrap()
.class_name
.unwrap()
.to_string()
);
assert_eq!(
"myMethod",
PhpMethod::try_from("'\\Drupal\\test\\TestClass::myMethod'")
.unwrap()
.name
);
assert_eq!(
"Drupal\\test\\TestClass",
PhpMethod::try_from("'\\Drupal\\test\\TestClass::myMethod'")
.unwrap()
.class_name
.unwrap()
.to_string()
);
assert!(PhpMethod::try_from("invalid class").is_err());
}
}