rust-format 0.3.4

A Rust source code formatting crate with a unified interface for string, file, and TokenStream input
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
#![cfg(feature = "post_process")]

use std::borrow::Cow;
use std::{cmp, slice};

use crate::Error;

const BLANK_START: &[&[u8]] = &[b"lank_", b"!", b"("];
const BLANK_END: &[&[u8]] = &[b";"];
const COMMENT_START: &[&[u8]] = &[b"omment_", b"!", b"("];
const COMMENT_END: &[&[u8]] = &[b")", b";"];
const COMMENT_END2: &[&[u8]] = &[b";"];
const DOC_BLOCK_START: &[&[u8]] = &[b"[", b"doc", b"="];
const DOC_BLOCK_END: &[&[u8]] = &[b"]"];

const EMPTY_COMMENT: &str = "//";
const COMMENT: &str = "// ";
const DOC_COMMENT: &str = "///";
const LF_STR: &str = "\n";
const CRLF_STR: &str = "\r\n";

const CR: u8 = b'\r';
const LF: u8 = b'\n';

const MIN_BUFF_SIZE: usize = 128;

// In order to replace the markers there were a few options:
// 1. Create a full special purpose Rust lexer, replace the tokens we want as we go, write it back
// 2. Find the markers via regular string search, copy everything up to that point, replace, repeat
// 3. A hybrid of 1 and 2
//
// The problem with #1 is it is hugely overkill - we are only interested in 3 markers
// The problem with #2 is that it would find markers in strings and comments - likely not an issue, but it bothered me
// (and also we generalize the marker replacement code also for doc blocks, which someone could have commented out)
// #3 is what is below - it does basic lexing of Rust comments and strings for the purposes of skipping them only. It
// understands just enough to do the job. The weird part is it literally searches inside all other constructs, but the
// probability of a false positive while low in comments and strings, is likely very close to zero anywhere else, so
// I think this is a good compromise. Regardless, the user should be advised to not use `_comment_!(` or `_blank_!(`
// anywhere in the source file other than where they want markers.

struct CopyingCursor<'a> {
    start_idx: usize,
    curr_idx: usize,
    curr: u8,

    // We can iterate as if this were raw bytes since we are only matching ASCII. We preserve
    // any unicode, however, and copy it verbatim
    iter: slice::Iter<'a, u8>,
    source: &'a str,
    buffer: String,
}

impl<'a> CopyingCursor<'a> {
    fn new(source: &'a str) -> Option<Self> {
        // Better to be too large than not large enough
        let buffer = String::with_capacity(cmp::max(source.len() * 2, MIN_BUFF_SIZE));
        let mut iter = source.as_bytes().iter();

        iter.next().map(|&ch| Self {
            start_idx: 0,
            curr_idx: 0,
            curr: ch,
            iter,
            source,
            buffer,
        })
    }

    #[inline]
    fn next(&mut self) -> Option<u8> {
        self.iter.next().map(|&ch| {
            self.curr_idx += 1;
            self.curr = ch;
            ch
        })
    }

    #[inline]
    fn copy_to_marker(&mut self, marker: usize, new_start_idx: usize) {
        if marker > self.start_idx {
            // Copy exclusive of marker position
            self.buffer.push_str(&self.source[self.start_idx..marker]);
        }
        self.start_idx = new_start_idx;
    }

