@@ -10,9 +10,6 @@ use ruff_source_file::Locator;
1010use ruff_text_size:: { Ranged , TextRange } ;
1111
1212use crate :: expression:: expr_f_string:: f_string_quoting;
13- use crate :: other:: f_string:: FormatFString ;
14- use crate :: other:: string_literal:: StringLiteralKind ;
15- use crate :: prelude:: * ;
1613use crate :: string:: Quoting ;
1714
1815/// Represents any kind of string expression. This could be either a string,
@@ -46,6 +43,10 @@ impl<'a> AnyString<'a> {
4643 }
4744 }
4845
46+ pub ( crate ) const fn is_fstring ( self ) -> bool {
47+ matches ! ( self , Self :: FString ( _) )
48+ }
49+
4950 /// Returns the quoting to be used for this string.
5051 pub ( super ) fn quoting ( self , locator : & Locator < ' _ > ) -> Quoting {
5152 match self {
@@ -54,23 +55,21 @@ impl<'a> AnyString<'a> {
5455 }
5556 }
5657
57- /// Returns a vector of all the [`AnyStringPart`] of this string.
58- pub ( super ) fn parts ( self , quoting : Quoting ) -> AnyStringPartsIter < ' a > {
58+ /// Returns an iterator over the [`AnyStringPart`]s of this string.
59+ pub ( super ) fn parts ( self ) -> AnyStringPartsIter < ' a > {
5960 match self {
6061 Self :: String ( ExprStringLiteral { value, .. } ) => {
6162 AnyStringPartsIter :: String ( value. iter ( ) )
6263 }
6364 Self :: Bytes ( ExprBytesLiteral { value, .. } ) => AnyStringPartsIter :: Bytes ( value. iter ( ) ) ,
64- Self :: FString ( ExprFString { value, .. } ) => {
65- AnyStringPartsIter :: FString ( value. iter ( ) , quoting)
66- }
65+ Self :: FString ( ExprFString { value, .. } ) => AnyStringPartsIter :: FString ( value. iter ( ) ) ,
6766 }
6867 }
6968
7069 pub ( crate ) fn is_multiline ( self , source : & str ) -> bool {
7170 match self {
7271 AnyString :: String ( _) | AnyString :: Bytes ( _) => {
73- self . parts ( Quoting :: default ( ) )
72+ self . parts ( )
7473 . next ( )
7574 . is_some_and ( |part| part. flags ( ) . is_triple_quoted ( ) )
7675 && memchr2 ( b'\n' , b'\r' , source[ self . range ( ) ] . as_bytes ( ) ) . is_some ( )
@@ -139,36 +138,20 @@ impl<'a> From<&'a ExprFString> for AnyString<'a> {
139138pub ( super ) enum AnyStringPartsIter < ' a > {
140139 String ( std:: slice:: Iter < ' a , StringLiteral > ) ,
141140 Bytes ( std:: slice:: Iter < ' a , ast:: BytesLiteral > ) ,
142- FString ( std:: slice:: Iter < ' a , ast:: FStringPart > , Quoting ) ,
141+ FString ( std:: slice:: Iter < ' a , ast:: FStringPart > ) ,
143142}
144143
145144impl < ' a > Iterator for AnyStringPartsIter < ' a > {
146145 type Item = AnyStringPart < ' a > ;
147146
148147 fn next ( & mut self ) -> Option < Self :: Item > {
149148 let part = match self {
150- Self :: String ( inner) => {
151- let part = inner. next ( ) ?;
152- AnyStringPart :: String {
153- part,
154- layout : StringLiteralKind :: String ,
155- }
156- }
149+ Self :: String ( inner) => AnyStringPart :: String ( inner. next ( ) ?) ,
157150 Self :: Bytes ( inner) => AnyStringPart :: Bytes ( inner. next ( ) ?) ,
158- Self :: FString ( inner, quoting) => {
159- let part = inner. next ( ) ?;
160- match part {
161- ast:: FStringPart :: Literal ( string_literal) => AnyStringPart :: String {
162- part : string_literal,
163- #[ allow( deprecated) ]
164- layout : StringLiteralKind :: InImplicitlyConcatenatedFString ( * quoting) ,
165- } ,
166- ast:: FStringPart :: FString ( f_string) => AnyStringPart :: FString {
167- part : f_string,
168- quoting : * quoting,
169- } ,
170- }
171- }
151+ Self :: FString ( inner) => match inner. next ( ) ? {
152+ ast:: FStringPart :: Literal ( string_literal) => AnyStringPart :: String ( string_literal) ,
153+ ast:: FStringPart :: FString ( f_string) => AnyStringPart :: FString ( f_string) ,
154+ } ,
172155 } ;
173156
174157 Some ( part)
@@ -183,53 +166,37 @@ impl FusedIterator for AnyStringPartsIter<'_> {}
183166/// This is constructed from the [`AnyString::parts`] method on [`AnyString`].
184167#[ derive( Clone , Debug ) ]
185168pub ( super ) enum AnyStringPart < ' a > {
186- String {
187- part : & ' a ast:: StringLiteral ,
188- layout : StringLiteralKind ,
189- } ,
169+ String ( & ' a ast:: StringLiteral ) ,
190170 Bytes ( & ' a ast:: BytesLiteral ) ,
191- FString {
192- part : & ' a ast:: FString ,
193- quoting : Quoting ,
194- } ,
171+ FString ( & ' a ast:: FString ) ,
195172}
196173
197174impl AnyStringPart < ' _ > {
198175 fn flags ( & self ) -> AnyStringFlags {
199176 match self {
200- Self :: String { part, .. } => part. flags . into ( ) ,
177+ Self :: String ( part) => part. flags . into ( ) ,
201178 Self :: Bytes ( bytes_literal) => bytes_literal. flags . into ( ) ,
202- Self :: FString { part, .. } => part. flags . into ( ) ,
179+ Self :: FString ( part) => part. flags . into ( ) ,
203180 }
204181 }
205182}
206183
207184impl < ' a > From < & AnyStringPart < ' a > > for AnyNodeRef < ' a > {
208185 fn from ( value : & AnyStringPart < ' a > ) -> Self {
209186 match value {
210- AnyStringPart :: String { part, .. } => AnyNodeRef :: StringLiteral ( part) ,
187+ AnyStringPart :: String ( part) => AnyNodeRef :: StringLiteral ( part) ,
211188 AnyStringPart :: Bytes ( part) => AnyNodeRef :: BytesLiteral ( part) ,
212- AnyStringPart :: FString { part, .. } => AnyNodeRef :: FString ( part) ,
189+ AnyStringPart :: FString ( part) => AnyNodeRef :: FString ( part) ,
213190 }
214191 }
215192}
216193
217194impl Ranged for AnyStringPart < ' _ > {
218195 fn range ( & self ) -> TextRange {
219196 match self {
220- Self :: String { part, .. } => part. range ( ) ,
197+ Self :: String ( part) => part. range ( ) ,
221198 Self :: Bytes ( part) => part. range ( ) ,
222- Self :: FString { part, .. } => part. range ( ) ,
223- }
224- }
225- }
226-
227- impl Format < PyFormatContext < ' _ > > for AnyStringPart < ' _ > {
228- fn fmt ( & self , f : & mut PyFormatter ) -> FormatResult < ( ) > {
229- match self {
230- AnyStringPart :: String { part, layout } => part. format ( ) . with_options ( * layout) . fmt ( f) ,
231- AnyStringPart :: Bytes ( bytes_literal) => bytes_literal. format ( ) . fmt ( f) ,
232- AnyStringPart :: FString { part, quoting } => FormatFString :: new ( part, * quoting) . fmt ( f) ,
199+ Self :: FString ( part) => part. range ( ) ,
233200 }
234201 }
235202}
0 commit comments