@@ -24,7 +24,7 @@ use crate::token::Token;
2424
2525pub type ParseResult < T > = Result < T , ParseError > ;
2626
27- #[ derive( PartialEq ) ]
27+ #[ derive( Debug , PartialEq ) ]
2828pub enum ParseErrors {
2929 // Cost errors
3030 CostOverflow ,
@@ -149,9 +149,45 @@ impl fmt::Display for ParseError {
149149 }
150150}
151151
152- impl fmt:: Display for ParseErrors {
153- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
154- let message = match & self {
152+ impl error:: Error for ParseError {
153+ fn source ( & self ) -> Option < & ( dyn error:: Error + ' static ) > {
154+ None
155+ }
156+ }
157+
158+ impl From < ParseErrors > for ParseError {
159+ fn from ( err : ParseErrors ) -> Self {
160+ ParseError :: new ( err)
161+ }
162+ }
163+
164+ impl From < CostErrors > for ParseError {
165+ fn from ( err : CostErrors ) -> Self {
166+ match err {
167+ CostErrors :: CostOverflow => ParseError :: new ( ParseErrors :: CostOverflow ) ,
168+ CostErrors :: CostBalanceExceeded ( a, b) => {
169+ ParseError :: new ( ParseErrors :: CostBalanceExceeded ( a, b) )
170+ }
171+ CostErrors :: MemoryBalanceExceeded ( a, b) => {
172+ ParseError :: new ( ParseErrors :: MemoryBalanceExceeded ( a, b) )
173+ }
174+ CostErrors :: CostComputationFailed ( s) => {
175+ ParseError :: new ( ParseErrors :: CostComputationFailed ( s) )
176+ }
177+ CostErrors :: CostContractLoadFailure => ParseError :: new (
178+ ParseErrors :: CostComputationFailed ( "Failed to load cost contract" . into ( ) ) ,
179+ ) ,
180+ CostErrors :: InterpreterFailure | CostErrors :: Expect ( _) => {
181+ ParseError :: new ( ParseErrors :: InterpreterFailure )
182+ }
183+ CostErrors :: ExecutionTimeExpired => ParseError :: new ( ParseErrors :: ExecutionTimeExpired ) ,
184+ }
185+ }
186+ }
187+
188+ impl DiagnosableError for ParseErrors {
189+ fn message ( & self ) -> String {
190+ match & self {
155191 ParseErrors :: CostOverflow => "Used up cost budget during the parse" . into ( ) ,
156192 ParseErrors :: CostBalanceExceeded ( bal, used) => {
157193 format ! ( "Used up cost budget during the parse: {bal} balance, {used} used" )
@@ -259,89 +295,14 @@ impl fmt::Display for ParseErrors {
259295 }
260296 ParseErrors :: TupleValueExpected => "expected value expression for tuple" . into ( ) ,
261297 ParseErrors :: IllegalClarityName ( name) => format ! ( "illegal clarity name, '{name}'" ) ,
262- ParseErrors :: IllegalASCIIString ( s) => {
263- // Protect against console flooding and process hanging while running tests,
264- // using a purely arbitrary max chars limit.
265- #[ cfg( any( test, feature = "testing" ) ) ]
266- let s = shorten_string_for_test ( s, 100 ) ;
267-
268- format ! ( "illegal ascii string \" {s}\" " )
269- }
298+ ParseErrors :: IllegalASCIIString ( s) => format ! ( "illegal ascii string \" {s}\" " ) ,
270299 ParseErrors :: ExpectedWhitespace => "expected whitespace before expression" . into ( ) ,
271300 ParseErrors :: NoteToMatchThis ( token) => format ! ( "to match this '{token}'" ) ,
272301 ParseErrors :: UnexpectedParserFailure => "unexpected failure while parsing" . to_string ( ) ,
273302 ParseErrors :: InterpreterFailure => "unexpected failure while parsing" . to_string ( ) ,
274303 ParseErrors :: ExecutionTimeExpired => "max execution time expired" . to_string ( ) ,
275- } ;
276- write ! ( f, "{message}" )
277- }
278- }
279-
280- /// Test helper function to shorten big strings while running tests
281- ///
282- /// This prevents both:
283- /// - Console flooding with multi-megabyte output during test runs.
284- /// - Potential test process blocking or hanging due to stdout buffering limits.
285- ///
286- /// In case a the input `string` need to be shortned based on `max_chars`,
287- /// the resulting string will be ellipsed showing the original character count.
288- #[ cfg( any( test, feature = "testing" ) ) ]
289- fn shorten_string_for_test ( string : & str , max_chars : usize ) -> String {
290- let char_count = string. chars ( ) . count ( ) ;
291- if char_count <= max_chars {
292- string. into ( )
293- } else {
294- let shortened: String = string. chars ( ) . take ( max_chars) . collect ( ) ;
295- format ! ( "{shortened}...[{char_count}]" )
296- }
297- }
298-
299- impl fmt:: Debug for ParseErrors {
300- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
301- fmt:: Display :: fmt ( self , f)
302- }
303- }
304-
305- impl error:: Error for ParseError {
306- fn source ( & self ) -> Option < & ( dyn error:: Error + ' static ) > {
307- None
308- }
309- }
310-
311- impl From < ParseErrors > for ParseError {
312- fn from ( err : ParseErrors ) -> Self {
313- ParseError :: new ( err)
314- }
315- }
316-
317- impl From < CostErrors > for ParseError {
318- fn from ( err : CostErrors ) -> Self {
319- match err {
320- CostErrors :: CostOverflow => ParseError :: new ( ParseErrors :: CostOverflow ) ,
321- CostErrors :: CostBalanceExceeded ( a, b) => {
322- ParseError :: new ( ParseErrors :: CostBalanceExceeded ( a, b) )
323- }
324- CostErrors :: MemoryBalanceExceeded ( a, b) => {
325- ParseError :: new ( ParseErrors :: MemoryBalanceExceeded ( a, b) )
326- }
327- CostErrors :: CostComputationFailed ( s) => {
328- ParseError :: new ( ParseErrors :: CostComputationFailed ( s) )
329- }
330- CostErrors :: CostContractLoadFailure => ParseError :: new (
331- ParseErrors :: CostComputationFailed ( "Failed to load cost contract" . into ( ) ) ,
332- ) ,
333- CostErrors :: InterpreterFailure | CostErrors :: Expect ( _) => {
334- ParseError :: new ( ParseErrors :: InterpreterFailure )
335- }
336- CostErrors :: ExecutionTimeExpired => ParseError :: new ( ParseErrors :: ExecutionTimeExpired ) ,
337304 }
338305 }
339- }
340-
341- impl DiagnosableError for ParseErrors {
342- fn message ( & self ) -> String {
343- format ! ( "{self}" )
344- }
345306
346307 fn suggestion ( & self ) -> Option < String > {
347308 None
0 commit comments