synsert 0.1.2

A primitive for writing structural search and replace programs for rust
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
use clap::Parser;
use console::{Style, Term};
use itertools::Itertools as _;
use proc_macro2::Span;
use quote::{quote, ToTokens as _};
use std::{error::Error, fs, io, path::PathBuf};
use syn::{
    parse_quote, punctuated::Punctuated, spanned::Spanned as _, visit::Visit, Expr, Ident, LitStr,
    Token,
};
use synsert::Editor;

use crate::{
    format_args::{FormatArgs, OwnedPiece, OwnedPosition},
    tracing_fields::{Field, FieldKey, Path, Sigil},
};

fn print_diff(old: &str, new: &str) {
    synsert::harness::print_diff_on(&console::Term::stdout(), old, new).unwrap()
}

#[derive(clap::Parser)]
struct Args {
    #[arg(num_args(1..), required = true)]
    file: Vec<PathBuf>,
}

fn main() -> Result<(), Box<dyn Error>> {
    let Args { file } = Args::parse();

    let mut edited_files = 0;
    let mut edited_call_sites = 0;
    for path in file.iter() {
        println!("{}", Style::new().apply_to(path.display()).dim());
        let before = fs::read_to_string(path)?;
        let mut visitor = Visitor {
            editor: Editor::new(&before),
            error: false,
        };
        let Ok(ast) = syn::parse_file(&before) else {
            println!("skipped (failed to parse file)");
            continue;
        };
        visitor.visit_file(&ast);
        match visitor.editor.len() {
            0 => {}
            n => {
                let after = visitor.editor.finish();
                print_diff(&before, &after);

                if dialoguer::Confirm::new()
                    .with_prompt("save the edited file? ")
                    .default(true)
                    .interact()?
                {
                    fs::write(path, after)?;
                    edited_files += 1;
                    edited_call_sites += n;
                }
            }
        }
    }
    println!(
        "edited {} call sites in {} files",
        edited_call_sites, edited_files
    );
    Term::stderr().show_cursor()?; // clean up after rustyline on Ctrl+C
    Ok(())
}

struct Visitor {
    editor: Editor,
    error: bool,
}