    fn into_buffer(mut self) -> Cow<'a, str> {
        // We have done some work
        if self.start_idx > 0 {
            // Last write to ensure everything is copied
            self.copy_to_marker(self.curr_idx + 1, self.curr_idx + 1);

            self.buffer.shrink_to_fit();
            Cow::Owned(self.buffer)
        // We have done nothing - just return original str
        } else {
            Cow::Borrowed(self.source)
        }
    }

    fn skip_block_comment(&mut self) {
        enum State {
            InComment,
            MaybeStarting,
            MaybeEnding,
        }

        let mut nest_level = 1;
        let mut state = State::InComment;

        while let Some(ch) = self.next() {
            match (ch, state) {
                (b'*', State::InComment) => {
                    state = State::MaybeEnding;
                }
                (b'/', State::MaybeEnding) => {
                    nest_level -= 1;
                    if nest_level == 0 {
                        break;
                    }
                    state = State::InComment;
                }
                (b'*', State::MaybeStarting) => {
                    nest_level += 1;
                    state = State::InComment;
                }
                (b'/', State::InComment) => {
                    state = State::MaybeStarting;
                }
                (_, _) => {
                    state = State::InComment;
                }
            }
        }
    }

    fn try_skip_comment(&mut self) -> bool {
        match self.next() {
            // Line comment of some form (we don't care which)
            Some(b'/') => {
                while let Some(ch) = self.next() {
                    if ch == b'\n' {
                        break;
                    }
                }

                true
            }
            // Block comment of some form (we don't care which)
            Some(b'*') => {
                self.skip_block_comment();
                true
            }
            // Not a comment or EOF, etc. - should be impossible in valid code
            _ => false,
        }
    }

    fn skip_string(&mut self) {
        let mut in_escape = false;

        while let Some(ch) = self.next() {
            match ch {
                b'"' if !in_escape => break,
                b'\\' if !in_escape => in_escape = true,
                _ if in_escape => in_escape = false,
                _ => {}
            }
        }
    }

    fn try_skip_raw_string(&mut self) -> bool {
        // First, match the entry sequence to the raw string and collect # of pads present
        let pads = match self.next() {
            Some(b'#') => {
                let mut pads = 1;

                while let Some(ch) = self.next() {
                    match ch {
                        b'#' => {
                            pads += 1;
                        }
                        b'"' => break,
                        // Not a raw string
                        _ => return false,
                    }
                }

                pads
            }
            Some(b'"') => 0,
            _ => return false,
        };

        #[derive(Clone, Copy)]
        enum State {
            InRawComment,
            MaybeEndingComment(i32),
        }

        let mut state = State::InRawComment;

        // Loop over the raw string looking for ending sequence and count pads until we have
        // the correct # of them
        while let Some(ch) = self.next() {
            match (ch, state) {
                (b'"', State::InRawComment) if pads == 0 => break,
                (b'"', State::InRawComment) => state = State::MaybeEndingComment(0),
                (b'#', State::MaybeEndingComment(pads_seen)) => {
                    let pads_seen = pads_seen + 1;
                    if pads_seen == pads {
                        break;
                    }
                    state = State::MaybeEndingComment(pads_seen);
                }
                (_, _) => {
                    state = State::InRawComment;
                }
            }
        }

        true
    }

    #[inline]
    fn skip_blank_param(&mut self) -> Result<(), Error> {
        while let Some(ch) = self.next() {
            if ch == b')' {
                return Ok(());
            }
        }

        // EOF
        Err(Error::BadSourceCode("Unexpected end of input".to_string()))
    }

    fn try_skip_string(&mut self) -> Result<Option<u8>, Error> {
        while let Some(ch) = self.next() {
            if Self::is_whitespace(ch) {
                continue;
            }

            return match ch {
                // Regular string
                b'"' => {
                    self.skip_string();
                    Ok(None)
                }
                // Raw string
                b'r' => {
                    if self.try_skip_raw_string() {
                        Ok(None)
                    } else {
                        Err(Error::BadSourceCode("Bad raw string".to_string()))
                    }
                }
                // Something else
                ch => Ok(Some(ch)),
            };
        }

        // EOF
        Err(Error::BadSourceCode("Unexpected end of input".to_string()))
    }

    // TODO: Was planning to match values here (but we only recognize ASCII atm):
    // https://github.com/rust-lang/rust/blob/38e0ae590caab982a4305da58a0a62385c2dd880/compiler/rustc_lexer/src/lib.rs#L245
    // We could switch back to UTF8 since we have been matching valid ASCII up to this point, but atm
    // any unicode whitespace will make it not match (not sure any code formatter preserves non-ASCII whitespace?)
    // For now, users should use NO whitespace and let the code formatters add any, if needed. I suspect
    // they will not add any non-ASCII whitespace on their own at min, but likely just ' ', '\n', and '\r'
    //
    // Code points we don't handle that we should (for future ref):
    // Code point 0x0085 == 0xC285
    // Code point 0x200E == 0xE2808E
    // Code point 0x200F == 0xE2808F
    // Code point 0x2028 == 0xE280A8
    // Code point 0x2029 == 0xE280A9
    #[inline]
    fn is_whitespace(ch: u8) -> bool {
        matches!(ch, b' ' | b'\n' | b'\r' | b'\t' | b'\x0b' | b'\x0c')
    }

    fn try_ws_matches(&mut self, slices: &[&[u8]], allow_whitespace_first: bool) -> bool {
        let mut allow_whitespace = allow_whitespace_first;

        'top: for &sl in slices {
            // Panic safety: it is pointless for us to pass in a blank slice, don't do that
            let first_ch = sl[0];

            while let Some(ch) = self.next() {
                // This is what we were looking for, now match the rest (if needed)
                if ch == first_ch {
                    // Panic safety: it is pointless for us to pass in a blank slice, don't do that
                    let remainder = &sl[1..];

                    if !remainder.is_empty() && !self.try_match(remainder) {
                        return false;
                    }
                    allow_whitespace = true;
                    continue 'top;
                } else if allow_whitespace && Self::is_whitespace(ch) {
                    // no op
                } else {
                    return false;
                }
            }

            // Premature EOF
            return false;
        }

        // If we can exhaust the iterator then they all must have matched
        true
    }

    fn try_match(&mut self, sl: &[u8]) -> bool {
        let iter = sl.iter();

        for &ch in iter {
            if self.next().is_none() {
                // This isn't great as it will reevaluate the last char - 'b' or 'c' in the main loop,
                // but since those aren't top level it will exit at the bottom of the main loop gracefully
                return false;
            }

            if self.curr != ch {
                return false;
            }
        }

        // If we can exhaust the iterator then it must have matched
        true
    }

    #[inline]
    fn detect_line_ending(&mut self) -> Option<&'static str> {
        match self.next() {
            Some(CR) => match self.next() {
                Some(LF) => Some(CRLF_STR),
                _ => None,
            },
            Some(LF) => Some(LF_STR),
            _ => None,
        }
    }

    #[inline]
    fn push_spaces(spaces: usize, buffer: &mut String) {
        for _ in 0..spaces {
            buffer.push(' ');
        }
    }

    fn process_blanks(
        _spaces: usize,
        buffer: &mut String,
        num: &str,
        ending: &str,
    ) -> Result<(), Error> {
        // Single blank line
        if num.is_empty() {
            buffer.push_str(ending);
        // Multiple blank lines
        } else {
            let num: syn::LitInt = syn::parse_str(num)?;
            let blanks: u32 = num.base10_parse()?;

            for _ in 0..blanks {
                buffer.push_str(ending);
            }
        }

        Ok(())
    }

    fn process_comments(
        spaces: usize,
        buffer: &mut String,
        s: &str,
        ending: &str,
    ) -> Result<(), Error> {
        // Single blank comment
        if s.is_empty() {
            Self::push_spaces(spaces, buffer);
            buffer.push_str(EMPTY_COMMENT);
            buffer.push_str(ending);
        // Multiple comments
        } else {
            let s: syn::LitStr = syn::parse_str(s)?;
            let comment = s.value();

            // Blank comment after parsing
            if comment.is_empty() {
                Self::push_spaces(spaces, buffer);
                buffer.push_str(EMPTY_COMMENT);
                buffer.push_str(ending);
            } else {
                for line in comment.lines() {
                    Self::push_spaces(spaces, buffer);

                    if line.is_empty() {
                        buffer.push_str(EMPTY_COMMENT);
                    } else {
                        buffer.push_str(COMMENT);
                        buffer.push_str(line);
                    }

                    buffer.push_str(ending);
                }
            }
        }

        Ok(())
    }

    // This is slightly different than comment in that we don't prepend a space but need to translate
    // the doc block literally (#[doc = "test"] == ///test <-- no prepended space)
    fn process_doc_block(
        spaces: usize,
        buffer: &mut String,
        s: &str,
        ending: &str,
    ) -> Result<(), Error> {
        // Single blank comment
        if s.is_empty() {
            Self::push_spaces(spaces, buffer);
            buffer.push_str(DOC_COMMENT);
            buffer.push_str(ending);
        // Multiple comments
        } else {
            let s: syn::LitStr = syn::parse_str(s)?;
            let comment = s.value();

            // Blank comment after parsing
            if comment.is_empty() {
                Self::push_spaces(spaces, buffer);
                buffer.push_str(DOC_COMMENT);
                buffer.push_str(ending);
            } else {
                for line in comment.lines() {
                    Self::push_spaces(spaces, buffer);
                    buffer.push_str(DOC_COMMENT);
                    buffer.push_str(line);
                    buffer.push_str(ending);
                }
            }
        }

        Ok(())
    }

    fn try_match_prefixes(
        &mut self,
        indent: usize,
        chars_matched: usize,
        prefixes: &[&[u8]],
        allow_ws_first: bool,
    ) -> Option<(usize, usize)> {
        // We already matched X chars before we got here (but didn't 'next()' after last match so minus 1)
        let mark_start_ident = self.curr_idx - ((chars_matched + indent) - 1);

        if self.try_ws_matches(prefixes, allow_ws_first) {
            let mark_start_value = self.curr_idx + 1;
            Some((mark_start_ident, mark_start_value))
        } else {
            None
        }
    }

    fn try_replace<F>(
        &mut self,
        spaces: usize,
        chars_matched: usize,
        suffixes: &[&[u8]],
        mark_start_ident: usize,
        mark_start_value: usize,
        f: F,
    ) -> Result<(), Error>
    where
        F: FnOnce(usize, &mut String, &str, &str) -> Result<(), Error>,
    {
        // End of value (exclusive)
        let mark_end_value = self.curr_idx + (1 - chars_matched);

        if !self.try_ws_matches(suffixes, true) {
            return Err(Error::BadSourceCode(
                "Unable to match suffix on doc block or marker.".to_string(),
            ));
        }

        if let Some(ending) = self.detect_line_ending() {
            // Mark end of ident here (inclusive)
            let mark_end_ident = self.curr_idx + 1;

            // Copy everything up until this marker
            self.copy_to_marker(mark_start_ident, mark_end_ident);

            // Parse and output
            f(
                spaces,
                &mut self.buffer,
                &self.source[mark_start_value..mark_end_value],
                ending,
            )?;
            Ok(())
        } else {
            Err(Error::BadSourceCode("Expected CR or LF".to_string()))
        }
    }

    fn try_replace_blank_marker(&mut self, spaces: usize) -> Result<bool, Error> {
        // 6 or 7 sections to match: _blank_ ! ( [int] ) ; CRLF|LF

        match self.try_match_prefixes(spaces, 2, BLANK_START, false) {
            Some((ident_start, value_start)) => {
                self.skip_blank_param()?;

                self.try_replace(
                    spaces,
                    1,
                    BLANK_END,
                    ident_start,
                    value_start,
                    CopyingCursor::process_blanks,
                )?;
                Ok(true)
            }
            None => Ok(false),
        }
    }

    fn try_replace_comment_marker(&mut self, spaces: usize) -> Result<bool, Error> {
        // 6 or 7 sections to match: _comment_ ! ( [string] ) ; CRLF|LF

        match self.try_match_prefixes(spaces, 2, COMMENT_START, false) {
            Some((ident_start, value_start)) => {
                // Make sure it is empty or a string
                let (matched, suffix) = match self.try_skip_string()? {
                    // String
                    None => (0, COMMENT_END),
                    // Empty
                    Some(b')') => (1, COMMENT_END2),
                    Some(ch) => {
                        return Err(Error::BadSourceCode(format!(
                            "Expected ')' or string, but got: {}",
                            ch as char
                        )))
                    }
                };

                self.try_replace(
                    spaces,
                    matched,
                    suffix,
                    ident_start,
                    value_start,
                    CopyingCursor::process_comments,
                )?;
                Ok(true)
            }
            None => Ok(false),
        }
    }

    fn try_replace_doc_block(&mut self, spaces: usize) -> Result<bool, Error> {
        // 7 sections to match: # [ doc = <string> ] CRLF|LF

        match self.try_match_prefixes(spaces, 1, DOC_BLOCK_START, true) {
            Some((ident_start, value_start)) => {
                // Make sure it is a string
                match self.try_skip_string()? {
                    // String
                    None => {
                        self.try_replace(
                            spaces,
                            0,
                            DOC_BLOCK_END,
                            ident_start,
                            value_start,
                            CopyingCursor::process_doc_block,
                        )?;
                        Ok(true)
                    }
                    Some(ch) => Err(Error::BadSourceCode(format!(
                        "Expected string, but got: {}",
                        ch as char
                    ))),
                }
            }
            None => Ok(false),
        }
    }
}

