Skip to content

Commit b2716de

Browse files
committed
feat: correct file parsing
1 parent 248a865 commit b2716de

8 files changed

Lines changed: 249 additions & 68 deletions

File tree

crates/cli/src/analyser/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::{analyser::index_identifier::IdentifierIndex, reader::Meta};
21
use crate::diagnostics::diagnose::Diagnose;
32
use crate::diagnostics::kinds::DiagnosticKind;
43
use crate::diagnostics::reporter::Reporter;
4+
use crate::{analyser::index_identifier::IdentifierIndex, reader::Meta};
55
use tucana::shared::{DefinitionDataType, FlowType, RuntimeFunctionDefinition};
66

77
#[derive(Clone)]

crates/cli/src/command/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub mod download;
2-
pub mod search_module;
2+
pub mod parse_errors;
33
pub mod push;
44
pub mod report;
55
pub mod search;
6+
pub mod search_module;
67
pub mod watch;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use crate::formatter::{error, error_without_trace};
2+
use crate::parser::Parser;
3+
4+
pub fn fail_on_parser_errors(parser: &Parser) {
5+
let mut total_errors = 0usize;
6+
7+
for module in &parser.modules {
8+
for parse_error in &module.errors {
9+
total_errors += 1;
10+
println!(
11+
"{}",
12+
error(
13+
format!(
14+
"Failed to parse {} `{}`: {}",
15+
parse_error.definition_type, parse_error.definition, parse_error.error
16+
),
17+
&parse_error.path,
18+
)
19+
);
20+
}
21+
}
22+
23+
if total_errors > 0 {
24+
error_without_trace(format!(
25+
"Found {} definition parse error(s). Fix the files above and retry.",
26+
total_errors
27+
));
28+
std::process::exit(1);
29+
}
30+
}

crates/cli/src/command/report.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use tucana::shared::Module;
22

33
use crate::analyser::core::Analyser;
4+
use crate::command::parse_errors::fail_on_parser_errors;
45
use crate::formatter::{success, success_table};
56
use crate::parser::Parser;
67
use crate::table::summary_table;
@@ -14,6 +15,7 @@ pub fn report_errors(path: Option<String>) {
1415
panic!("Error reading definitions");
1516
}
1617
};
18+
fail_on_parser_errors(&parser);
1719

1820
let mut analyser = Analyser::new(dir_path.as_str());
1921
analyser.report(true, true);

crates/cli/src/command/search.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::command::parse_errors::fail_on_parser_errors;
12
use crate::formatter::{info, success};
23
use crate::parser::Parser;
34
use colored::Colorize;
@@ -11,6 +12,7 @@ pub fn search_definition(name: String, path: Option<String>) {
1112
panic!("Error reading definitions");
1213
}
1314
};
15+
fail_on_parser_errors(&parser);
1416

1517
search_and_display_definitions(&name, &parser);
1618
}

crates/cli/src/command/search_module.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use tucana::shared::Module;
22

33
use crate::analyser::core::Analyser;
4+
use crate::command::parse_errors::fail_on_parser_errors;
45
use crate::formatter::{success, success_table};
56
use crate::parser::{DefinitionModule, Parser};
67
use crate::table::{module_table, summary_table};
@@ -14,6 +15,7 @@ pub fn search_module(name: Option<String>, path: Option<String>) {
1415
panic!("Error reading definitions");
1516
}
1617
};
18+
fail_on_parser_errors(&parser);
1719

1820
let mut analyser = Analyser::new(dir_path.as_str());
1921
analyser.report(true, true);