impl<'ast> Visit<'ast> for Visitor {
    fn visit_macro(&mut self, node: &'ast syn::Macro) {
        if self.error {
            return; // short circuit
        };

        if !node.path.segments.last().is_some_and(|it| {
            matches!(
                it.ident.to_string().as_str(),
                "error" | "warn" | "info" | "debug" | "trace"
            )
        }) {
            return; // not the right macro
        }

        let body = match node.parse_body::<FormatArgs>() {
            Ok(it) => it,
            Err(e) => {
                println!(
                    "failed to parse callsite as `format!(..)`-style macro, skipping:\n{}\n",
                    syn_miette::Error::new(e, self.editor.source()).render()
                );
                return;
            }
        };

        let slug = body
            .pieces
            .iter()
            .filter_map(|it| match it {
                OwnedPiece::Lit(s) => Some(s),
                OwnedPiece::NextArgument(_) => None,
            })
            .fold(String::new(), |mut acc, el| {
                acc += el;
                acc
            });
        let slug = slug.trim();
        let slug = match slug.is_empty() {
            true => None,
            false => Some(slug),
        };

        let mut fields = Punctuated::<_, Token![,]>::new();

        let mut failed = vec![];
        for arg in body.pieces.iter().filter_map(|it| match it {
            OwnedPiece::Lit(_) => None,
            OwnedPiece::NextArgument(it) => Some(&**it),
        }) {
            let sigil = match arg.format.ty.as_str() {
                "" => Some(Sigil::Percent),
                "?" => Some(Sigil::QuestionMark),
                _ => None,
            };
            match &arg.position {
                OwnedPosition::ArgumentImplicitlyIs(ix) | OwnedPosition::ArgumentIs(ix) => {
                    match body.positional_args.get(*ix) {
                        Some(fail @ Expr::Path(it)) if it.path.segments.len() == 1 => {
                            match syn::parse2(it.to_token_stream()) {
                                Ok(it) => fields.push(Field::Shorthand(sigil, it)),
                                Err(e) => {
                                    failed.push(fail);
                                    println!(
                                        "failed to parse argument:\n{}",
                                        syn_miette::Error::new(e, self.editor.source()).render()
                                    )
                                }
                            }
                        }
                        Some(fail @ Expr::Field(it)) => match syn::parse2(it.to_token_stream()) {
                            Ok(it) => fields.push(Field::Shorthand(sigil, it)),
                            Err(e) => {
                                failed.push(fail);
                                println!(
                                    "failed to parse argument:\n{}",
                                    syn_miette::Error::new(e, self.editor.source()).render()
                                )
                            }
                        },
                        Some(it) => fields.push(Field::KV(
                            FieldKey::Quoted(LitStr::new(
                                it.to_token_stream().to_string().as_str(),
                                Span::call_site(),
                            )),
                            Token![=](Span::call_site()),
                            sigil,
                            it.clone(),
                        )),
                        None => {
                            println!(
                                "{}",
                                syn_miette::Error::new(
                                    syn::Error::new(
                                        node.span(),
                                        format!("missing positional argument at index {}", ix),
                                    ),
                                    self.editor.source(),
                                )
                                .render()
                            )
                        }
                    }
                }
                OwnedPosition::ArgumentNamed(it) => {
                    let ident = Ident::new(it, Span::call_site());
                    match body.named_args.get(&ident) {
                        Some(e) => fields.push(Field::KV(
                            FieldKey::Path(Path::from(ident)),
                            Token![=](Span::call_site()),
                            sigil,
                            e.clone(),
                        )),
                        None => fields.push(Field::Shorthand(sigil, Path::from(ident))),
                    }
                }
            }
        }

        if fields.is_empty() && failed.is_empty() {
            return; // do nothing
        }

        let before = self.editor.source_at(&node.tokens);
        let after = prettyprint(quote!(#fields, #slug))
            .replace("? ", "?")
            .replace("% ", "%");

        print_diff(before, &after);
        println!();

        match Action::interact(failed.is_empty()) {
            Ok(Action::Skip) => println!("skipped."),
            Ok(Action::Edit) => match rustyline::DefaultEditor::new() {
                Ok(mut it) => match it.readline_with_initial("", (after.as_str(), "")) {
                    Ok(it) => match it.is_empty() {
                        true => println!("skipped."),
                        false => {
                            self.editor.replace(&node.tokens, it);
                            println!("applied.")
                        }
                    },
                    Err(e) => {
                        self.error = true;
                        println!("Error: {}", e)
                    }
                },
                Err(e) => {
                    self.error = true;
                    println!("Error: {}", e)
                }
            },
            Ok(Action::Approve) => {
                self.editor.replace(&node.tokens, after);
                println!("applied.")
            }
            Err(e) => {
                self.error = true;
                println!("Error: {}", e)
            }
        }
    }
}

fn prettyprint(tokens: proc_macro2::TokenStream) -> String {
    let span = Span::call_site();
    prettyplease::unparse(&syn::File {
        shebang: None,
        attrs: vec![],
        items: vec![syn::Item::Macro(syn::ItemMacro {
            attrs: vec![],
            ident: None,
            mac: syn::Macro {
                path: parse_quote!(__remove_me),
                bang_token: Token![!](span),
                delimiter: syn::MacroDelimiter::Paren(syn::token::Paren(span)),
                tokens,
            },
            semi_token: Some(Token![;](span)),
        })],
    })
    .trim_start_matches("__remove_me!(")
    .trim_end()
    .trim_end_matches(");")
    .lines()
    .map(|it| it.trim())
    .join(" ")
    .trim()
    .into()
}

enum Action {
    Skip,
    Edit,
    Approve,
}

impl Action {
    fn interact(can_approve: bool) -> io::Result<Self> {
        let mut options = vec!["skip", "edit"];
        if can_approve {
            options.push("approve")
        }
        match dialoguer::Select::new()
            .default(0)
            .items(&options)
            .interact()
        {
            Ok(n) => Ok(match n {
                0 => Self::Skip,
                1 => Self::Edit,
                2 => Self::Approve,
                _ => unreachable!(),
            }),
            Err(dialoguer::Error::IO(e)) => Err(e),
        }
    }
}

mod tracing_fields {
    use derive_quote_to_tokens::ToTokens;
    use quote::{quote, ToTokens};
    use syn::{
        ext::IdentExt as _,
        parse::{Parse, ParseStream},
        punctuated::Punctuated,
        Expr, Ident, LitStr, Token,
    };

    #[derive(Debug, Clone)]
    pub enum Sigil {
        Percent,
        QuestionMark,
    }
    impl ToTokens for Sigil {
        fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
            tokens.extend(match self {
                Sigil::Percent => quote!(%),
                Sigil::QuestionMark => quote!(?),
            })
        }
    }
    #[derive(ToTokens, Debug, Clone)]
    pub struct Path {
        /// Non-empty, no trailing dots
        inner: Punctuated<Ident, Token![.]>,
    }
    impl Parse for Path {
        fn parse(input: ParseStream) -> syn::Result<Self> {
            Ok(Self {
                inner: Punctuated::parse_separated_nonempty_with(input, Ident::parse_any)?,
            })
        }
    }
    impl From<Ident> for Path {
        fn from(value: Ident) -> Self {
            Self {
                inner: Punctuated::from_iter([value]),
            }
        }
    }
    #[derive(ToTokens, Debug, Clone)]
    pub enum FieldKey {
        Path(Path),
        Quoted(LitStr),
    }

    #[derive(ToTokens, Debug, Clone)]
    pub enum Field {
        Shorthand(Option<Sigil>, Path),
        KV(FieldKey, Token![=], Option<Sigil>, Expr),
    }
}

mod format_args {
    use rustc_parse_format::{
        Alignment, Argument, Count, DebugHex, FormatSpec, ParseMode, Parser, Piece, Position, Sign,
    };
    use std::{collections::HashMap, ops::Range};
    use syn::{
        parse::{Parse, ParseStream},
        Expr, Ident, LitStr, Token,
    };

