automapper-proc 0.0.1

Procedural macro for automapper - please use `automapper` crate
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
use anyhow::{anyhow, bail, Context};
use quote::{format_ident, ToTokens};
use rustdoc_types::{Crate, GenericArg, GenericArgs, Item, ItemSummary};
use search::SearchResult;

mod search;

//
// Public
//

pub fn query_types(name: &syn::Path, rdocs: &Crate) -> anyhow::Result<Vec<RustType>> {
    let mut structs = query_structs(name, rdocs)?
        .into_iter()
        .map(RustType::Struct)
        .collect::<Vec<_>>();

    let enums = query_enums(name, rdocs)?.into_iter().map(RustType::Enum);
    structs.extend(enums.collect::<Vec<_>>());

    Ok(structs)
}

/// Find a type (struct/enum) by name,
/// return the item with exact match or a similar one otherwise.
pub fn find_types_try_exact(name: &syn::Path, rdocs: &Crate) -> anyhow::Result<RustType> {
    let items = query_types(name, rdocs)?;
    items
        .iter()
        .find(|i| i.exact_match())
        .or_else(|| items.first())
        .cloned()
        .with_context(|| format!("failed to find type: {}", name.to_token_stream()))
}

pub fn find_path_by_id(id: &rustdoc_types::Id, rdocs: &Crate) -> syn::Path {
    let dc = rdocs.paths.get(id).unwrap();

    if !matches!(
        dc.kind,
        rustdoc_types::ItemKind::Struct | rustdoc_types::ItemKind::Enum
    ) {
        unreachable!("find_path_by_id: path must be a struct or enum type")
    }

    syn::parse_str(&dc.path.join("::")).expect("failed to parse path")
}

//
// Private
//

/// Find enums by name.
///
///
fn query_enums(name: &syn::Path, rdocs: &Crate) -> anyhow::Result<Vec<EnumRustType>> {
    search::query_items(name, rdocs)
        .context("find struct by name")?
        .into_iter()
        .filter(|i| matches!(i.item.kind, rustdoc_types::ItemKind::Enum))
        .map(|result| _find_enum_with_resolved_variants(&result, rdocs))
        .collect::<anyhow::Result<Vec<_>>>()
}

/// Find structs by name.
///
/// This function will return a list of structs that match the given name partially or exactly.
/// Check the [StructWrapper::is_exact_match] field to see if the match was exact or not.
///
fn query_structs(name: &syn::Path, rdocs: &Crate) -> anyhow::Result<Vec<StructRustType>> {
    search::query_items(name, rdocs)
        .context("find struct by name")?
        .into_iter()
        .filter(|i| matches!(i.item.kind, rustdoc_types::ItemKind::Struct))
        .map(|result| _find_struct_with_resolved_fields(&result, rdocs))
        .collect::<anyhow::Result<Vec<_>>>()
}

fn _find_enum_with_resolved_variants(
    result: &SearchResult,
    rdocs: &Crate,
) -> Result<EnumRustType, anyhow::Error> {
    let rustdoc_types::ItemKind::Enum = result.item.kind else {
        bail!("not an enum type")
    };

    let (_, item) = rdocs
        .index
        .iter()
        .find(|(_, item)| item.id.eq(result.id))
        .context("locate struct in .index")?;

    let rustdoc_types::ItemEnum::Enum(enum_) = &item.inner else {
        unreachable!("_find_enum_with_resolved_variants: must be a struct type",)
    };

    let variants = enum_
        .variants
        .iter()
        .flat_map(|v| rdocs.index.get(v))
        .map(|e| {
            let rustdoc_types::ItemEnum::Variant(ref variant) = e.inner else {
                unreachable!("variant must be a variant type")
            };

            match &variant.kind {
                rustdoc_types::VariantKind::Plain => EnumVariant {
                    name: e.name.clone().expect("name of enum variant"),
                    kind: EnumVariantKind::Plain,
                },
                rustdoc_types::VariantKind::Tuple(items) => EnumVariant {
                    name: e.name.clone().expect("name of enum variant"),
                    kind: EnumVariantKind::Tuple(_resolve_fields(
                        rdocs,
                        items.iter().flatten().copied().collect::<Vec<_>>().as_ref(),
                    )),
                },
                rustdoc_types::VariantKind::Struct {
                    fields,
                    has_stripped_fields: _,
                } => EnumVariant {
                    name: e.name.clone().expect("name of enum variant"),
                    kind: EnumVariantKind::Struct {
                        fields: _resolve_fields(rdocs, fields),
                    },
                },
            }
        })
        .collect::<Vec<_>>();

    Ok(EnumRustType {
        is_exact_match: result.exact_match,
        is_root_crate: item.crate_id == 0,
        path: result.item.path.clone(),
        variants,
    })
}

