-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreference.rs
More file actions
138 lines (122 loc) · 4.72 KB
/
reference.rs
File metadata and controls
138 lines (122 loc) · 4.72 KB
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
use std::path::Path;
use anyhow::{bail, Context};
use crate::packs::{
constant_resolver::ConstantResolver, pack::Pack,
parsing::UnresolvedReference, Configuration, PackSet, SourceLocation,
};
#[derive(Debug)]
pub struct Reference {
pub constant_name: String,
pub defining_pack_name: Option<String>,
pub relative_defining_file: Option<String>,
pub referencing_pack_name: String,
pub relative_referencing_file: String,
pub source_location: SourceLocation,
}
impl Reference {
pub fn defining_pack<'a>(
&self,
pack_set: &'a PackSet,
) -> anyhow::Result<Option<&'a Pack>> {
if let Some(name) = &self.defining_pack_name {
Ok(Some(pack_set
.for_pack(name)
.context(format!(
"Reference#defining_pack_name is {}, but that pack is not found in pack set.",
&name
))?))
} else {
Ok(None)
}
}
pub fn referencing_pack<'a>(
&self,
pack_set: &'a PackSet,
) -> anyhow::Result<&'a Pack> {
pack_set.for_pack(&self.referencing_pack_name).
context(format!("Reference#referencing_pack_name is {}, but that pack is not found in pack set.",
&self.referencing_pack_name))
}
}
impl Reference {
pub fn from_unresolved_reference(
configuration: &Configuration,
constant_resolver: &(dyn ConstantResolver + Send + Sync),
unresolved_reference: &UnresolvedReference,
referencing_file_path: &Path,
) -> anyhow::Result<Vec<Reference>> {
let referencing_pack_name = match configuration
.pack_set
.for_file(referencing_file_path)?
.map(|pack| pack.name.clone())
{
Some(pack_name) => pack_name,
None => bail!(
"Could not find pack for referencing file path: {}",
&referencing_file_path.display()
),
};
let loc = &unresolved_reference.location;
let source_location = SourceLocation {
line: loc.start_row,
column: loc.start_col,
};
let relative_referencing_file_path = referencing_file_path
.strip_prefix(&configuration.absolute_root)
.unwrap()
.to_path_buf();
let relative_referencing_file =
relative_referencing_file_path.to_str().unwrap().to_string();
let str_namespace_path: Vec<&str> = unresolved_reference
.namespace_path
.iter()
.map(|s| s.as_str())
.collect::<Vec<&str>>();
let maybe_constant_definition = constant_resolver
.resolve(&unresolved_reference.name, &str_namespace_path);
if let Some(constant_definitions) = &maybe_constant_definition {
Ok(constant_definitions
.iter()
.map(move |constant| {
let absolute_path_of_definition =
&constant.absolute_path_of_definition;
let relative_defining_file = absolute_path_of_definition
.strip_prefix(&configuration.absolute_root)
.unwrap()
.to_path_buf()
.to_str()
.unwrap()
.to_string();
let defining_pack_name = configuration
.pack_set
.for_file(absolute_path_of_definition)?
.map(|pack| pack.name.clone());
let relative_defining_file = Some(relative_defining_file);
let constant_name = constant.fully_qualified_name.clone();
Ok(Reference {
constant_name,
defining_pack_name,
referencing_pack_name: referencing_pack_name.clone(),
relative_referencing_file: relative_referencing_file
.clone(),
source_location: source_location.clone(),
relative_defining_file,
})
})
.collect::<anyhow::Result<Vec<Reference>>>()?)
} else {
let defining_pack_name = None;
let relative_defining_file = None;
// Constant name is not known, so we'll just use the unresolved name for now
let constant_name = unresolved_reference.name.clone();
Ok(vec![Reference {
constant_name,
defining_pack_name,
referencing_pack_name,
relative_referencing_file,
source_location,
relative_defining_file,
}])
}
}
}