1
- pub mod printf {
1
+ pub ( crate ) mod printf {
2
2
use super :: strcursor:: StrCursor as Cur ;
3
3
use rustc_span:: InnerSpan ;
4
4
@@ -36,10 +36,10 @@ pub mod printf {
36
36
///
37
37
/// This ignores cases where the substitution does not have an exact equivalent, or where
38
38
/// the substitution would be unnecessary.
39
- pub fn translate ( & self ) -> Option < String > {
39
+ pub fn translate ( & self ) -> Result < String , Option < String > > {
40
40
match * self {
41
41
Substitution :: Format ( ref fmt) => fmt. translate ( ) ,
42
- Substitution :: Escape => None ,
42
+ Substitution :: Escape => Err ( None ) ,
43
43
}
44
44
}
45
45
}
@@ -68,9 +68,9 @@ pub mod printf {
68
68
impl Format < ' _ > {
69
69
/// Translate this directive into an equivalent Rust formatting directive.
70
70
///
71
- /// Returns `None ` in cases where the `printf` directive does not have an exact Rust
71
+ /// Returns `Err ` in cases where the `printf` directive does not have an exact Rust
72
72
/// equivalent, rather than guessing.
73
- pub fn translate ( & self ) -> Option < String > {
73
+ pub fn translate ( & self ) -> Result < String , Option < String > > {
74
74
use std:: fmt:: Write ;
75
75
76
76
let ( c_alt, c_zero, c_left, c_plus) = {
@@ -84,7 +84,12 @@ pub mod printf {
84
84
'0' => c_zero = true ,
85
85
'-' => c_left = true ,
86
86
'+' => c_plus = true ,
87
- _ => return None ,
87
+ _ => {
88
+ return Err ( Some ( format ! (
89
+ "the flag `{}` is unknown or unsupported" ,
90
+ c
91
+ ) ) ) ;
92
+ }
88
93
}
89
94
}
90
95
( c_alt, c_zero, c_left, c_plus)
@@ -104,7 +109,9 @@ pub mod printf {
104
109
let width = match self . width {
105
110
Some ( Num :: Next ) => {
106
111
// NOTE: Rust doesn't support this.
107
- return None ;
112
+ return Err ( Some (
113
+ "you have to use a positional or named parameter for the width" . to_string ( ) ,
114
+ ) ) ;
108
115
}
109
116
w @ Some ( Num :: Arg ( _) ) => w,
110
117
w @ Some ( Num :: Num ( _) ) => w,
@@ -125,13 +132,21 @@ pub mod printf {
125
132
"p" => ( Some ( self . type_ ) , false , true ) ,
126
133
"g" => ( Some ( "e" ) , true , false ) ,
127
134
"G" => ( Some ( "E" ) , true , false ) ,
128
- _ => return None ,
135
+ _ => {
136
+ return Err ( Some ( format ! (
137
+ "the conversion specifier `{}` is unknown or unsupported" ,
138
+ self . type_
139
+ ) ) ) ;
140
+ }
129
141
} ;
130
142
131
143
let ( fill, width, precision) = match ( is_int, width, precision) {
132
144
( true , Some ( _) , Some ( _) ) => {
133
145
// Rust can't duplicate this insanity.
134
- return None ;
146
+ return Err ( Some (
147
+ "width and precision cannot both be specified for integer conversions"
148
+ . to_string ( ) ,
149
+ ) ) ;
135
150
}
136
151
( true , None , Some ( p) ) => ( Some ( "0" ) , Some ( p) , None ) ,
137
152
( true , w, None ) => ( fill, w, None ) ,
@@ -169,7 +184,17 @@ pub mod printf {
169
184
s. push ( '{' ) ;
170
185
171
186
if let Some ( arg) = self . parameter {
172
- write ! ( s, "{}" , arg. checked_sub( 1 ) ?) . ok ( ) ?;
187
+ match write ! (
188
+ s,
189
+ "{}" ,
190
+ match arg. checked_sub( 1 ) {
191
+ Some ( a) => a,
192
+ None => return Err ( None ) ,
193
+ }
194
+ ) {
195
+ Err ( _) => return Err ( None ) ,
196
+ _ => { }
197
+ }
173
198
}
174
199
175
200
if has_options {
@@ -199,12 +224,18 @@ pub mod printf {
199
224
}
200
225
201
226
if let Some ( width) = width {
202
- width. translate ( & mut s) . ok ( ) ?;
227
+ match width. translate ( & mut s) {
228
+ Err ( _) => return Err ( None ) ,
229
+ _ => { }
230
+ }
203
231
}
204
232
205
233
if let Some ( precision) = precision {
206
234
s. push ( '.' ) ;
207
- precision. translate ( & mut s) . ok ( ) ?;
235
+ match precision. translate ( & mut s) {
236
+ Err ( _) => return Err ( None ) ,
237
+ _ => { }
238
+ }
208
239
}
209
240
210
241
if let Some ( type_) = type_ {
@@ -213,7 +244,7 @@ pub mod printf {
213
244
}
214
245
215
246
s. push ( '}' ) ;
216
- Some ( s)
247
+ Ok ( s)
217
248
}
218
249
}
219
250
@@ -623,11 +654,11 @@ pub mod shell {
623
654
}
624
655
}
625
656
626
- pub fn translate ( & self ) -> Option < String > {
657
+ pub fn translate ( & self ) -> Result < String , Option < String > > {
627
658
match * self {
628
- Substitution :: Ordinal ( n, _) => Some ( format ! ( "{{{}}}" , n) ) ,
629
- Substitution :: Name ( n, _) => Some ( format ! ( "{{{}}}" , n) ) ,
630
- Substitution :: Escape ( _) => None ,
659
+ Substitution :: Ordinal ( n, _) => Ok ( format ! ( "{{{}}}" , n) ) ,
660
+ Substitution :: Name ( n, _) => Ok ( format ! ( "{{{}}}" , n) ) ,
661
+ Substitution :: Escape ( _) => Err ( None ) ,
631
662
}
632
663
}
633
664
}
0 commit comments