Clean up internal Exports struct

- Use deref instead of `.0` tuple access (looks neater)
- Provide a convenience find() function that also makes use of the sorting
This commit is contained in:
Simon Hausmann 2022-12-01 21:16:16 +01:00 committed by Simon Hausmann
parent 6f19ab38e7
commit b38fe7c5f0
5 changed files with 13 additions and 18 deletions

View file

@ -2068,7 +2068,7 @@ impl ExportedName {
}
#[derive(Default, Debug, derive_more::Deref)]
pub struct Exports(pub Vec<(ExportedName, Either<Rc<Component>, Type>)>);
pub struct Exports(Vec<(ExportedName, Either<Rc<Component>, Type>)>);
impl Exports {
pub fn from_node(
@ -2225,6 +2225,13 @@ impl Exports {
}
}
}
pub fn find(&self, name: &str) -> Option<Either<Rc<Component>, Type>> {
self.0
.binary_search_by(|(exported_name, _)| exported_name.as_str().cmp(name))
.ok()
.map(|index| self.0[index].1.clone())
}
}
impl std::iter::IntoIterator for Exports {

View file

@ -10,7 +10,7 @@ use crate::object_tree::{Component, Document, PropertyVisibility};
pub fn check_public_api(doc: &Document, diag: &mut BuildDiagnostics) {
check_public_api_component(&doc.root_component, diag);
for (export_name, e) in &doc.exports.0 {
for (export_name, e) in &*doc.exports {
if let Some(c) = e.as_ref().left() {
if c.is_global() {
// This global will become part of the public API.

View file

@ -16,7 +16,7 @@ pub fn collect_globals(doc: &Document, _diag: &mut BuildDiagnostics) {
doc.root_component.used_types.borrow_mut().globals.clear();
let mut set = HashSet::new();
let mut sorted_globals = vec![];
for (_, ty) in &doc.exports.0 {
for (_, ty) in &*doc.exports {
if let Some(c) = ty.as_ref().left() {
if c.is_global() {
if set.insert(ByAddress(c.clone())) {

View file

@ -230,13 +230,7 @@ impl TypeLoader {
let doc = self.all_documents.docs.get(&doc_path).unwrap();
doc.exports.0.iter().find_map(|(export_name, ty)| {
if type_name == export_name.as_str() {
ty.clone().left()
} else {
None
}
})
doc.exports.find(type_name).and_then(|compo_or_type| compo_or_type.left())
}
/// Append a possibly relative path to a base path. Returns the data if it resolves to a built-in (compiled-in)
@ -396,13 +390,7 @@ impl TypeLoader {
build_diagnostics: &mut BuildDiagnostics,
) {
for import_name in imported_types {
let imported_type = doc.exports.0.iter().find_map(|(export_name, ty)| {
if import_name.external_name == export_name.as_str() {
Some(ty.clone())
} else {
None
}
});
let imported_type = doc.exports.find(&import_name.external_name);
let imported_type = match imported_type {
Some(ty) => ty,

View file

@ -147,7 +147,7 @@ pub(crate) fn completion_at(
}
};
for (exported_name, ty) in &doc.exports.0 {
for (exported_name, ty) in &*doc.exports {
if available_types.contains(&exported_name.name) {
continue;
}