11//! Adapter for the Syntect syntax highlighter plugin.
22
3- use crate :: adapters:: SyntaxHighlighterAdapter ;
4- use crate :: html;
3+ use std:: borrow:: Cow ;
54use std:: collections:: { hash_map, HashMap } ;
65use std:: fmt:: { self , Write } ;
76use syntect:: easy:: HighlightLines ;
@@ -13,6 +12,9 @@ use syntect::parsing::{SyntaxReference, SyntaxSet};
1312use syntect:: util:: LinesWithEndings ;
1413use syntect:: Error ;
1514
15+ use crate :: adapters:: SyntaxHighlighterAdapter ;
16+ use crate :: html;
17+
1618#[ derive( Debug ) ]
1719/// Syntect syntax highlighter plugin.
1820pub struct SyntectAdapter {
@@ -103,10 +105,10 @@ impl SyntaxHighlighterAdapter for SyntectAdapter {
103105 }
104106 }
105107
106- fn write_pre_tag (
108+ fn write_pre_tag < ' s > (
107109 & self ,
108110 output : & mut dyn Write ,
109- attributes : HashMap < & ' static str , String > ,
111+ attributes : HashMap < & ' static str , Cow < ' s , str > > ,
110112 ) -> fmt:: Result {
111113 match & self . theme {
112114 Some ( theme) => {
@@ -129,29 +131,29 @@ impl SyntaxHighlighterAdapter for SyntectAdapter {
129131 }
130132 }
131133
132- fn write_code_tag (
134+ fn write_code_tag < ' s > (
133135 & self ,
134136 output : & mut dyn Write ,
135- attributes : HashMap < & ' static str , String > ,
137+ attributes : HashMap < & ' static str , Cow < ' s , str > > ,
136138 ) -> fmt:: Result {
137139 html:: write_opening_tag ( output, "code" , attributes)
138140 }
139141}
140142
141- struct SyntectPreAttributes {
143+ struct SyntectPreAttributes < ' s > {
142144 syntect_style : String ,
143- attributes : HashMap < & ' static str , String > ,
145+ attributes : HashMap < & ' static str , Cow < ' s , str > > ,
144146}
145147
146- impl SyntectPreAttributes {
147- fn new ( attributes : HashMap < & ' static str , String > , syntect_style : & str ) -> Self {
148+ impl < ' s > SyntectPreAttributes < ' s > {
149+ fn new ( attributes : HashMap < & ' static str , Cow < ' s , str > > , syntect_style : & str ) -> Self {
148150 Self {
149151 syntect_style : syntect_style. into ( ) ,
150152 attributes,
151153 }
152154 }
153155
154- fn iter_mut ( & mut self ) -> SyntectPreAttributesIter < ' _ > {
156+ fn iter_mut ( & mut self ) -> SyntectPreAttributesIter < ' _ , ' s > {
155157 SyntectPreAttributesIter {
156158 iter_mut : self . attributes . iter_mut ( ) ,
157159 syntect_style : & self . syntect_style ,
@@ -160,20 +162,20 @@ impl SyntectPreAttributes {
160162 }
161163}
162164
163- struct SyntectPreAttributesIter < ' a > {
164- iter_mut : hash_map:: IterMut < ' a , & ' static str , String > ,
165+ struct SyntectPreAttributesIter < ' a , ' s > {
166+ iter_mut : hash_map:: IterMut < ' a , & ' static str , Cow < ' s , str > > ,
165167 syntect_style : & ' a str ,
166168 style_written : bool ,
167169}
168170
169- impl < ' a > Iterator for SyntectPreAttributesIter < ' a > {
171+ impl < ' a , ' s > Iterator for SyntectPreAttributesIter < ' a , ' s > {
170172 type Item = ( & ' a str , & ' a str ) ;
171173
172174 fn next ( & mut self ) -> Option < Self :: Item > {
173175 match self . iter_mut . next ( ) {
174176 Some ( ( k, v) ) if * k == "style" && !self . style_written => {
175177 self . style_written = true ;
176- v. insert_str ( 0 , self . syntect_style ) ;
178+ v. to_mut ( ) . insert_str ( 0 , self . syntect_style ) ;
177179 Some ( ( k, v) )
178180 }
179181 Some ( ( k, v) ) => Some ( ( k, v) ) ,
0 commit comments