@@ -86,8 +86,14 @@ impl From<FStringError>
8686}
8787
8888enum ParseState {
89- Text { content : String } ,
90- FormattedValue { expression : String , depth : usize } ,
89+ Text {
90+ content : String ,
91+ } ,
92+ FormattedValue {
93+ expression : String ,
94+ spec : Option < String > ,
95+ depth : usize ,
96+ } ,
9197}
9298
9399pub fn parse_fstring ( source : & str ) -> Result < ast:: StringGroup , FStringError > {
@@ -114,6 +120,7 @@ pub fn parse_fstring(source: &str) -> Result<ast::StringGroup, FStringError> {
114120
115121 FormattedValue {
116122 expression : String :: new ( ) ,
123+ spec : None ,
117124 depth : 0 ,
118125 }
119126 }
@@ -135,17 +142,28 @@ pub fn parse_fstring(source: &str) -> Result<ast::StringGroup, FStringError> {
135142
136143 FormattedValue {
137144 mut expression,
145+ mut spec,
138146 depth,
139147 } => match ch {
148+ ':' if depth == 0 => FormattedValue {
149+ expression,
150+ spec : Some ( String :: new ( ) ) ,
151+ depth,
152+ } ,
140153 '{' => {
141154 if let Some ( '{' ) = chars. peek ( ) {
142155 expression. push_str ( "{{" ) ;
143156 chars. next ( ) ;
144- FormattedValue { expression, depth }
157+ FormattedValue {
158+ expression,
159+ spec,
160+ depth,
161+ }
145162 } else {
146163 expression. push ( '{' ) ;
147164 FormattedValue {
148165 expression,
166+ spec,
149167 depth : depth + 1 ,
150168 }
151169 }
@@ -154,28 +172,42 @@ pub fn parse_fstring(source: &str) -> Result<ast::StringGroup, FStringError> {
154172 if let Some ( '}' ) = chars. peek ( ) {
155173 expression. push_str ( "}}" ) ;
156174 chars. next ( ) ;
157- FormattedValue { expression, depth }
175+ FormattedValue {
176+ expression,
177+ spec,
178+ depth,
179+ }
158180 } else if depth > 0 {
159181 expression. push ( '}' ) ;
160182 FormattedValue {
161183 expression,
184+ spec,
162185 depth : depth - 1 ,
163186 }
164187 } else {
165188 values. push ( ast:: StringGroup :: FormattedValue {
166- value : Box :: new ( match parse_expression ( dbg ! ( expression. trim( ) ) ) {
189+ value : Box :: new ( match parse_expression ( expression. trim ( ) ) {
167190 Ok ( expr) => expr,
168191 Err ( _) => return Err ( FStringError :: InvalidExpression ) ,
169192 } ) ,
193+ spec : spec. unwrap_or_default ( ) ,
170194 } ) ;
171195 Text {
172196 content : String :: new ( ) ,
173197 }
174198 }
175199 }
176200 _ => {
177- expression. push ( ch) ;
178- FormattedValue { expression, depth }
201+ if let Some ( spec) = spec. as_mut ( ) {
202+ spec. push ( ch)
203+ } else {
204+ expression. push ( ch) ;
205+ }
206+ FormattedValue {
207+ expression,
208+ spec,
209+ depth,
210+ }
179211 }
180212 } ,
181213 } ;
@@ -617,10 +649,12 @@ mod tests {
617649 ast:: StringGroup :: Joined {
618650 values: vec![
619651 ast:: StringGroup :: FormattedValue {
620- value: Box :: new( mk_ident( "a" ) )
652+ value: Box :: new( mk_ident( "a" ) ) ,
653+ spec: String :: new( ) ,
621654 } ,
622655 ast:: StringGroup :: FormattedValue {
623- value: Box :: new( mk_ident( "b" ) )
656+ value: Box :: new( mk_ident( "b" ) ) ,
657+ spec: String :: new( ) ,
624658 } ,
625659 ast:: StringGroup :: Constant {
626660 value: "{foo}" . to_owned( )
0 commit comments