1+ use crate :: formatter:: { error, warning} ;
2+ use code0_definition_reader:: reader:: ParsableDefinition ;
13use std:: cmp:: PartialEq ;
24use std:: path:: Path ;
35use std:: process:: exit;
4- use code0_definition_reader:: reader:: ParsableDefinition ;
5- use crate :: formatter:: { error, warning} ;
66
77#[ derive( Default ) ]
88pub struct Reporter {
9- diagnose : Vec < Diagnose >
9+ diagnose : Vec < Diagnose > ,
1010}
1111
1212impl PartialEq for Severity {
@@ -35,13 +35,11 @@ impl PartialEq for Severity {
3535}
3636
3737impl Reporter {
38-
3938 pub fn add_report ( & mut self , diagnose : Diagnose ) {
4039 self . diagnose . push ( diagnose) ;
4140 }
4241
43- pub fn run_report ( & self ) {
44-
42+ pub fn run_report ( & self , will_exit : bool ) {
4543 for error in & self . get_errors ( ) {
4644 println ! ( "{}" , error. print( ) ) ;
4745 }
@@ -50,30 +48,37 @@ impl Reporter {
5048 println ! ( "{}" , warning. print( ) ) ;
5149 }
5250
53- if !self . get_errors ( ) . is_empty ( ) {
51+ if !self . get_errors ( ) . is_empty ( ) && will_exit {
5452 exit ( 1 )
5553 }
56-
5754 }
5855
5956 pub fn get_errors ( & self ) -> Vec < & Diagnose > {
60- self . diagnose . iter ( ) . filter ( |p| p. kind . severity ( ) == Severity :: Error ) . collect ( )
57+ self . diagnose
58+ . iter ( )
59+ . filter ( |p| p. kind . severity ( ) == Severity :: Error )
60+ . collect ( )
6161 }
6262
6363 pub fn get_warnings ( & self ) -> Vec < & Diagnose > {
64- self . diagnose . iter ( ) . filter ( |p| p. kind . severity ( ) == Severity :: Warning ) . collect ( )
64+ self . diagnose
65+ . iter ( )
66+ . filter ( |p| p. kind . severity ( ) == Severity :: Warning )
67+ . collect ( )
6568 }
6669
6770 pub fn get_debug ( & self ) -> Vec < & Diagnose > {
68- self . diagnose . iter ( ) . filter ( |p| p. kind . severity ( ) == Severity :: Debug ) . collect ( )
71+ self . diagnose
72+ . iter ( )
73+ . filter ( |p| p. kind . severity ( ) == Severity :: Debug )
74+ . collect ( )
6975 }
70-
7176}
7277
7378pub enum Severity {
7479 Error ,
7580 Warning ,
76- Debug
81+ Debug ,
7782}
7883
7984pub struct Diagnose {
@@ -90,7 +95,7 @@ pub enum DiagnosticKind {
9095 DuplicateRuntimeParameterIdentifier { identifier : String } ,
9196 UndefinedDataTypeIdentifier { identifier : String } ,
9297 EmptyGenericMapper ,
93- GenericKeyNotInMappingTarget { key : String , target : String } ,
98+ GenericKeyNotInMappingTarget { key : String , target : String } ,
9499 NullField { field_name : String } ,
95100 ForbiddenVariant ,
96101 UnusedGenericKey { key : String } ,
@@ -120,8 +125,11 @@ impl DiagnosticKind {
120125}
121126
122127impl Diagnose {
123-
124- pub fn new ( definition_name : String , definition : ParsableDefinition , kind : DiagnosticKind ) -> Self {
128+ pub fn new (
129+ definition_name : String ,
130+ definition : ParsableDefinition ,
131+ kind : DiagnosticKind ,
132+ ) -> Self {
125133 Self {
126134 definition_name,
127135 definition,
@@ -130,7 +138,6 @@ impl Diagnose {
130138 }
131139
132140 pub fn print ( & self ) -> String {
133-
134141 let path = format ! (
135142 "{}:{}:{}" ,
136143 Path :: new( & self . definition. path. clone( ) . unwrap_or_default( ) ) . display( ) ,
@@ -140,32 +147,94 @@ impl Diagnose {
140147
141148 use DiagnosticKind :: * ;
142149 match & self . kind {
143- EmptyGenericMapper =>
144- error ( format ! ( "`{}` defined a generic_type but its mapper are empty!`" , self . definition_name) , & path) ,
145- DeserializationError { description } =>
146- error ( format ! ( "A JSON paring error occurred: `{}`" , description) , & path) ,
147- GenericKeyNotInMappingTarget { key, target} =>
148- error ( format ! ( "`{}` is mapping the key: {} onto the target: {}. But the target did not define this generic_key!" , self . definition_name, key, target) , & path) ,
149- DuplicateDataTypeIdentifier { identifier } =>
150- error ( format ! ( "The data_type `{}` is already defined resulting in a duplicate!" , identifier) , & path) ,
151- DuplicateFlowTypeIdentifier { identifier } =>
152- error ( format ! ( "The flow_type `{}` is already defined resulting in a duplicate!" , identifier) , & path) ,
153- DuplicateRuntimeFunctionIdentifier { identifier } =>
154- error ( format ! ( "The runtime_function `{}` is already defined resulting in a duplicate!" , identifier) , & path) ,
155- DuplicateRuntimeParameterIdentifier { identifier } =>
156- error ( format ! ( "The runtime_parameter `{}` is already defined resulting in a duplicate!" , identifier) , & path) ,
157- UndefinedDataTypeIdentifier { identifier } =>
158- error ( format ! ( "`{}` uses an undefined data_type_identifier: `{}`!" , self . definition_name, identifier) , & path) ,
159- NullField { field_name } =>
160- error ( format ! ( "`{}` has a field (`{}`) that is null!" , self . definition_name, field_name) , & path) ,
161- ForbiddenVariant =>
162- error ( format ! ( "The data_type variant of `{}` is 0 and thus incorrect!" , self . definition_name) , & path) ,
163- UnusedGenericKey { key } =>
164- error ( format ! ( "`{}` defined a generic_key (`{}`) that is never used!" , self . definition_name, key) , & path) ,
165- UndefinedGenericKey { key } =>
166- error ( format ! ( "`{}` uses a generic_key (`{}`) that's not defined!" , self . definition_name, key) , & path) ,
167- UndefinedTranslation { translation_field } =>
168- warning ( format ! ( "`{}` has an empty field (`{}`) of translations!" , self . definition_name, translation_field) , & path) ,
150+ EmptyGenericMapper => error (
151+ format ! (
152+ "`{}` defined a generic_type but its mapper are empty!`" ,
153+ self . definition_name
154+ ) ,
155+ & path,
156+ ) ,
157+ DeserializationError { description } => error (
158+ format ! ( "A JSON paring error occurred: `{}`" , description) ,
159+ & path,
160+ ) ,
161+ GenericKeyNotInMappingTarget { key, target } => error (
162+ format ! (
163+ "`{}` is mapping the key: {} onto the target: {}. But the target did not define this generic_key!" ,
164+ self . definition_name, key, target
165+ ) ,
166+ & path,
167+ ) ,
168+ DuplicateDataTypeIdentifier { identifier } => error (
169+ format ! (
170+ "The data_type `{}` is already defined resulting in a duplicate!" ,
171+ identifier
172+ ) ,
173+ & path,
174+ ) ,
175+ DuplicateFlowTypeIdentifier { identifier } => error (
176+ format ! (
177+ "The flow_type `{}` is already defined resulting in a duplicate!" ,
178+ identifier
179+ ) ,
180+ & path,
181+ ) ,
182+ DuplicateRuntimeFunctionIdentifier { identifier } => error (
183+ format ! (
184+ "The runtime_function `{}` is already defined resulting in a duplicate!" ,
185+ identifier
186+ ) ,
187+ & path,
188+ ) ,
189+ DuplicateRuntimeParameterIdentifier { identifier } => error (
190+ format ! (
191+ "The runtime_parameter `{}` is already defined resulting in a duplicate!" ,
192+ identifier
193+ ) ,
194+ & path,
195+ ) ,
196+ UndefinedDataTypeIdentifier { identifier } => error (
197+ format ! (
198+ "`{}` uses an undefined data_type_identifier: `{}`!" ,
199+ self . definition_name, identifier
200+ ) ,
201+ & path,
202+ ) ,
203+ NullField { field_name } => error (
204+ format ! (
205+ "`{}` has a field (`{}`) that is null!" ,
206+ self . definition_name, field_name
207+ ) ,
208+ & path,
209+ ) ,
210+ ForbiddenVariant => error (
211+ format ! (
212+ "The data_type variant of `{}` is 0 and thus incorrect!" ,
213+ self . definition_name
214+ ) ,
215+ & path,
216+ ) ,
217+ UnusedGenericKey { key } => error (
218+ format ! (
219+ "`{}` defined a generic_key (`{}`) that is never used!" ,
220+ self . definition_name, key
221+ ) ,
222+ & path,
223+ ) ,
224+ UndefinedGenericKey { key } => error (
225+ format ! (
226+ "`{}` uses a generic_key (`{}`) that's not defined!" ,
227+ self . definition_name, key
228+ ) ,
229+ & path,
230+ ) ,
231+ UndefinedTranslation { translation_field } => warning (
232+ format ! (
233+ "`{}` has an empty field (`{}`) of translations!" ,
234+ self . definition_name, translation_field
235+ ) ,
236+ & path,
237+ ) ,
169238 }
170239 }
171240}
0 commit comments