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

use convert_case::{Case, Casing};
use syn::{punctuated::Punctuated, spanned::Spanned, Ident, PathSegment};
use syn_prelude::{JoinSynErrors, ToErr, ToIdent, ToIdentWithCase, ToSynError};

use crate::{
    dep::Deps,
    model::{FieldType, Import, Message, MessageElement, Package, Type},
};

#[derive(Debug)]
pub struct ResolveContext<'a> {
    pub package: &'a Option<Package>,
    pub types: HashMap<Ident, InsideType>,
    pub deps: &'a Deps,
    pub imports: &'a Vec<Import>,
}

#[derive(Debug)]
pub enum InsideType {
    Message(MessageHierarchy),
    Enum(Ident),
}

#[derive(Debug)]
pub struct MessageHierarchy {
    pub name: Vec<Ident>,
    pub inner_messages: Option<Vec<MessageHierarchy>>,
    pub inner_enums: Option<Vec<Ident>>,
}

impl Message {
    pub fn resolve(&mut self, ctx: &ResolveContext) -> syn::Result<()> {
        if let Some(InsideType::Message(hierarchy)) = ctx.types.get(&self.name) {
            self.resolve_field_types(ctx, hierarchy)
        } else {
            Ok(())
        }
    }
    fn resolve_field_types(
        &mut self,
        ctx: &ResolveContext,
        hierarchy: &MessageHierarchy,
    ) -> syn::Result<()> {
        let mut err = self
            .messages
            .iter_mut()
            .enumerate()
            .filter_map(|(index, inner_message)| {
                hierarchy.inner_messages.as_ref().map(|messages| {
                    inner_message.resolve_field_types(ctx, messages.get(index).unwrap())
                })
            })
            .collect::<Vec<_>>()
            .join_errors();

        if let Some(err2) = self
            .fields
            .iter_mut()
            .map(|f| match f {
                MessageElement::Field(f) => ctx.resolve_field_type(Some(hierarchy), &mut f.typ),
                MessageElement::OneOf(oneof) => match oneof
                    .fields
                    .iter_mut()
                    .map(|f| ctx.resolve_field_type(Some(hierarchy), &mut f.typ))
                    .collect::<Vec<syn::Result<_>>>()
                    .join_errors()
                {
                    Some(err) => Err(err),
                    None => Ok(()),
                },
            })
            .collect::<Vec<syn::Result<_>>>()
            .join_errors()
        {
            if let Some(err) = &mut err {
                err.combine(err2);
            } else {
                err = Some(err2);
            }
        }

        match err {
            Some(err) => Err(err),
            None => Ok(()),
        }
    }
}

impl From<(&Vec<Ident>, &Message)> for MessageHierarchy {
    fn from((path, value): (&Vec<Ident>, &Message)) -> Self {
        let mut current_path = Vec::with_capacity(path.len() + 1);
        current_path.clone_from(path);
        current_path.push(value.name.clone());
        let inner_messages = if value.messages.is_empty() {
            None
        } else {
            Some(
                value
                    .messages
                    .iter()
                    .map(|m| Self::from((&current_path, m)))
                    .collect::<Vec<_>>(),
            )
        };
        let inner_enums = if value.enums.is_empty() {
            None
        } else {
            Some(
                value
                    .enums
                    .iter()
                    .map(|e| e.name.clone())
                    .collect::<Vec<_>>(),
            )
        };
        Self {
            name: current_path,
            inner_messages,
            inner_enums,
        }
    }
}

impl MessageHierarchy {
    fn find_message(&self, name: &Ident) -> Option<&Self> {
        if let Some(inner_messages) = &self.inner_messages {
            inner_messages
                .iter()
                .find(|m| m.name.last().map(|n| n.eq(name)).unwrap_or_default())
        } else {
            None
        }
    }

    fn find_enum(&self, name: &Ident) -> Option<&Ident> {
        if let Some(inner_enums) = &self.inner_enums {
            inner_enums.iter().find(|e| name.eq(*e))
        } else {
            None
        }
    }
}

