-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.rs
More file actions
218 lines (200 loc) · 8.6 KB
/
Copy pathparser.rs
File metadata and controls
218 lines (200 loc) · 8.6 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
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use tucana::shared::{
DefinitionDataType, FlowType, FunctionDefinition, Module, ModuleConfigurationDefinition,
RuntimeFlowType, RuntimeFunctionDefinition, Translation,
};
use crate::reader::{Meta, MetaType, Reader};
#[derive(Serialize, Clone, Debug)]
pub struct DefinitionError {
pub definition: String,
pub definition_type: crate::reader::MetaType,
pub path: String,
pub error: String,
}
#[derive(Debug)]
pub struct Parser {
pub modules: Vec<DefinitionModule>,
}
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct ModuleConfiguration {
pub identifier: String,
pub name: Vec<Translation>,
pub description: Vec<Translation>,
pub documentation: String,
pub author: String,
pub icon: String,
#[serde(default, skip_serializing_if = "String::is_empty")]
pub version: String,
}
#[derive(Serialize, Clone, Debug, Default)]
pub struct DefinitionModule {
// Configuration of module `module.json`
pub config: ModuleConfiguration,
// DataTypes of module `/data_types`
pub data_types: Vec<DefinitionDataType>,
// FlowTypes of module `/flow_types`
pub flow_types: Vec<FlowType>,
// RuntimeFlowTypes of module `/runtime_flow_types`
pub runtime_flow_types: Vec<RuntimeFlowType>,
// Functions of module `/functions`
pub functions: Vec<FunctionDefinition>,
// RuntimeFunctions of module `/runtime_functions`
pub runtime_functions: Vec<RuntimeFunctionDefinition>,
// Settings of module `/configurations`
pub module_configs: Vec<ModuleConfigurationDefinition>,
// Errors found while parsing
pub errors: Vec<DefinitionError>,
}
impl DefinitionModule {
pub fn into_module(self) -> Module {
Module {
identifier: self.config.identifier,
name: self.config.name,
description: self.config.description,
documentation: self.config.documentation,
author: self.config.author,
icon: self.config.icon,
version: self.config.version,
flow_types: self.flow_types,
runtime_flow_types: self.runtime_flow_types,
function_definitions: self.functions,
runtime_function_definitions: self.runtime_functions,
definition_data_types: self.data_types,
configurations: self.module_configs,
definitions: Vec::new(),
}
}
}
impl Parser {
pub fn from_path(path: &str) -> Option<Self> {
let reader = Reader::from_path(path)?;
Some(Self::from_reader(reader))
}
pub fn from_reader(reader: Reader) -> Self {
let mut modules: Vec<DefinitionModule> = vec![];
let mut module_indices_by_name: HashMap<String, usize> = HashMap::new();
for meta in &reader.meta {
let module_index = if let Some(index) = module_indices_by_name.get(&meta.name) {
*index
} else {
let mut new_mod = DefinitionModule::default();
Parser::append_meta(&mut new_mod, meta);
modules.push(new_mod);
let new_index = modules.len() - 1;
module_indices_by_name.insert(meta.name.clone(), new_index);
continue;
};
Parser::append_meta(&mut modules[module_index], meta);
}
Parser { modules }
}
pub fn extract_identifier(definition: &str, meta_type: MetaType) -> String {
let field_name = match meta_type {
MetaType::RuntimeFunction | MetaType::Function => "runtime_name",
_ => "identifier",
};
// Look for the field pattern: "field_name": "value" or "field_name":"value"
if let Some(start) = definition.find(&format!("\"{field_name}\"")) {
// Find the colon after the field name
if let Some(colon_pos) = definition[start..].find(':') {
let after_colon = &definition[start + colon_pos + 1..];
// Skip whitespace and find the opening quote
let trimmed = after_colon.trim_start();
if let Some(stripped) = trimmed.strip_prefix('"') {
// Find the closing quote
if let Some(end_quote) = stripped.find('"') {
return trimmed[1..end_quote + 1].to_string();
}
}
}
}
// Fallback: return the whole definition if identifier can't be extracted
definition.to_string()
}
fn append_meta(feature: &mut DefinitionModule, meta: &Meta) {
let definition = meta.definition_string.as_str();
match meta.r#type {
MetaType::DataType => match serde_json::from_str::<DefinitionDataType>(definition) {
Ok(data_type) => feature.data_types.push(data_type),
Err(err) => feature.errors.push(DefinitionError {
definition: Parser::extract_identifier(definition, MetaType::DataType),
definition_type: MetaType::DataType,
path: meta.path.clone(),
error: err.to_string(),
}),
},
MetaType::FlowType => match serde_json::from_str::<FlowType>(definition) {
Ok(flow_type) => feature.flow_types.push(flow_type),
Err(err) => feature.errors.push(DefinitionError {
definition: Parser::extract_identifier(definition, MetaType::FlowType),
definition_type: MetaType::FlowType,
path: meta.path.clone(),
error: err.to_string(),
}),
},
MetaType::RuntimeFunction => {
match serde_json::from_str::<RuntimeFunctionDefinition>(definition) {
Ok(func) => feature.runtime_functions.push(func),
Err(err) => feature.errors.push(DefinitionError {
definition: Parser::extract_identifier(
definition,
MetaType::RuntimeFunction,
),
definition_type: MetaType::RuntimeFunction,
path: meta.path.clone(),
error: err.to_string(),
}),
}
}
MetaType::RuntimeFlowType => {
match serde_json::from_str::<RuntimeFlowType>(definition) {
Ok(v) => feature.runtime_flow_types.push(v),
Err(err) => feature.errors.push(DefinitionError {
definition: Parser::extract_identifier(
definition,
MetaType::RuntimeFlowType,
),
definition_type: MetaType::RuntimeFlowType,
path: meta.path.clone(),
error: err.to_string(),
}),
}
}
MetaType::Function => match serde_json::from_str::<FunctionDefinition>(definition) {
Ok(v) => feature.functions.push(v),
Err(err) => feature.errors.push(DefinitionError {
definition: Parser::extract_identifier(definition, MetaType::Function),
definition_type: MetaType::Function,
path: meta.path.clone(),
error: err.to_string(),
}),
},
MetaType::Configs => {
match serde_json::from_str::<ModuleConfigurationDefinition>(definition) {
Ok(v) => feature.module_configs.push(v),
Err(err) => feature.errors.push(DefinitionError {
definition: Parser::extract_identifier(definition, MetaType::Configs),
definition_type: MetaType::Configs,
path: meta.path.clone(),
error: err.to_string(),
}),
}
}
MetaType::ModuleDefinition => {
match serde_json::from_str::<ModuleConfiguration>(definition) {
Ok(v) => feature.config = v,
Err(err) => feature.errors.push(DefinitionError {
definition: Parser::extract_identifier(
definition,
MetaType::ModuleDefinition,
),
definition_type: MetaType::ModuleDefinition,
path: meta.path.clone(),
error: err.to_string(),
}),
}
}
}
}
}