fn _find_struct_with_resolved_fields(
    result: &SearchResult,
    rdocs: &Crate,
) -> Result<StructRustType, anyhow::Error> {
    let rustdoc_types::ItemKind::Struct = result.item.kind else {
        bail!("not a struct type")
    };

    let (_, item) = rdocs
        .index
        .iter()
        .find(|(_, item)| item.id.eq(result.id))
        .context("locate struct in .index")?;

    let rustdoc_types::ItemEnum::Struct(struct_) = &item.inner else {
        unreachable!("_find_struct_with_resolved_fields: must be a struct type",)
    };

    let kind = match &struct_.kind {
        rustdoc_types::StructKind::Unit => StructKind::Unit,
        rustdoc_types::StructKind::Tuple(vec) => StructKind::Tuple(_resolve_fields(
            rdocs,
            &vec.iter().flatten().copied().collect::<Vec<_>>(),
        )),
        rustdoc_types::StructKind::Plain {
            fields,
            has_stripped_fields: _,
        } => StructKind::Plain {
            fields: _resolve_fields(rdocs, fields),
        },
    };

    Ok(StructRustType {
        is_exact_match: result.exact_match,
        is_root_crate: item.crate_id == 0,
        path: result.item.path.clone(),
        kind,
    })
}

fn _resolve_fields(rdocs: &Crate, fields: &[rustdoc_types::Id]) -> Vec<StructFieldOrEnumVariant> {
    fields
        .iter()
        .flat_map(|id| rdocs.index.get(id))
        .map(|item| {
            let rustdoc_types::ItemEnum::StructField(ty) = &item.inner else {
                unreachable!("_resolve_fields: must be a struct field")
            };

            let kind = match ty {
                rustdoc_types::Type::ResolvedPath(path) => {
                    StructFieldKind::ResolvedPath { path: path.clone() }
                }
                rustdoc_types::Type::Primitive(ty) => {
                    StructFieldKind::Primitive { name: ty.clone() }
                }
                _ => unimplemented!("only struct kind plain or resolved path are supported"),
            };

            StructFieldOrEnumVariant {
                name: item.name.clone(),
                kind,
            }
        })
        .collect::<Vec<_>>()
}

#[derive(Debug, Clone)]
pub enum RustType {
    Struct(StructRustType),
    Enum(EnumRustType),
}

#[derive(Debug, Clone)]
pub struct EnumRustType {
    pub is_exact_match: bool,
    is_root_crate: bool,
    pub path: Vec<String>,
    pub variants: Vec<EnumVariant>,
}

#[derive(Debug, Clone)]
pub struct EnumVariant {
    pub name: String,
    pub kind: EnumVariantKind,
}

impl EnumVariant {
    pub fn are_same_kind(&self, other: &EnumVariant) -> bool {
        matches!(
            (&self.kind, &other.kind),
            (
                EnumVariantKind::Struct { .. },
                EnumVariantKind::Struct { .. }
            ) | (EnumVariantKind::Plain, EnumVariantKind::Plain)
                | (EnumVariantKind::Tuple(_), EnumVariantKind::Tuple(_))
        )
    }
}

#[derive(Debug, Clone)]
pub enum EnumVariantKind {
    Plain,
    Tuple(Vec<StructFieldOrEnumVariant>),
    Struct {
        fields: Vec<StructFieldOrEnumVariant>,
    },
}

#[derive(Debug, Clone)]
pub struct StructRustType {
    pub is_exact_match: bool,
    is_root_crate: bool,
    pub path: Vec<String>,
    pub kind: StructKind,
}

impl RustType {
    pub fn name(&self) -> &str {
        match self {
            RustType::Struct(s) => s.path.last(),
            RustType::Enum(e) => e.path.last(),
        }
        .expect("name")
    }

    pub fn is_root_crate(&self) -> bool {
        match self {
            RustType::Struct(s) => s.is_root_crate,
            RustType::Enum(e) => e.is_root_crate,
        }
    }

    pub fn exact_match(&self) -> bool {
        match self {
            RustType::Struct(s) => s.is_exact_match,
            RustType::Enum(e) => e.is_exact_match,
        }
    }

    pub fn path_segments(&self) -> &Vec<String> {
        match self {
            RustType::Struct(s) => &s.path,
            RustType::Enum(e) => &e.path,
        }
    }

    pub fn path(&self) -> syn::Path {
        let is_root_crate = self.is_root_crate();
        let mut segments = self
            .path_segments()
            .clone()
            .into_iter()
            .skip(if is_root_crate { 1 } else { 0 }) // TODO(FIX): Skip the crate name
            .collect::<Vec<_>>();
        if is_root_crate {
            segments.insert(0, "crate".to_string());
        }
        let segments = segments.join("::");
        syn::parse_str(&segments).expect("parse path")
    }

    pub fn are_same_kind(&self, other: &Self) -> bool {
        matches!(
            (self, other),
            (RustType::Struct(_), RustType::Struct(_)) | (RustType::Enum(_), RustType::Enum(_))
        )
    }
}

