@@ -38,7 +38,7 @@ use core::marker::PhantomData;
3838/// Error codes returned by the merve lexer.
3939#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
4040pub enum LexerError {
41- Todo ,
41+ EmptySource ,
4242 UnexpectedParen ,
4343 UnexpectedBrace ,
4444 UnterminatedParen ,
@@ -60,7 +60,7 @@ impl LexerError {
6060 #[ must_use]
6161 pub fn from_code ( code : i32 ) -> Self {
6262 match code {
63- 0 => Self :: Todo ,
63+ 0 => Self :: EmptySource ,
6464 1 => Self :: UnexpectedParen ,
6565 2 => Self :: UnexpectedBrace ,
6666 3 => Self :: UnterminatedParen ,
@@ -81,7 +81,7 @@ impl LexerError {
8181 #[ must_use]
8282 pub fn as_str ( & self ) -> & ' static str {
8383 match self {
84- Self :: Todo => "todo " ,
84+ Self :: EmptySource => "empty source " ,
8585 Self :: UnexpectedParen => "unexpected parenthesis" ,
8686 Self :: UnexpectedBrace => "unexpected brace" ,
8787 Self :: UnterminatedParen => "unterminated parenthesis" ,
@@ -169,11 +169,7 @@ impl<'a> Analysis<'a> {
169169 return None ;
170170 }
171171 let line = unsafe { ffi:: merve_get_export_line ( self . handle , index) } ;
172- if line == 0 {
173- None
174- } else {
175- Some ( line)
176- }
172+ if line == 0 { None } else { Some ( line) }
177173 }
178174
179175 /// Get the module specifier of the re-export at `index`.
@@ -197,11 +193,7 @@ impl<'a> Analysis<'a> {
197193 return None ;
198194 }
199195 let line = unsafe { ffi:: merve_get_reexport_line ( self . handle , index) } ;
200- if line == 0 {
201- None
202- } else {
203- Some ( line)
204- }
196+ if line == 0 { None } else { Some ( line) }
205197 }
206198
207199 /// Iterate over all named exports.
@@ -319,29 +311,25 @@ impl ExactSizeIterator for ExportIter<'_, '_> {}
319311/// assert_eq!(analysis.export_name(0), Some("hello"));
320312/// ```
321313pub fn parse_commonjs ( source : & str ) -> Result < Analysis < ' _ > , LexerError > {
322- // Rust uses a dangling pointer (0x1) for empty slices, which is not
323- // dereferenceable. Pass NULL instead; the C API handles NULL as "".
324- let ( ptr, len) = if source. is_empty ( ) {
325- ( core:: ptr:: null ( ) , 0 )
326- } else {
327- ( source. as_ptr ( ) . cast ( ) , source. len ( ) )
328- } ;
329- let handle = unsafe { ffi:: merve_parse_commonjs ( ptr, len) } ;
314+ if source. is_empty ( ) {
315+ return Err ( LexerError :: EmptySource ) ;
316+ }
317+ let handle = unsafe { ffi:: merve_parse_commonjs ( source. as_ptr ( ) . cast ( ) , source. len ( ) ) } ;
330318 if handle. is_null ( ) {
331319 // NULL means allocation failure; map to a generic error
332320 let code = unsafe { ffi:: merve_get_last_error ( ) } ;
333321 return Err ( if code >= 0 {
334322 LexerError :: from_code ( code)
335323 } else {
336- LexerError :: Todo
324+ LexerError :: Unknown ( code )
337325 } ) ;
338326 }
339327 if !unsafe { ffi:: merve_is_valid ( handle) } {
340328 let code = unsafe { ffi:: merve_get_last_error ( ) } ;
341329 let err = if code >= 0 {
342330 LexerError :: from_code ( code)
343331 } else {
344- LexerError :: Todo
332+ LexerError :: Unknown ( code )
345333 } ;
346334 unsafe { ffi:: merve_free ( handle) } ;
347335 return Err ( err) ;
@@ -444,9 +432,9 @@ mod tests {
444432
445433 #[ test]
446434 fn empty_input ( ) {
447- let analysis = parse_commonjs ( "" ) . expect ( "empty should parse ") ;
448- assert_eq ! ( analysis . exports_count ( ) , 0 ) ;
449- assert_eq ! ( analysis . reexports_count ( ) , 0 ) ;
435+ let result = parse_commonjs ( "" ) ;
436+ assert ! ( result . is_err ( ) ) ;
437+ assert_eq ! ( result . unwrap_err ( ) , LexerError :: EmptySource ) ;
450438 }
451439
452440 #[ test]
0 commit comments