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
use std::collections::HashMap;

use log::{info, trace};
use log_derive::logfn;
use rustdoc_types as types;

use crate::types::*;

pub trait Approximate<Destination> {
    fn approx(
        &self,
        dest: &Destination,
        generics: &types::Generics,
        substs: &mut HashMap<String, Type>,
    ) -> Vec<Similarity>;
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Similarity {
    Equivalent,
    Subequal,
    Different,
}

use Similarity::*;

impl Approximate<types::Item> for Query {
    #[logfn(info, fmt = "Approximating `Query` to `Item` finished: {:?}")]
    fn approx(
        &self,
        item: &types::Item,
        generics: &types::Generics,
        substs: &mut HashMap<String, Type>,
    ) -> Vec<Similarity> {
        info!("Approximating `Query` to `Item`");
        trace!("approx(lhs={:?}, rhs={:?})", self, item);

        let mut sims = Vec::new();

        if let Some(ref name) = self.name {
            match item.name {
                Some(ref item_name) => sims.append(&mut name.approx(item_name, generics, substs)),
                None => sims.push(Different),
            }
        }

        if let Some(ref kind) = self.kind {
            sims.append(&mut kind.approx(&item.inner, generics, substs))
        }

        trace!("sims: {:?}", sims);
        sims
    }
}

impl Approximate<String> for Symbol {
    #[logfn(info, fmt = "Approximating `Symbol` to `String` finished: {:?}")]
    fn approx(
        &self,
        string: &String,
        generics: &types::Generics,
        substs: &mut HashMap<String, Type>,
    ) -> Vec<Similarity> {
        info!("Approximating `Symbol` to `String`");
        trace!("approx(lhs: {:?}, rhs: {:?})", self, string);

        if self == string {
            vec![Equivalent]
        } else {
            vec![Different]
        }
    }
}

impl Approximate<types::ItemEnum> for QueryKind {
    #[logfn(info, fmt = "Approximating `QueryKind` to `ItemEnum` finished: {:?}")]
    fn approx(
        &self,
        kind: &types::ItemEnum,
        generics: &types::Generics,
        substs: &mut HashMap<String, Type>,
    ) -> Vec<Similarity> {
        info!("Approximating `QueryKind` to `ItemEnum`");
        trace!("approx(lhs: {:?}, rhs: {:?})", self, kind);

        use types::ItemEnum::*;
        use QueryKind::*;
        match (self, kind) {
            (FunctionQuery(q), Function(i)) => q.approx(i, generics, substs),
            _ => vec![Different],
        }
    }
}

impl Approximate<types::Function> for Function {
    #[logfn(info, fmt = "Approximating `Function` to `Function` finished: {:?}")]
    fn approx(
        &self,
        function: &types::Function,
        generics: &types::Generics,
        substs: &mut HashMap<String, Type>,
    ) -> Vec<Similarity> {
        info!("Approximating `Function` to `Function`");
        trace!("approx(lhs: {:?}, rhs: {:?})", self, function);

        // update `generics` using `function.generics`
        self.decl.approx(&function.decl, generics, substs)
    }
}

impl Approximate<types::FnDecl> for FnDecl {
    #[logfn(info, fmt = "Approximating `FnDecl` to `FnDecl` finished: {:?}")]
    fn approx(
        &self,
        decl: &types::FnDecl,
        generics: &types::Generics,
        substs: &mut HashMap<String, Type>,
    ) -> Vec<Similarity> {
        info!("Approximating `FnDecl` to `FnDecl`");
        trace!("approx(lhs: {:?}, rhs: {:?})", self, decl);

        let mut sims = Vec::new();

        if let Some(ref inputs) = self.inputs {
            inputs
                .iter()
                .enumerate()
                .for_each(|(idx, input)| match decl.inputs.get(idx) {
                    Some(arg) => sims.append(&mut input.approx(arg, generics, substs)),
                    None => sims.push(Different),
                })
        }

        if let Some(ref output) = self.output {
            sims.append(&mut output.approx(&decl.output, generics, substs))
        }

        sims
    }
}

impl Approximate<(String, types::Type)> for Argument {
    #[logfn(
        info,
        fmt = "Approximating `Argument` to `(String, Type)` finished: {:?}"
    )]
    fn approx(
        &self,
        arg: &(String, types::Type),
        generics: &types::Generics,
        substs: &mut HashMap<String, Type>,
    ) -> Vec<Similarity> {
        info!("Approximating `Argument` to `(String, Type)`");
        trace!("approx(lhs: {:?}, rhs: {:?})", self, arg);

        let mut sims = Vec::new();

        if let Some(ref type_) = self.ty {
            sims.append(&mut type_.approx(&arg.1, generics, substs));
        }

        if let Some(ref name) = self.name {
            sims.append(&mut name.approx(&arg.0, generics, substs));
        }

        sims
    }
}

impl Approximate<Option<types::Type>> for FnRetTy {
    #[logfn(info, fmt = "Approximating `FnRetTy` to `Option<Type>` finished: {:?}")]
    fn approx(
        &self,
        ret_ty: &Option<types::Type>,
        generics: &types::Generics,
        substs: &mut HashMap<String, Type>,
    ) -> Vec<Similarity> {
        info!("Approximating `FnRetTy` to `Option<Type>`");
        trace!("approx(lhs: {:?}, rhs: {:?})", self, ret_ty);

        match (self, ret_ty) {
            (FnRetTy::Return(q), Some(i)) => q.approx(i, generics, substs),
            (FnRetTy::DefaultReturn, None) => vec![Equivalent],
            _ => vec![Different],
        }
    }
}

impl Approximate<types::Type> for Type {
    #[logfn(info, fmt = "Approximating `Type` to `Type` finished: {:?}")]
    fn approx(
        &self,
        type_: &types::Type,
        generics: &types::Generics,
        substs: &mut HashMap<String, Type>,
    ) -> Vec<Similarity> {
        info!("Approximating `Type` to `Type`");
        trace!(
            "approx(lhs: {:?}, rhs: {:?}, generics: {:?}, substs: {:?})",
            self,
            type_,
            generics,
            substs
        );

        use Type::*;
        match (self, type_) {
            (q, types::Type::Generic(i)) => match substs.get(i) {
                Some(i) => {
                    if q == i {
                        vec![Equivalent]
                    } else {
                        vec![Different]
                    }
                }
                None => {
                    substs.insert(i.clone(), q.clone());
                    vec![Equivalent]
                }
            },
            (q, types::Type::BorrowedRef { type_: i, .. }) => q.approx(i, generics, substs),
            (Primitive(q), types::Type::Primitive(i)) => q.approx(i, generics, substs),
            (Primitive(_), _) => vec![Different],
            _ => unimplemented!(),
        }
    }
}

impl Approximate<String> for PrimitiveType {
    #[logfn(
        info,
        fmt = "Approximating `PrimitiveType` to `PrimitiveType` finished: {:?}"
    )]
    fn approx(
        &self,
        prim_ty: &String,
        generics: &types::Generics,
        substs: &mut HashMap<String, Type>,
    ) -> Vec<Similarity> {
        info!("Approximating `PrimitiveType` to `String`");
        trace!("approx(lhs: {:?}, rhs: {:?})", self, prim_ty);

        if self.as_str() == prim_ty {
            vec![Equivalent]
        } else {
            vec![Different]
        }
    }
}