    #[derive(PartialEq, Debug)]
    pub struct FormatArgs {
        pub pieces: Vec<OwnedPiece>,
        pub positional_args: Vec<Expr>,
        pub named_args: HashMap<Ident, Expr>,
    }

    #[test]
    fn test() {
        use syn::parse_quote;
        assert_eq!(
            FormatArgs {
                pieces: vec![
                    OwnedPiece::Lit(String::from("hello ")),
                    OwnedPiece::NextArgument(Box::new(OwnedArgument {
                        position: OwnedPosition::ArgumentImplicitlyIs(0),
                        position_span: InnerSpan { start: 8, end: 8 },
                        format: OwnedFormatSpec::default(),
                    })),
                    OwnedPiece::Lit(String::from("!")),
                ],
                positional_args: vec![parse_quote!(42)],
                named_args: HashMap::new()
            },
            parse_quote!("hello {}!", 42)
        );
    }

    impl Parse for FormatArgs {
        fn parse(input: ParseStream) -> syn::Result<Self> {
            let fmt = input.parse::<LitStr>()?.value();
            let pieces = Parser::new(&fmt, None, None, false, ParseMode::Format)
                .map(Into::into)
                .collect();
            let mut positional_args = Vec::new();
            let mut named_args = HashMap::new();
            if input.is_empty() {
                return Ok(Self {
                    pieces,
                    positional_args,
                    named_args,
                });
            }
            input.parse::<Token![,]>()?;
            for arg in input.parse_terminated(Arg::parse, Token![,])? {
                match arg {
                    Arg::Positional(it) => match named_args.is_empty() {
                        true => positional_args.push(it),
                        false => {
                            return Err(
                                input.error("positional arguments may not follow named arguments")
                            )
                        }
                    },
                    Arg::Named(name, _, val) => match named_args.get_key_value(&name) {
                        None => {
                            named_args.insert(name, val);
                        }
                        Some((already, _)) => {
                            let mut error = input.error("duplicate named argument");
                            error.combine(syn::Error::new(
                                already.span(),
                                "previous definition here",
                            ));
                            return Err(error);
                        }
                    },
                }
            }

            Ok(Self {
                pieces,
                positional_args,
                named_args,
            })
        }
    }

    enum Arg {
        Positional(Expr),
        Named(Ident, #[allow(unused)] Token![=], Expr),
    }
    impl Parse for Arg {
        fn parse(input: ParseStream) -> syn::Result<Self> {
            Ok(match input.peek2(Token![=]) {
                true => Self::Named(input.parse()?, input.parse()?, input.parse()?),
                false => Self::Positional(input.parse()?),
            })
        }
    }

    /// A piece is a portion of the format string which represents the next part
    /// to emit. These are converted from [`Piece`]s emitted as a stream by the [`Parser`] class.
    #[derive(Clone, PartialEq, Debug)]
    pub enum OwnedPiece {
        /// A literal string which should directly be emitted
        Lit(String),
        /// This describes that formatting should process the next argument (as
        /// specified inside) for emission.
        NextArgument(Box<OwnedArgument>),
    }

    impl From<Piece<'_>> for OwnedPiece {
        fn from(value: Piece<'_>) -> Self {
            match value {
                Piece::Lit(it) => Self::Lit(it.into()),
                Piece::NextArgument(it) => Self::NextArgument(Box::new((*it).into())),
            }
        }
    }

    /// Representation of an argument specification.
    #[derive(Clone, PartialEq, Debug)]
    pub struct OwnedArgument {
        /// Where to find this argument
        pub position: OwnedPosition,
        /// The span of the position indicator. Includes any whitespace in implicit
        /// positions (`{  }`).
        pub position_span: Range<usize>,
        /// How to format the argument
        pub format: OwnedFormatSpec,
    }

