@@ -343,7 +343,7 @@ struct TokenHandler<'a, 'tcx, F: Write> {
343343 /// We need to keep the `Class` for each element because it could contain a `Span` which is
344344 /// used to generate links.
345345 href_context : Option < HrefContext < ' a , ' tcx > > ,
346- write_line_number : fn ( u32 ) -> String ,
346+ write_line_number : fn ( u32 ) -> LineNumber ,
347347 line : u32 ,
348348 max_lines : u32 ,
349349}
@@ -355,7 +355,7 @@ impl<F: Write> std::fmt::Debug for TokenHandler<'_, '_, F> {
355355}
356356
357357impl < ' a , F : Write > TokenHandler < ' a , ' _ , F > {
358- fn handle_backline ( & mut self ) -> Option < String > {
358+ fn handle_backline ( & mut self ) -> Option < LineNumber > {
359359 self . line += 1 ;
360360 if self . line < self . max_lines {
361361 return Some ( ( self . write_line_number ) ( self . line ) ) ;
@@ -376,8 +376,7 @@ impl<'a, F: Write> TokenHandler<'a, '_, F> {
376376 if text == "\n "
377377 && let Some ( backline) = self . handle_backline ( )
378378 {
379- self . out . write_str ( & text) . unwrap ( ) ;
380- self . out . write_str ( & backline) . unwrap ( ) ;
379+ write ! ( self . out, "{text}{backline}" ) . unwrap ( ) ;
381380 } else {
382381 self . push_token_without_backline_check ( class, text, true ) ;
383382 }
@@ -437,20 +436,27 @@ impl<F: Write> Drop for TokenHandler<'_, '_, F> {
437436 }
438437}
439438
440- fn scraped_line_number ( line : u32 ) -> String {
441- // https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag#data-nosnippet-attr
442- // Do not show "1 2 3 4 5 ..." in web search results.
443- format ! ( "<span data-nosnippet>{line}</span>" )
439+ /// Represents line numbers to be generated as HTML.
440+ enum LineNumber {
441+ /// Used for scraped code examples.
442+ Scraped ( u32 ) ,
443+ /// Used for non-scraped code examples, ie source code pages and code examples in documentation.
444+ Normal ( u32 ) ,
445+ /// Code examples without line numbers.
446+ #[ allow( dead_code) ]
447+ Empty ( u32 ) ,
444448}
445449
446- fn line_number ( line : u32 ) -> String {
447- // https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag#data-nosnippet-attr
448- // Do not show "1 2 3 4 5 ..." in web search results.
449- format ! ( "<a href=#{line} id={line} data-nosnippet>{line}</a>" )
450- }
451-
452- fn empty_line_number ( _: u32 ) -> String {
453- String :: new ( )
450+ impl Display for LineNumber {
451+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
452+ match self {
453+ // https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag#data-nosnippet-attr
454+ // Do not show "1 2 3 4 5 ..." in web search results.
455+ Self :: Scraped ( line) => write ! ( f, "<span data-nosnippet>{line}</span>" ) ,
456+ Self :: Normal ( line) => write ! ( f, "<a href=#{line} id={line} data-nosnippet>{line}</a>" ) ,
457+ Self :: Empty ( _) => Ok ( ( ) ) ,
458+ }
459+ }
454460}
455461
456462fn get_next_expansion (
@@ -537,12 +543,12 @@ pub(super) fn write_code(
537543 write_line_number : match line_info {
538544 Some ( line_info) => {
539545 if line_info. is_scraped_example {
540- scraped_line_number
546+ LineNumber :: Scraped
541547 } else {
542- line_number
548+ LineNumber :: Normal
543549 }
544550 }
545- None => empty_line_number ,
551+ None => LineNumber :: Empty ,
546552 } ,
547553 line : 0 ,
548554 max_lines : u32:: MAX ,
@@ -552,8 +558,12 @@ pub(super) fn write_code(
552558 if let Some ( line_info) = line_info {
553559 token_handler. line = line_info. start_line - 1 ;
554560 token_handler. max_lines = line_info. max_lines ;
555- if let Some ( text) = token_handler. handle_backline ( ) {
556- token_handler. push_token_without_backline_check ( None , Cow :: Owned ( text) , false ) ;
561+ if let Some ( backline) = token_handler. handle_backline ( ) {
562+ token_handler. push_token_without_backline_check (
563+ None ,
564+ Cow :: Owned ( backline. to_string ( ) ) ,
565+ false ,
566+ ) ;
557567 }
558568 }
559569
0 commit comments