#[derive(Debug, Clone)]
pub enum StructKind {
    Unit,
    Tuple(Vec<StructFieldOrEnumVariant>),
    Plain {
        fields: Vec<StructFieldOrEnumVariant>,
    },
}

#[derive(Debug, Clone)]
pub struct StructFieldOrEnumVariant {
    /// Unset for tuple fields
    pub name: Option<String>,
    pub kind: StructFieldKind,
}

#[derive(Debug, Clone)]
pub enum StructFieldKind {
    Primitive { name: String },
    ResolvedPath { path: rustdoc_types::Path },
}

impl StructFieldOrEnumVariant {
    pub fn t_of_option(&self) -> anyhow::Result<&rustdoc_types::Path> {
        let StructFieldKind::ResolvedPath { path: source_path } = &self.kind else {
            anyhow::bail!("must be a resolved path")
        };

        let Some(generic_args) = &source_path.args else {
            anyhow::bail!("unreachable(BUG): must have args");
        };
        let GenericArgs::AngleBracketed {
            args,
            constraints: _,
        } = generic_args.as_ref()
        else {
            dbg!(generic_args);

            anyhow::bail!("unimplemented: Option type of parentized argument")
        };
        let Some(GenericArg::Type(ty)) = args.iter().next() else {
            dbg!(generic_args);

            anyhow::bail!("unimplemented: Option type with infered, const or lifetype argument")
        };

        let rustdoc_types::Type::ResolvedPath(path) = ty else {
            dbg!(generic_args);

            anyhow::bail!("unimplemented: Option type with touple value: Option<(,)>")
        };

        Ok(path)
    }
}

impl StructFieldKind {
    pub fn as_str(&self) -> &str {
        match self {
            StructFieldKind::Primitive { name } => name,
            StructFieldKind::ResolvedPath { path } => &path.name,
        }
    }
    pub fn is_same_kind(&self, other: &StructFieldKind) -> bool {
        matches!(
            (self, other),
            (
                StructFieldKind::Primitive { .. },
                StructFieldKind::Primitive { .. }
            ) | (
                StructFieldKind::ResolvedPath { .. },
                StructFieldKind::ResolvedPath { .. }
            )
        )
    }

    pub fn are_both_option_type(item1: &StructFieldKind, item2: &StructFieldKind) -> bool {
        match (item1, item2) {
            (
                StructFieldKind::ResolvedPath { path: p1 },
                StructFieldKind::ResolvedPath { path: p2 },
            ) => p1.name.clone() == "Option" && p2.name == "Option",
            _ => false,
        }
    }

    pub fn is_primitive(&self) -> bool {
        matches!(self, StructFieldKind::Primitive { .. })
    }

    pub fn is_resolved_path(&self) -> bool {
        matches!(self, StructFieldKind::ResolvedPath { .. })
    }

    pub fn is_primitive_eq(&self, other: &StructFieldKind) -> bool {
        match (self, other) {
            (StructFieldKind::Primitive { name: a }, StructFieldKind::Primitive { name: b }) => {
                a == b
            }
            _ => false,
        }
    }
}

#[cfg(test)]
mod test {
    use quote::format_ident;

    use super::*;

    #[test]
    fn find_struct() {
        let rdoc = get_test_data();
        let struct_ = query_structs(&format_ident!("SourceStruct").into(), &rdoc).unwrap();
        assert_eq!(struct_.len(), 1);
        assert!(!struct_[0].is_exact_match);

        let struct_ = query_structs(
            &syn::parse_str("usage::models::SourceStruct").unwrap(),
            &rdoc,
        )
        .unwrap();
        assert_eq!(struct_.len(), 1);
        assert!(struct_[0].is_exact_match);
    }

    pub(crate) fn get_test_data() -> Crate {
        let json = include_str!("../../usage/rustdoc.json");
        serde_json::from_str(json).unwrap()
    }
}

pub trait KindAsStr {
    fn kind_as_str(&self) -> &'static str;
}

impl KindAsStr for EnumVariant {
    fn kind_as_str(&self) -> &'static str {
        match self.kind {
            EnumVariantKind::Plain => "Plain",
            EnumVariantKind::Tuple(_) => "Tuple",
            EnumVariantKind::Struct { .. } => "Struct",
        }
    }
}

impl KindAsStr for RustType {
    fn kind_as_str(&self) -> &'static str {
        match self {
            RustType::Struct(_) => "Struct",
            RustType::Enum(_) => "Enum",
        }
    }
}

impl KindAsStr for StructKind {
    fn kind_as_str(&self) -> &'static str {
        match self {
            StructKind::Unit => "Unit",
            StructKind::Tuple(_) => "Tuple",
            StructKind::Plain { .. } => "Plain",
        }
    }
}