1- pub mod printf {
1+ pub ( crate ) mod printf {
22 use super :: strcursor:: StrCursor as Cur ;
33 use rustc_span:: InnerSpan ;
44
@@ -36,10 +36,10 @@ pub mod printf {
3636 ///
3737 /// This ignores cases where the substitution does not have an exact equivalent, or where
3838 /// the substitution would be unnecessary.
39- pub fn translate ( & self ) -> Option < String > {
39+ pub fn translate ( & self ) -> Result < String , Option < String > > {
4040 match * self {
4141 Substitution :: Format ( ref fmt) => fmt. translate ( ) ,
42- Substitution :: Escape => None ,
42+ Substitution :: Escape => Err ( None ) ,
4343 }
4444 }
4545 }
@@ -68,9 +68,9 @@ pub mod printf {
6868 impl Format < ' _ > {
6969 /// Translate this directive into an equivalent Rust formatting directive.
7070 ///
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
7272 /// equivalent, rather than guessing.
73- pub fn translate ( & self ) -> Option < String > {
73+ pub fn translate ( & self ) -> Result < String , Option < String > > {
7474 use std:: fmt:: Write ;
7575
7676 let ( c_alt, c_zero, c_left, c_plus) = {
@@ -84,7 +84,12 @@ pub mod printf {
8484 '0' => c_zero = true ,
8585 '-' => c_left = true ,
8686 '+' => c_plus = true ,
87- _ => return None ,
87+ _ => {
88+ return Err ( Some ( format ! (
89+ "the flag `{}` is unknown or unsupported" ,
90+ c
91+ ) ) ) ;
92+ }
8893 }
8994 }
9095 ( c_alt, c_zero, c_left, c_plus)
@@ -104,7 +109,9 @@ pub mod printf {
104109 let width = match self . width {
105110 Some ( Num :: Next ) => {
106111 // 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+ ) ) ;
108115 }
109116 w @ Some ( Num :: Arg ( _) ) => w,
110117 w @ Some ( Num :: Num ( _) ) => w,
@@ -125,13 +132,21 @@ pub mod printf {
125132 "p" => ( Some ( self . type_ ) , false , true ) ,
126133 "g" => ( Some ( "e" ) , true , false ) ,
127134 "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+ }
129141 } ;
130142
131143 let ( fill, width, precision) = match ( is_int, width, precision) {
132144 ( true , Some ( _) , Some ( _) ) => {
133145 // 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+ ) ) ;
135150 }
136151 ( true , None , Some ( p) ) => ( Some ( "0" ) , Some ( p) , None ) ,
137152 ( true , w, None ) => ( fill, w, None ) ,
@@ -169,7 +184,17 @@ pub mod printf {
169184 s. push ( '{' ) ;
170185
171186 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+ }
173198 }
174199
175200 if has_options {
@@ -199,12 +224,18 @@ pub mod printf {
199224 }
200225
201226 if let Some ( width) = width {
202- width. translate ( & mut s) . ok ( ) ?;
227+ match width. translate ( & mut s) {
228+ Err ( _) => return Err ( None ) ,
229+ _ => { }
230+ }
203231 }
204232
205233 if let Some ( precision) = precision {
206234 s. push ( '.' ) ;
207- precision. translate ( & mut s) . ok ( ) ?;
235+ match precision. translate ( & mut s) {
236+ Err ( _) => return Err ( None ) ,
237+ _ => { }
238+ }
208239 }
209240
210241 if let Some ( type_) = type_ {
@@ -213,7 +244,7 @@ pub mod printf {
213244 }
214245
215246 s. push ( '}' ) ;
216- Some ( s)
247+ Ok ( s)
217248 }
218249 }
219250
@@ -623,11 +654,11 @@ pub mod shell {
623654 }
624655 }
625656
626- pub fn translate ( & self ) -> Option < String > {
657+ pub fn translate ( & self ) -> Result < String , Option < String > > {
627658 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 ) ,
631662 }
632663 }
633664 }
0 commit comments