@@ -22,62 +22,6 @@ pub enum NewlineStyle {
2222 Native ,
2323}
2424
25- impl NewlineStyle {
26- fn auto_detect ( raw_input_text : & str ) -> NewlineStyle {
27- if let Some ( pos) = raw_input_text. find ( '\n' ) {
28- let pos = pos. saturating_sub ( 1 ) ;
29- if let Some ( '\r' ) = raw_input_text. chars ( ) . nth ( pos) {
30- NewlineStyle :: Windows
31- } else {
32- NewlineStyle :: Unix
33- }
34- } else {
35- NewlineStyle :: Native
36- }
37- }
38-
39- fn native ( ) -> NewlineStyle {
40- if cfg ! ( windows) {
41- NewlineStyle :: Windows
42- } else {
43- NewlineStyle :: Unix
44- }
45- }
46-
47- /// Apply this newline style to the formatted text. When the style is set
48- /// to `Auto`, the `raw_input_text` is used to detect the existing line
49- /// endings.
50- ///
51- /// If the style is set to `Auto` and `raw_input_text` contains no
52- /// newlines, the `Native` style will be used.
53- pub ( crate ) fn apply ( self , formatted_text : & mut String , raw_input_text : & str ) {
54- use crate :: NewlineStyle :: * ;
55- let mut style = self ;
56- if style == Auto {
57- style = Self :: auto_detect ( raw_input_text) ;
58- }
59- if style == Native {
60- style = Self :: native ( ) ;
61- }
62- match style {
63- Windows => {
64- let mut transformed = String :: with_capacity ( 2 * formatted_text. capacity ( ) ) ;
65- for c in formatted_text. chars ( ) {
66- match c {
67- '\n' => transformed. push_str ( "\r \n " ) ,
68- '\r' => continue ,
69- c => transformed. push ( c) ,
70- }
71- }
72- * formatted_text = transformed;
73- }
74- Unix => return ,
75- Native => unreachable ! ( "NewlineStyle::Native" ) ,
76- Auto => unreachable ! ( "NewlineStyle::Auto" ) ,
77- }
78- }
79- }
80-
8125#[ config_type]
8226/// Where to put the opening brace of items (`fn`, `impl`, etc.).
8327pub enum BraceStyle {
@@ -412,59 +356,3 @@ impl Edition {
412356 }
413357 }
414358}
415-
416- #[ test]
417- fn test_newline_style_auto_detect ( ) {
418- let lf = "One\n Two\n Three" ;
419- let crlf = "One\r \n Two\r \n Three" ;
420- let none = "One Two Three" ;
421-
422- assert_eq ! ( NewlineStyle :: Unix , NewlineStyle :: auto_detect( lf) ) ;
423- assert_eq ! ( NewlineStyle :: Windows , NewlineStyle :: auto_detect( crlf) ) ;
424- assert_eq ! ( NewlineStyle :: Native , NewlineStyle :: auto_detect( none) ) ;
425- }
426-
427- #[ test]
428- fn test_newline_style_auto_apply ( ) {
429- let auto = NewlineStyle :: Auto ;
430-
431- let formatted_text = "One\n Two\n Three" ;
432- let raw_input_text = "One\n Two\n Three" ;
433-
434- let mut out = String :: from ( formatted_text) ;
435- auto. apply ( & mut out, raw_input_text) ;
436- assert_eq ! ( "One\n Two\n Three" , & out, "auto should detect 'lf'" ) ;
437-
438- let formatted_text = "One\n Two\n Three" ;
439- let raw_input_text = "One\r \n Two\r \n Three" ;
440-
441- let mut out = String :: from ( formatted_text) ;
442- auto. apply ( & mut out, raw_input_text) ;
443- assert_eq ! ( "One\r \n Two\r \n Three" , & out, "auto should detect 'crlf'" ) ;
444-
445- #[ cfg( not( windows) ) ]
446- {
447- let formatted_text = "One\n Two\n Three" ;
448- let raw_input_text = "One Two Three" ;
449-
450- let mut out = String :: from ( formatted_text) ;
451- auto. apply ( & mut out, raw_input_text) ;
452- assert_eq ! (
453- "One\n Two\n Three" , & out,
454- "auto-native-unix should detect 'lf'"
455- ) ;
456- }
457-
458- #[ cfg( windows) ]
459- {
460- let formatted_text = "One\n Two\n Three" ;
461- let raw_input_text = "One Two Three" ;
462-
463- let mut out = String :: from ( formatted_text) ;
464- auto. apply ( & mut out, raw_input_text) ;
465- assert_eq ! (
466- "One\r \n Two\r \n Three" , & out,
467- "auto-native-windows should detect 'crlf'"
468- ) ;
469- }
470- }
0 commit comments