@@ -589,12 +589,14 @@ impl FormatString {
589589 Ok ( ( first_char, chars. as_str ( ) ) )
590590 }
591591
592- fn parse_literal ( text : & str ) -> Result < ( FormatPart , & str ) , FormatParseError > {
592+ fn parse_literal ( text : & str , is_raw : bool ) -> Result < ( FormatPart , & str ) , FormatParseError > {
593593 let mut cur_text = text;
594594 let mut result_string = String :: new ( ) ;
595595 let mut pending_escape = false ;
596596 while !cur_text. is_empty ( ) {
597- if pending_escape
597+ // Raw strings: \N{...} is literal, not a Unicode escape
598+ if !is_raw
599+ && pending_escape
598600 && let Some ( ( unicode_string, remaining) ) =
599601 FormatString :: parse_escaped_unicode_string ( cur_text)
600602 {
@@ -697,23 +699,12 @@ impl FormatString {
697699 ( & text[ ..end_idx] , & text[ end_idx..] )
698700 } )
699701 }
700- }
701-
702- pub trait FromTemplate < ' a > : Sized {
703- type Err ;
704- fn from_str ( s : & ' a str ) -> Result < Self , Self :: Err > ;
705- }
706702
707- impl < ' a > FromTemplate < ' a > for FormatString {
708- type Err = FormatParseError ;
709-
710- fn from_str ( text : & ' a str ) -> Result < Self , Self :: Err > {
703+ pub fn parse ( text : & str , is_raw : bool ) -> Result < Self , FormatParseError > {
711704 let mut cur_text: & str = text;
712705 let mut parts: Vec < FormatPart > = Vec :: new ( ) ;
713706 while !cur_text. is_empty ( ) {
714- // Try to parse both literals and bracketed format parts until we
715- // run out of text
716- cur_text = FormatString :: parse_literal ( cur_text)
707+ cur_text = FormatString :: parse_literal ( cur_text, is_raw)
717708 . or_else ( |_| FormatString :: parse_spec ( cur_text, AllowPlaceholderNesting :: Yes ) )
718709 . map ( |( part, new_text) | {
719710 parts. push ( part) ;
@@ -726,6 +717,19 @@ impl<'a> FromTemplate<'a> for FormatString {
726717 }
727718}
728719
720+ pub trait FromTemplate < ' a > : Sized {
721+ type Err ;
722+ fn from_str ( s : & ' a str ) -> Result < Self , Self :: Err > ;
723+ }
724+
725+ impl < ' a > FromTemplate < ' a > for FormatString {
726+ type Err = FormatParseError ;
727+
728+ fn from_str ( text : & ' a str ) -> Result < Self , Self :: Err > {
729+ FormatString :: parse ( text, false )
730+ }
731+ }
732+
729733#[ cfg( test) ]
730734mod tests {
731735 use super :: * ;
0 commit comments