impl ResolveContext<'_> {
    pub fn resolve_type(
        &self,
        parent: Option<&MessageHierarchy>,
        typ: &mut Type,
    ) -> syn::Result<()> {
        let import0 = self.imports.get(0).unwrap();
        let type_path_seg_first = typ
            .type_path
            .segments
            .first()
            .ok_or(typ.type_path.span().to_syn_error("missing type name"))?;

        // lookup inner types
        if let Some(container) = parent {
            let mut type_name_iter = typ.type_path.segments.iter();
            if let Some(is_message) = Self::match_with_message_inner_type(
                import0,
                self.package.as_ref().map(|p| &p.package),
                container,
                &mut type_name_iter,
                &mut typ.complete_path,
            )? {
                typ.target_is_message = is_message;
                return Ok(());
            }
        }
        // search in proto
        if let Some(t) = self.types.get(type_path_seg_first) {
            match t {
                InsideType::Message(hierarchy) => {
                    if typ.type_path.segments.len() > 1 {
                        if let Some(is_message) = Self::match_with_message_inner_type(
                            import0,
                            self.package.as_ref().map(|p| &p.package),
                            hierarchy,
                            &mut typ.type_path.segments.iter().skip(1),
                            &mut typ.complete_path,
                        )? {
                            typ.target_is_message = is_message;
                            return Ok(());
                        }
                    } else {
                        typ.complete_path.push_import_with_scope(
                            import0,
                            self.package.as_ref().map(|p| &p.package),
                        );
                        typ.complete_path.push(type_path_seg_first);
                        typ.target_is_message = true;
                        return Ok(());
                    }
                }
                InsideType::Enum(e) => {
                    if typ.type_path.segments.len() > 1 {
                        typ.type_path
                            .span()
                            .to_syn_error("cannot find this type in inner enumeration")
                            .to_err()?;
                    } else {
                        typ.complete_path.push_import_with_scope(
                            import0,
                            self.package.as_ref().map(|p| &p.package),
                        );
                        typ.complete_path.push(e);
                        typ.target_is_message = false;
                        return Ok(());
                    }
                }
            }
        }
        // search in import-wide
        let type_path_first_str = type_path_seg_first.to_string();
        let try_package_name = type_path_seg_first.clone();
        if if type_path_first_str.is_case(Case::UpperCamel) {
            // try search default scope first
            self.match_with_external_type(None, typ)?
                || self.match_with_external_type(Some(&try_package_name), typ)?
        } else {
            self.match_with_external_type(Some(&try_package_name), typ)?
                || self.match_with_external_type(None, typ)?
        } {
            return Ok(());
        }

        typ.type_path
            .span()
            .to_syn_error(&format!(
                "no such type '{}'",
                typ.type_path
                    .segments
                    .iter()
                    .map(|s| s.to_string())
                    .collect::<Vec<_>>()
                    .join(".")
            ))
            .to_err()
    }

    pub fn resolve_field_type(
        &self,
        parent: Option<&MessageHierarchy>,
        typ: &mut FieldType,
    ) -> syn::Result<()> {
        match typ {
            FieldType::MessageOrEnum(typ) => self.resolve_type(parent, typ),
            FieldType::Map(map) => {
                if let Some(err) = (
                    self.resolve_field_type(parent, map.key.as_mut()),
                    self.resolve_field_type(parent, map.value.as_mut()),
                )
                    .join_errors()
                {
                    err.to_err()
                } else {
                    Ok(())
                }
            }
            _ => Ok(()),
        }
    }

    fn match_with_message_inner_type<'a>(
        import0: &Import,
        package: Option<&Ident>,
        hierarchy: &'a MessageHierarchy,
        type_name_iter: &mut impl Iterator<Item = &'a Ident>,
        full_type_path: &mut syn::Path,
    ) -> syn::Result<Option<bool>> {
        if let Some(type_name_seg) = type_name_iter.next() {
            if let Some(msg) = hierarchy.find_message(type_name_seg) {
                if type_name_iter.count() == 0 {
                    full_type_path.push_import_with_scope(import0, package);
                    full_type_path.push_type_vec(&msg.name, false);
                    return Ok(Some(true));
                }
                Self::match_with_message_inner_type(
                    import0,
                    package,
                    msg,
                    type_name_iter,
                    full_type_path,
                )
            } else if let Some(enum_name) = hierarchy.find_enum(type_name_seg) {
                if type_name_iter.count() == 0 {
                    full_type_path.push_import_with_scope(import0, package);
                    full_type_path.push_type_vec(&hierarchy.name, true);
                    full_type_path.push(enum_name);
                    Ok(Some(false))
                } else {
                    // cannot find more types in enumeration
                    type_name_seg
                        .to_syn_error("cannot find this type in inner enumeration")
                        .to_err()
                }
            } else {
                // cannot find this inner type in message
                Ok(None)
            }
        } else {
            // unreachable
            Ok(None)
        }
    }

    fn match_with_external_type(
        &self,
        package: Option<&Ident>,
        typ: &mut Type,
    ) -> syn::Result<bool> {
        let skip = if package.is_some() { 1 } else { 0 };
        if let Some(scope) = self
            .deps
            .scopes
            .get(&package.map(|p| p.to_string()).unwrap_or_default())
        {
            let type_path_str = typ
                .type_path
                .segments
                .iter()
                .skip(skip)
                .map(|s| s.to_string())
                .collect::<Vec<_>>()
                .join(".");
            if let Some(x) = scope.get(&type_path_str) {
                if x.prost_type {
                    let mut type_name_iter = typ.type_path.segments.iter().skip(2);
                    let type_name = typ.type_path.segments.last().unwrap();
                    typ.complete_path
                        .push(&("prost", type_name.span()).to_ident());
                    typ.complete_path.push_type_path(&mut type_name_iter, false);
                } else {
                    typ.complete_path
                        .push_import_with_scope(self.imports.get(x.import_index).unwrap(), package)
                        .push_type_path(&mut typ.type_path.segments.iter().skip(skip), false);
                }
                return Ok(true);
            }
        }
        Ok(false)
    }
}