pub(crate) fn replace_markers(s: &str, replace_doc_blocks: bool) -> Result<Cow<str>, Error> {
    match CopyingCursor::new(s) {
        Some(mut cursor) => {
            let mut indent = 0;

            loop {
                match cursor.curr {
                    // Possible raw string
                    b'r' => {
                        indent = 0;
                        if !cursor.try_skip_raw_string() {
                            continue;
                        }
                    }
                    // Regular string
                    b'\"' => {
                        indent = 0;
                        cursor.skip_string()
                    }
                    // Possible comment
                    b'/' => {
                        indent = 0;
                        if !cursor.try_skip_comment() {
                            continue;
                        }
                    }
                    // Possible special ident (_comment!_ or _blank!_)
                    b'_' => {
                        if cursor.next().is_none() {
                            break;
                        }

                        match cursor.curr {
                            // Possible blank marker
                            b'b' => {
                                if !cursor.try_replace_blank_marker(indent)? {
                                    indent = 0;
                                    continue;
                                }
                            }
                            // Possible comment marker
                            b'c' => {
                                if !cursor.try_replace_comment_marker(indent)? {
                                    indent = 0;
                                    continue;
                                }
                            }
                            // Nothing we are interested in
                            _ => {
                                indent = 0;
                                continue;
                            }
                        }

                        indent = 0;
                    }
                    // Possible doc block
                    b'#' if replace_doc_blocks => {
                        if !cursor.try_replace_doc_block(indent)? {
                            indent = 0;
                            continue;
                        }

                        indent = 0;
                    }
                    // Count spaces in front of our three special replacements
                    b' ' => {
                        indent += 1;
                    }
                    // Anything else
                    _ => {
                        indent = 0;
                    }
                }

                if cursor.next().is_none() {
                    break;
                }
            }

            Ok(cursor.into_buffer())
        }
        // Empty file
        None => Ok(Cow::Borrowed(s)),
    }
}

