@@ -117,21 +117,28 @@ impl<'src> Token<'src> {
117117 }
118118
119119 #[ must_use]
120- pub fn is_compatible_with ( self , other : & Self ) -> bool {
120+ pub fn is_compatible_with ( self , other : Self ) -> bool {
121121 if self . code ( ) != other. code ( ) {
122122 return false ;
123123 }
124124
125- match ( self , other) {
126- ( Self :: Code { .. } , Self :: Code { .. } )
127- | ( Self :: Interpolation { .. } , Self :: Interpolation { .. } )
128- | ( Self :: Text { .. } , Self :: Text { .. } ) => true ,
129- ( Self :: CodeLine { closed, .. } , Self :: CodeLine { closed : other, .. } )
130- | ( Self :: InterpolationLine { closed, .. } , Self :: InterpolationLine { closed : other, .. } ) => {
131- closed == * other
125+ if self . block ( ) != other. block ( ) {
126+ for token in [ self , other] {
127+ if !matches ! ( token, Self :: Code { .. } | Self :: CodeLine { .. } ) {
128+ return false ;
129+ }
130+ }
131+ }
132+
133+ if let Self :: InterpolationLine { closed, .. } = self {
134+ if let Self :: InterpolationLine { closed : other, .. } = other {
135+ if closed != other {
136+ return false ;
137+ }
132138 }
133- _ => false ,
134139 }
140+
141+ true
135142 }
136143
137144 #[ must_use]
@@ -148,6 +155,148 @@ impl<'src> Token<'src> {
148155mod tests {
149156 use { super :: * , pretty_assertions:: assert_eq, Token :: * } ;
150157
158+ #[ test]
159+ fn compatibility ( ) {
160+ #[ track_caller]
161+ fn case ( a : Token , b : Token ) {
162+ assert ! ( a. is_compatible_with( b) ) ;
163+ }
164+ case (
165+ Text {
166+ contents : "foo" ,
167+ index : 0 ,
168+ } ,
169+ Text {
170+ contents : "bar" ,
171+ index : 1 ,
172+ } ,
173+ ) ;
174+ case ( Code { contents : "foo" } , Code { contents : "foo" } ) ;
175+ case ( Code { contents : " foo" } , Code { contents : "foo" } ) ;
176+ case ( Code { contents : "foo " } , Code { contents : "foo" } ) ;
177+ case (
178+ CodeLine {
179+ contents : "foo" ,
180+ closed : true ,
181+ } ,
182+ CodeLine {
183+ contents : "foo" ,
184+ closed : true ,
185+ } ,
186+ ) ;
187+ case (
188+ CodeLine {
189+ contents : "foo" ,
190+ closed : false ,
191+ } ,
192+ CodeLine {
193+ contents : "foo" ,
194+ closed : false ,
195+ } ,
196+ ) ;
197+ case (
198+ CodeLine {
199+ contents : "foo" ,
200+ closed : true ,
201+ } ,
202+ CodeLine {
203+ contents : "foo" ,
204+ closed : false ,
205+ } ,
206+ ) ;
207+ case (
208+ CodeLine {
209+ contents : "foo" ,
210+ closed : false ,
211+ } ,
212+ CodeLine {
213+ contents : "foo" ,
214+ closed : true ,
215+ } ,
216+ ) ;
217+ case (
218+ Code { contents : "foo" } ,
219+ CodeLine {
220+ contents : "foo" ,
221+ closed : true ,
222+ } ,
223+ ) ;
224+ case (
225+ CodeLine {
226+ contents : "foo" ,
227+ closed : true ,
228+ } ,
229+ Code { contents : "foo" } ,
230+ ) ;
231+ case (
232+ Interpolation { contents : "foo" } ,
233+ Interpolation { contents : "foo" } ,
234+ ) ;
235+ case (
236+ InterpolationLine {
237+ contents : "foo" ,
238+ closed : true ,
239+ } ,
240+ InterpolationLine {
241+ contents : "foo" ,
242+ closed : true ,
243+ } ,
244+ ) ;
245+ case (
246+ InterpolationLine {
247+ contents : "foo" ,
248+ closed : false ,
249+ } ,
250+ InterpolationLine {
251+ contents : "foo" ,
252+ closed : false ,
253+ } ,
254+ ) ;
255+ }
256+
257+ #[ test]
258+ fn incompatibility ( ) {
259+ #[ track_caller]
260+ fn case ( a : Token , b : Token ) {
261+ assert ! ( !a. is_compatible_with( b) ) ;
262+ }
263+ case (
264+ Text {
265+ contents : "foo" ,
266+ index : 0 ,
267+ } ,
268+ Code { contents : "bar" } ,
269+ ) ;
270+ case ( Code { contents : "foo" } , Interpolation { contents : "bar" } ) ;
271+ case (
272+ Interpolation { contents : "foo" } ,
273+ InterpolationLine {
274+ contents : "bar" ,
275+ closed : false ,
276+ } ,
277+ ) ;
278+ case (
279+ InterpolationLine {
280+ contents : "foo" ,
281+ closed : true ,
282+ } ,
283+ InterpolationLine {
284+ contents : "bar" ,
285+ closed : true ,
286+ } ,
287+ ) ;
288+ case (
289+ InterpolationLine {
290+ contents : "foo" ,
291+ closed : true ,
292+ } ,
293+ InterpolationLine {
294+ contents : "foo" ,
295+ closed : false ,
296+ } ,
297+ ) ;
298+ }
299+
151300 #[ track_caller]
152301 fn assert_parse ( expected : & str , expected_tokens : & [ Token ] ) {
153302 let actual_tokens = Token :: parse ( expected) . unwrap ( ) ;
0 commit comments