    impl From<Argument<'_>> for OwnedArgument {
        fn from(value: Argument<'_>) -> Self {
            let Argument {
                position,
                position_span,
                format,
            } = value;
            Self {
                position: position.into(),
                position_span,
                format: format.into(),
            }
        }
    }

    /// Enum describing where an argument for a format can be located.
    #[derive(Clone, PartialEq, Debug)]
    #[allow(clippy::enum_variant_names)]
    pub enum OwnedPosition {
        /// The argument is implied to be located at an index
        ArgumentImplicitlyIs(usize),
        /// The argument is located at a specific index given in the format,
        ArgumentIs(usize),
        /// The argument has a name.
        ArgumentNamed(String),
    }
    impl From<Position<'_>> for OwnedPosition {
        fn from(value: Position<'_>) -> Self {
            match value {
                Position::ArgumentImplicitlyIs(it) => Self::ArgumentImplicitlyIs(it),
                Position::ArgumentIs(it) => Self::ArgumentIs(it),
                Position::ArgumentNamed(it) => Self::ArgumentNamed(it.into()),
            }
        }
    }

    /// Specification for the formatting of an argument in the format string.
    #[derive(Clone, PartialEq, Debug)]
    pub struct OwnedFormatSpec {
        /// Optionally specified character to fill alignment with.
        pub fill: Option<char>,
        /// Span of the optionally specified fill character.
        pub fill_span: Option<Range<usize>>,
        /// Optionally specified alignment.
        pub align: Alignment,
        /// The `+` or `-` flag.
        pub sign: Option<Sign>,
        /// The `#` flag.
        pub alternate: bool,
        /// The `0` flag.
        pub zero_pad: bool,
        /// The `x` or `X` flag. (Only for `Debug`.)
        pub debug_hex: Option<DebugHex>,
        /// The integer precision to use.
        pub precision: OwnedCount,
        /// The span of the precision formatting flag (for diagnostics).
        pub precision_span: Option<Range<usize>>,
        /// The string width requested for the resulting format.
        pub width: OwnedCount,
        /// The span of the width formatting flag (for diagnostics).
        pub width_span: Option<Range<usize>>,
        /// The descriptor string representing the name of the format desired for
        /// this argument, this can be empty or any number of characters, although
        /// it is required to be one word.
        pub ty: String,
        /// The span of the descriptor string (for diagnostics).
        pub ty_span: Option<Range<usize>>,
    }
    impl From<FormatSpec<'_>> for OwnedFormatSpec {
        fn from(value: FormatSpec<'_>) -> Self {
            let FormatSpec {
                fill,
                fill_span,
                align,
                sign,
                alternate,
                zero_pad,
                debug_hex,
                precision,
                precision_span,
                width,
                width_span,
                ty,
                ty_span,
            } = value;
            Self {
                fill,
                fill_span,
                align,
                sign,
                alternate,
                zero_pad,
                debug_hex,
                precision: precision.into(),
                precision_span,
                width: width.into(),
                width_span,
                ty: ty.into(),
                ty_span,
            }
        }
    }

    impl Default for OwnedFormatSpec {
        fn default() -> Self {
            Self {
                fill: Default::default(),
                fill_span: Default::default(),
                align: Alignment::AlignUnknown,
                sign: Default::default(),
                alternate: Default::default(),
                zero_pad: Default::default(),
                debug_hex: Default::default(),
                precision: Default::default(),
                precision_span: Default::default(),
                width: Default::default(),
                width_span: Default::default(),
                ty: Default::default(),
                ty_span: Default::default(),
            }
        }
    }

    /// A count is used for the precision and width parameters of an integer, and
    /// can reference either an argument or a literal integer.
    #[derive(Clone, PartialEq, Debug, Default)]
    #[allow(clippy::enum_variant_names)]
    pub enum OwnedCount {
        /// The count is specified explicitly.
        CountIs(u16),
        /// The count is specified by the argument with the given name.
        CountIsName(String, Range<usize>),
        /// The count is specified by the argument at the given index.
        CountIsParam(usize),
        /// The count is specified by a star (like in `{:.*}`) that refers to the argument at the given index.
        CountIsStar(usize),
        /// The count is implied and cannot be explicitly specified.
        #[default]
        CountImplied,
    }
    impl From<Count<'_>> for OwnedCount {
        fn from(value: Count<'_>) -> Self {
            match value {
                Count::CountIs(it) => Self::CountIs(it),
                Count::CountIsName(l, r) => Self::CountIsName(l.into(), r),
                Count::CountIsParam(it) => Self::CountIsParam(it),
                Count::CountIsStar(it) => Self::CountIsStar(it),
                Count::CountImplied => Self::CountImplied,
            }
        }
    }
}