@@ -2,6 +2,7 @@ use std::fmt::Display;
2
2
3
3
use serde:: { Deserialize , Serialize } ;
4
4
5
+ use crate :: diagnostics:: diagnostics_registry:: DIAGNOSTICS ;
5
6
use plc_ast:: ast:: AstNode ;
6
7
use plc_source:: {
7
8
source_location:: { SourceLocation , SourceLocationFactory } ,
@@ -88,8 +89,10 @@ impl Diagnostic {
88
89
self
89
90
}
90
91
91
- pub fn with_error_code ( mut self , error_code : & ' static str ) -> Self {
92
- self . error_code = error_code;
92
+ pub fn with_error_code ( mut self , code : & ' static str ) -> Self {
93
+ debug_assert ! ( DIAGNOSTICS . get( code) . is_some( ) , "Error {code} does not exist" ) ;
94
+
95
+ self . error_code = code;
93
96
self
94
97
}
95
98
@@ -199,12 +202,26 @@ impl Diagnostic {
199
202
. with_error_code ( "E006" )
200
203
}
201
204
202
- pub fn invalid_parameter_count ( expected : usize , received : usize , location : SourceLocation ) -> Diagnostic {
203
- Diagnostic :: new (
204
- format ! (
205
- "Invalid parameter count. Received {received} parameters while {expected} parameters were expected." ,
206
- ) ) . with_error_code ( "E032" )
207
- . with_location ( location)
205
+ pub fn invalid_argument_count < T > ( expected : usize , actual : usize , location : T ) -> Diagnostic
206
+ where
207
+ T : Into < SourceLocation > ,
208
+ {
209
+ // Let's be extra fancy here 🕺
210
+ fn message ( value : usize ) -> String {
211
+ match value {
212
+ 1 => format ! ( "{value} argument" ) ,
213
+ _ => format ! ( "{value} arguments" ) ,
214
+ }
215
+ }
216
+
217
+ Diagnostic :: new ( format ! (
218
+ "this POU takes {expected} but {actual} {was_or_were} supplied" ,
219
+ expected = message( expected) ,
220
+ actual = message( actual) ,
221
+ was_or_were = if actual == 1 { "was" } else { "were" }
222
+ ) )
223
+ . with_error_code ( "E032" )
224
+ . with_location ( location. into ( ) )
208
225
}
209
226
210
227
pub fn unknown_type ( type_name : & str , location : SourceLocation ) -> Diagnostic {
0 commit comments