rune/worker/
wildcard_import.rs

1use crate::alloc::prelude::*;
2use crate::compile::{self, ErrorKind, Location, ModId, Visibility};
3use crate::item::IntoComponent;
4use crate::query::Query;
5use crate::ItemBuf;
6
7pub(crate) struct WildcardImport {
8    pub(crate) visibility: Visibility,
9    pub(crate) from: ItemBuf,
10    pub(crate) name: ItemBuf,
11    pub(crate) location: Location,
12    pub(crate) module: ModId,
13    pub(crate) found: bool,
14}
15
16impl WildcardImport {
17    pub(crate) fn process_global(&mut self, query: &mut Query<'_, '_>) -> compile::Result<()> {
18        if query.context.contains_prefix(&self.name)? {
19            for c in query.context.iter_components(&self.name)? {
20                let name = self.name.extended(c)?;
21
22                query.insert_import(
23                    &self.location,
24                    self.module,
25                    self.visibility,
26                    &self.from,
27                    &name,
28                    None,
29                    true,
30                )?;
31            }
32
33            self.found = true;
34        }
35
36        Ok(())
37    }
38
39    /// Process a local wildcard import.
40    pub(crate) fn process_local(&mut self, query: &mut Query) -> compile::Result<()> {
41        if query.contains_prefix(&self.name)? {
42            let components = query
43                .iter_components(&self.name)?
44                .map(|c| c.into_component())
45                .try_collect::<Result<Vec<_>, _>>()??;
46
47            for c in components {
48                let name = self.name.extended(c)?;
49
50                query.insert_import(
51                    &self.location,
52                    self.module,
53                    self.visibility,
54                    &self.from,
55                    &name,
56                    None,
57                    true,
58                )?;
59            }
60
61            self.found = true;
62        }
63
64        if !self.found {
65            return Err(compile::Error::new(
66                self.location,
67                ErrorKind::MissingItem {
68                    item: self.name.try_clone()?,
69                },
70            ));
71        }
72
73        Ok(())
74    }
75}