rustpython_ruff_python_ast/
str_prefix.rs1use ruff_text_size::TextSize;
2
3use std::fmt;
4
5#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, is_macro::Is)]
10pub enum StringLiteralPrefix {
11 Empty,
13
14 Unicode,
22
23 Raw { uppercase: bool },
26}
27
28impl StringLiteralPrefix {
29 pub const fn as_str(self) -> &'static str {
31 match self {
32 Self::Empty => "",
33 Self::Unicode => "u",
34 Self::Raw { uppercase: true } => "R",
35 Self::Raw { uppercase: false } => "r",
36 }
37 }
38
39 pub const fn text_len(self) -> TextSize {
40 match self {
41 Self::Empty => TextSize::new(0),
42 Self::Unicode | Self::Raw { .. } => TextSize::new(1),
43 }
44 }
45}
46
47impl fmt::Display for StringLiteralPrefix {
48 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49 f.write_str(self.as_str())
50 }
51}
52
53#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
55pub enum FStringPrefix {
56 Regular,
58
59 Raw { uppercase_r: bool },
62}
63
64impl FStringPrefix {
65 pub const fn as_str(self) -> &'static str {
67 match self {
68 Self::Regular => "f",
69 Self::Raw { uppercase_r: true } => "Rf",
70 Self::Raw { uppercase_r: false } => "rf",
71 }
72 }
73
74 pub const fn text_len(self) -> TextSize {
75 match self {
76 Self::Regular => TextSize::new(1),
77 Self::Raw { .. } => TextSize::new(2),
78 }
79 }
80
81 pub const fn is_raw(self) -> bool {
84 matches!(self, Self::Raw { .. })
85 }
86}
87
88impl fmt::Display for FStringPrefix {
89 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90 f.write_str(self.as_str())
91 }
92}
93
94#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
96pub enum TStringPrefix {
97 Regular,
99
100 Raw { uppercase_r: bool },
103}
104
105impl TStringPrefix {
106 pub const fn as_str(self) -> &'static str {
108 match self {
109 Self::Regular => "t",
110 Self::Raw { uppercase_r: true } => "Rt",
111 Self::Raw { uppercase_r: false } => "rt",
112 }
113 }
114
115 pub const fn text_len(self) -> TextSize {
116 match self {
117 Self::Regular => TextSize::new(1),
118 Self::Raw { .. } => TextSize::new(2),
119 }
120 }
121
122 pub const fn is_raw(self) -> bool {
125 matches!(self, Self::Raw { .. })
126 }
127}
128
129impl fmt::Display for TStringPrefix {
130 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
131 f.write_str(self.as_str())
132 }
133}
134
135#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
137pub enum ByteStringPrefix {
138 Regular,
140
141 Raw { uppercase_r: bool },
144}
145
146impl ByteStringPrefix {
147 pub const fn as_str(self) -> &'static str {
149 match self {
150 Self::Regular => "b",
151 Self::Raw { uppercase_r: true } => "Rb",
152 Self::Raw { uppercase_r: false } => "rb",
153 }
154 }
155
156 pub const fn text_len(self) -> TextSize {
157 match self {
158 Self::Regular => TextSize::new(1),
159 Self::Raw { .. } => TextSize::new(2),
160 }
161 }
162
163 pub const fn is_raw(self) -> bool {
166 matches!(self, Self::Raw { .. })
167 }
168}
169
170impl fmt::Display for ByteStringPrefix {
171 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
172 f.write_str(self.as_str())
173 }
174}
175
176#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, is_macro::Is)]
188pub enum AnyStringPrefix {
189 Bytes(ByteStringPrefix),
191
192 Format(FStringPrefix),
194
195 Template(TStringPrefix),
197
198 Regular(StringLiteralPrefix),
200}
201
202impl AnyStringPrefix {
203 pub const fn as_str(self) -> &'static str {
204 match self {
205 Self::Regular(regular_prefix) => regular_prefix.as_str(),
206 Self::Bytes(bytestring_prefix) => bytestring_prefix.as_str(),
207 Self::Format(fstring_prefix) => fstring_prefix.as_str(),
208 Self::Template(tstring_prefix) => tstring_prefix.as_str(),
209 }
210 }
211
212 pub const fn text_len(self) -> TextSize {
213 match self {
214 Self::Regular(regular_prefix) => regular_prefix.text_len(),
215 Self::Bytes(bytestring_prefix) => bytestring_prefix.text_len(),
216 Self::Format(fstring_prefix) => fstring_prefix.text_len(),
217 Self::Template(tstring_prefix) => tstring_prefix.text_len(),
218 }
219 }
220
221 pub const fn is_raw(self) -> bool {
222 match self {
223 Self::Regular(regular_prefix) => regular_prefix.is_raw(),
224 Self::Bytes(bytestring_prefix) => bytestring_prefix.is_raw(),
225 Self::Format(fstring_prefix) => fstring_prefix.is_raw(),
226 Self::Template(tstring_prefix) => tstring_prefix.is_raw(),
227 }
228 }
229}
230
231impl fmt::Display for AnyStringPrefix {
232 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
233 f.write_str(self.as_str())
234 }
235}
236
237impl Default for AnyStringPrefix {
238 fn default() -> Self {
239 Self::Regular(StringLiteralPrefix::Empty)
240 }
241}