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
use crate::{
    semver::{SemVerError, Version as SemverVersion},
    utils::normalize,
    Semantics, Upgrader,
};

use paste::paste;
use rust_visitor::ra_ap_syntax::ast;

use std::collections::HashMap as Map;

macro_rules! members {
    ($($node:ident,)*) => {
        paste! {
            pub struct Version {
                pub(crate) version: SemverVersion,
                pub(crate) peers: Vec<String>,
                pub(crate) rename_structs: Map<String, Map<String, String>>,
                pub(crate) rename_methods: Map<String, Map<String, String>>,
                pub(crate) rename_members: Map<String, Map<String, String>>,
                pub(crate) rename_variants: Map<String, Map<String, String>>,
                $(pub(crate) [<hook_ $node:snake>]: Vec<Box<dyn Fn(&mut Upgrader, &ast::$node, &Semantics)>>,)*
            }

            impl Version {
                pub fn new(version: &str) -> Result<Self, SemVerError> {
                    Ok(Self {
                        version: SemverVersion::parse(version)?,
                        peers: vec![],
                        rename_structs: Map::new(),
                        rename_methods: Map::new(),
                        rename_members: Map::new(),
                        rename_variants: Map::new(),
                        $([<hook_ $node:snake>]: Vec::new(),)*
                    })
                }
            }
        }
    };
}

macro_rules! methods {
    ($($node:ident,)*) => {
        paste! {
            $(
                pub fn [<hook_ $node:snake>]<F>(mut self, f: F) -> Self
                where
                    F: Fn(&mut Upgrader, &ast::$node, &Semantics) + 'static,
                {
                    self.[<hook_ $node:snake>].push(Box::new(f));
                    self
                }
            )*
        }
    };
}

members!(
    MethodCallExpr,
    CallExpr,
    IdentPat,
    Path,
    PathExpr,
    PathPat,
    FieldExpr,
    RecordPat,
    RecordExpr,
    RecordExprField,
    RecordPatField,
    TupleStructPat,
);

impl Version {
    pub fn peers(mut self, peers: &[&str]) -> Self {
        self.peers = peers.to_vec().iter().map(|x| normalize(*x)).collect();
        self
    }

    pub fn rename_structs(mut self, name: &str, map: &[[&str; 2]]) -> Self {
        self.rename_structs.insert(
            name.to_string(),
            map.iter()
                .map(|x| (x[0].to_string(), x[1].to_string()))
                .collect(),
        );
        self
    }

    pub fn rename_methods(mut self, name: &str, map: &[[&str; 2]]) -> Self {
        self.rename_methods.insert(
            name.to_string(),
            map.iter()
                .map(|x| (x[0].to_string(), x[1].to_string()))
                .collect(),
        );
        self
    }

    pub fn rename_members(mut self, name: &str, map: &[[&str; 2]]) -> Self {
        self.rename_members.insert(
            name.to_string(),
            map.iter()
                .map(|x| (x[0].to_string(), x[1].to_string()))
                .collect(),
        );
        self
    }

    pub fn rename_variants(mut self, name: &str, map: &[[&str; 2]]) -> Self {
        self.rename_variants.insert(
            name.to_string(),
            map.iter()
                .map(|x| (x[0].to_string(), x[1].to_string()))
                .collect(),
        );
        self
    }
}

impl Version {
    methods!(
        MethodCallExpr,
        CallExpr,
        IdentPat,
        Path,
        PathExpr,
        PathPat,
        FieldExpr,
        RecordPat,
        RecordExpr,
        RecordExprField,
        RecordPatField,
        TupleStructPat,
    );
}