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
use std::collections::HashMap;
use genotype_parser::{
tree::{GTIdentifier, GTImportName, GTImportReference, GTPath},
GTDefinitionId, GTPathKind,
};
use genotype_path::GtRelativePath;
use miette::Result;
use crate::error::GTProjectError;
use super::*;
/// Module resolve data. It describes relations between module entities. It allows to
#[derive(Debug, PartialEq, Clone)]
pub struct GTPModuleResolve {
/// Paths resolve.
pub paths: HashMap<GTPath, GtModulePath>,
/// Identifiers resolve.
pub identifiers: HashMap<GTIdentifier, GTPModuleIdentifierResolve>,
/// Definitions resolve.
pub definitions: HashMap<GTDefinitionId, GtProjectModuleDefinitionResolve>,
}
impl GTPModuleResolve {
pub fn try_new(
modules: &Vec<GTProjectModuleParse>,
parse: >ProjectModuleParse,
) -> Result<Self> {
// Resolve module dependencies by mapping local paths to project module paths
let mut paths: HashMap<GTPath, GtModulePath> = HashMap::new();
for local_path in parse.1.resolve.deps.iter() {
// Continue if the dependency is already resolved or it is a package path
if paths.contains_key(local_path) || local_path.kind() == GTPathKind::Package {
continue;
}
// Get the project module path from the local path
let module_path = parse.0.resolve(local_path);
paths.insert(local_path.clone(), module_path);
}
// Resolve module references mapping identifiers to dependencies
let mut identifiers: HashMap<GTIdentifier, GTPModuleIdentifierResolve> = HashMap::new();
for reference in parse.1.resolve.references.iter() {
// Continue if the reference is already resolved
if identifiers.contains_key(reference) {
continue;
};
// Check if the reference is a package import
let package_import = parse.1.module.imports.iter().find(|import| {
if import.path.kind() != GTPathKind::Package {
return false;
}
match &import.reference {
GTImportReference::Name(_, name) => name.1 == reference.1,
GTImportReference::Names(_, names) => names.iter().any(|name| match name {
GTImportName::Name(_, name) => name.1 == reference.1,
GTImportName::Alias(_, _, alias) => alias.1 == reference.1,
}),
GTImportReference::Glob(_) => false,
}
});
if let Some(import) = package_import {
identifiers.insert(
reference.clone(),
GTPModuleIdentifierResolve {
source: GTPModuleIdentifierSource::Package(import.path.clone()),
},
);
continue;
}
// Check if the reference is local
if parse
.1
.resolve
.exports
.iter()
.any(|export| export.1 == reference.1)
{
identifiers.insert(
reference.clone(),
GTPModuleIdentifierResolve {
source: GTPModuleIdentifierSource::Local,
},
);
continue;
}
// Find the local path of the reference
let (local_path, _) = paths
.iter()
.find(|(local_path, module_resolve)| {
let import = parse.1.module.imports.iter().find(|import| {
if import.path != **local_path {
return false;
}
match &import.reference {
GTImportReference::Glob(_) => {
let module = modules
.iter()
.find(|module| module.0 == **module_resolve)
.unwrap();
module
.1
.resolve
.exports
.iter()
.any(|export| export.1 == reference.1)
}
GTImportReference::Name(_, name) => name.1 == reference.1,
GTImportReference::Names(_, names) => {
names.iter().any(|name| match name {
GTImportName::Name(_, name) => name.1 == reference.1,
GTImportName::Alias(_, _, alias) => alias.1 == reference.1,
})
}
}
});
!import.is_none()
})
.ok_or_else(|| GTProjectError::UndefinedType {
span: reference.as_span(),
identifier: reference.as_string(),
})?;
identifiers.insert(
reference.clone(),
GTPModuleIdentifierResolve {
source: GTPModuleIdentifierSource::External(local_path.clone()),
},
);
}
Ok(GTPModuleResolve {
paths,
identifiers,
definitions: Default::default(),
})
}
}