crates/cli/src/parser.rs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use serde::Serialize;
1+
use serde::{Deserialize, Serialize};
2+
use std::collections::HashMap;
23
use tucana::shared::{
34
DefinitionDataType, FlowType, FunctionDefinition, Module, ModuleConfigurationDefinition,
45
RuntimeFlowType, RuntimeFunctionDefinition, Translation,
@@ -10,6 +11,7 @@ use crate::reader::{Meta, MetaType, Reader};
1011
pub struct DefinitionError {
1112
pub definition: String,
1213
pub definition_type: crate::reader::MetaType,
14+
pub path: String,
1315
pub error: String,
1416
}
1517

@@ -18,14 +20,15 @@ pub struct Parser {
1820
pub modules: Vec<DefinitionModule>,
1921
}
2022

21-
#[derive(Serialize, Clone, Debug, Default)]
23+
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
2224
pub struct ModuleConfiguration {
2325
pub identifier: String,
2426
pub name: Vec<Translation>,
2527
pub description: Vec<Translation>,
2628
pub documentation: String,
2729
pub author: String,
2830
pub icon: String,
31+
#[serde(default, skip_serializing_if = "String::is_empty")]
2932
pub version: String,
3033
}
3134

@@ -79,19 +82,21 @@ impl Parser {
7982

8083
pub fn from_reader(reader: Reader) -> Self {
8184
let mut modules: Vec<DefinitionModule> = vec![];
85+
let mut module_indices_by_name: HashMap<String, usize> = HashMap::new();
8286

8387
for meta in &reader.meta {
84-
let module = modules
85-
.iter_mut()
86-
.find(|m| m.config.identifier == meta.name);
87-
88-
if let Some(existing) = module {
89-
Parser::append_meta(existing, meta);
88+
let module_index = if let Some(index) = module_indices_by_name.get(&meta.name) {
89+
*index
9090
} else {
9191
let mut new_mod = DefinitionModule::default();
9292
Parser::append_meta(&mut new_mod, meta);
9393
modules.push(new_mod);
94-
}
94+
let new_index = modules.len() - 1;
95+
module_indices_by_name.insert(meta.name.clone(), new_index);
96+
continue;
97+
};
98+
99+
Parser::append_meta(&mut modules[module_index], meta);
95100
}
96101

97102
Parser { modules }
@@ -132,6 +137,7 @@ impl Parser {
132137
Err(err) => feature.errors.push(DefinitionError {
133138
definition: Parser::extract_identifier(definition, MetaType::DataType),
134139
definition_type: MetaType::DataType,
140+
path: meta.path.clone(),
135141
error: err.to_string(),
136142
}),
137143
},
@@ -140,6 +146,7 @@ impl Parser {
140146
Err(err) => feature.errors.push(DefinitionError {
141147
definition: Parser::extract_identifier(definition, MetaType::FlowType),
142148
definition_type: MetaType::FlowType,
149+
path: meta.path.clone(),
143150
error: err.to_string(),
144151
}),
145152
},
@@ -152,6 +159,7 @@ impl Parser {
152159
MetaType::RuntimeFunction,
153160
),
154161
definition_type: MetaType::RuntimeFunction,
162+
path: meta.path.clone(),
155163
error: err.to_string(),
156164
}),
157165
}
@@ -165,6 +173,7 @@ impl Parser {
165173
MetaType::RuntimeFlowType,
166174
),
167175
definition_type: MetaType::RuntimeFlowType,
176+
path: meta.path.clone(),
168177
error: err.to_string(),
169178
}),
170179
}
@@ -174,18 +183,31 @@ impl Parser {
174183
Err(err) => feature.errors.push(DefinitionError {
175184
definition: Parser::extract_identifier(definition, MetaType::Function),
176185
definition_type: MetaType::Function,
186+
path: meta.path.clone(),
177187
error: err.to_string(),
178188
}),
179189
},
180190
MetaType::Configs => {
181191
match serde_json::from_str::<ModuleConfigurationDefinition>(definition) {
182192
Ok(v) => feature.module_configs.push(v),
193+
Err(err) => feature.errors.push(DefinitionError {
194+
definition: Parser::extract_identifier(definition, MetaType::Configs),
195+
definition_type: MetaType::Configs,
196+
path: meta.path.clone(),
197+
error: err.to_string(),
198+
}),
199+
}
200+
}
201+
MetaType::ModuleDefinition => {
202+
match serde_json::from_str::<ModuleConfiguration>(definition) {
203+
Ok(v) => feature.config = v,
183204
Err(err) => feature.errors.push(DefinitionError {
184205
definition: Parser::extract_identifier(
185206
definition,
186-
MetaType::RuntimeFlowType,
207+
MetaType::ModuleDefinition,
187208
),
188-
definition_type: MetaType::RuntimeFlowType,
209+
definition_type: MetaType::ModuleDefinition,
210+
path: meta.path.clone(),
189211
error: err.to_string(),
190212
}),
191213
}

0 commit comments

Comments
 (0)