// *** Tests ***

#[cfg(test)]
mod tests {
    use std::borrow::Cow;

    use pretty_assertions::assert_eq;

    use crate::replace::replace_markers;
    use crate::Error;

    #[test]
    fn blank() {
        let source = "";

        let actual = replace_markers(source, false).unwrap();
        let expected = source;

        assert_eq!(expected, actual);
        assert!(matches!(actual, Cow::Borrowed(_)));
    }

    #[test]
    fn no_replacements() {
        let source = r####"// _comment!_("comment");

/* /* nested comment */ */
        
/// This is a main function
fn main() {
    println!("hello world");
    println!(r##"hello raw world!"##);
}
_blank!_;
"####;

        let actual = replace_markers(source, false).unwrap();
        let expected = source;

        assert_eq!(expected, actual);
        assert!(matches!(actual, Cow::Borrowed(_)));
    }

    #[test]
    fn replace_comments() {
        let source = r####"// _comment!_("comment");

/* /* nested comment */ */
_comment_!("comment 1\n\ncomment 2");
_comment_!("test");
_comment!("skip this");
/// This is a main function
fn main() {
    println!(r##"hello raw world!"##);
    _comment_!(r"");
    _comment_!();
    println!("hello \nworld");
}

   _comment_ !
( r#"This is two
comments"# )
;
_blank!_;
"####;

        let actual = replace_markers(source, false).unwrap();
        let expected = r####"// _comment!_("comment");

/* /* nested comment */ */
// comment 1
//
// comment 2
// test
_comment!("skip this");
/// This is a main function
fn main() {
    println!(r##"hello raw world!"##);
    //
    //
    println!("hello \nworld");
}

   // This is two
   // comments
_blank!_;
"####;

        assert_eq!(expected, actual);
    }

    #[test]
    fn replace_blanks() {
        let source = r####"// _blank!_(5);

/* /* nested comment */ */
_blank_!(2);
_blank!_("skip this");
#[doc = "This is a main function"]
fn main() {
    let r#test = "hello";
    println!(r"hello raw world!");
    _blank_!();
    println!("hello \nworld");
}

      _blank_
!(
2
);
_blank!_;
"####;

        let actual = replace_markers(source, false).unwrap();
        let expected = r####"// _blank!_(5);

/* /* nested comment */ */


_blank!_("skip this");
#[doc = "This is a main function"]
fn main() {
    let r#test = "hello";
    println!(r"hello raw world!");

    println!("hello \nworld");
}



_blank!_;
"####;

        assert_eq!(expected, actual);
    }

    #[test]
    fn replace_doc_blocks() {
        let source = r####"// _blank!_(5);

/* not a nested comment */
#[doc = r#" This is a main function"#]
#[doc = r#" This is two doc
 comments"#]
#[cfg(feature = "main")]
#[doc(hidden)]
fn main() {
    println!(r##"hello raw world!"##);
    #[doc = ""]
    println!("hello \nworld");
}

#    [
doc
 = 
 " this is\n\n three doc comments"
 
 ]
fn test() {
}
_blank!_;
"####;

        let actual = replace_markers(source, true).unwrap();
        let expected = r####"// _blank!_(5);

/* not a nested comment */
/// This is a main function
/// This is two doc
/// comments
#[cfg(feature = "main")]
#[doc(hidden)]
fn main() {
    println!(r##"hello raw world!"##);
    ///
    println!("hello \nworld");
}

/// this is
///
/// three doc comments
fn test() {
}
_blank!_;
"####;

        assert_eq!(expected, actual);
    }

    #[test]
    fn replace_crlf() {
        let source = "_blank_!(2);\r\n";
        let actual = replace_markers(source, false).unwrap();

        let expected = "\r\n\r\n";
        assert_eq!(expected, actual);
    }

    #[test]
    fn marker_end_after_prefix() {
        assert!(matches!(
            replace_markers("_blank_!(", false),
            Err(Error::BadSourceCode(_))
        ));
    }

    #[test]
    fn marker_param_not_string() {
        assert!(matches!(
            replace_markers("_comment_!(blah);\n", false),
            Err(Error::BadSourceCode(_))
        ));
    }

    #[test]
    fn marker_bad_suffix() {
        assert!(matches!(
            replace_markers("_comment_!(\"blah\"];\n", false),
            Err(Error::BadSourceCode(_))
        ));
    }

    #[test]
    fn doc_block_string_not_closed() {
        assert!(matches!(
            replace_markers("#[doc = \"test]\n", true),
            Err(Error::BadSourceCode(_))
        ));
    }
}