pub trait PathMod {
    fn new() -> Self;
    fn push(&mut self, type_name_seg: &Ident) -> &mut Self;
    fn push_import_with_scope(&mut self, import: &Import, package: Option<&Ident>) -> &mut Self;
    fn push_type_path<'a>(
        &mut self,
        iter: &mut impl Iterator<Item = &'a Ident>,
        tailing: bool,
    ) -> &mut Self;
    fn push_type_vec(&mut self, name: &Vec<Ident>, tailing: bool) -> &mut Self;
}

impl PathMod for syn::Path {
    fn new() -> Self {
        Self {
            leading_colon: None,
            segments: Punctuated::new(),
        }
    }

    fn push(&mut self, type_name_seg: &Ident) -> &mut Self {
        self.segments.push(PathSegment {
            ident: type_name_seg.clone(),
            arguments: syn::PathArguments::None,
        });
        self
    }

    fn push_import_with_scope(&mut self, import: &Import, package: Option<&Ident>) -> &mut Self {
        if let Some(file_path) = &import.file_path {
            for seg in file_path.mod_path.segments.iter() {
                self.segments.push(seg.clone());
            }
        }
        if let Some(package) = package {
            self.push(package);
        }
        self
    }

    fn push_type_path<'a>(
        &mut self,
        iter: &mut impl Iterator<Item = &'a Ident>,
        tailing: bool,
    ) -> &mut Self {
        while let Some(seg) = iter.next() {
            if tailing || iter.count() > 0 {
                self.push(&seg.to_ident_with_case(Case::Snake));
            } else {
                self.push(seg);
            }
        }
        self
    }
    fn push_type_vec(&mut self, name: &Vec<Ident>, tailing: bool) -> &mut Self {
        self.push_type_path(&mut name.iter(), tailing)
    }
}