diff options
| author | Laurenz <laurmaedje@gmail.com> | 2025-01-29 15:20:30 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-29 14:20:30 +0000 |
| commit | 1b2719c94c6422112508cfad24bdd9504541c363 (patch) | |
| tree | f58682b5203b3448a560b2efd3f50c4c7d79372b /crates/typst-library/src/foundations/module.rs | |
| parent | 9665eecdb62ee94cd9fcf4dfc61e2c70ba9391fb (diff) | |
Resolve bound name of bare import statically (#5773)
Diffstat (limited to 'crates/typst-library/src/foundations/module.rs')
| -rw-r--r-- | crates/typst-library/src/foundations/module.rs | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/crates/typst-library/src/foundations/module.rs b/crates/typst-library/src/foundations/module.rs index a476d6af..2001aca1 100644 --- a/crates/typst-library/src/foundations/module.rs +++ b/crates/typst-library/src/foundations/module.rs @@ -32,7 +32,7 @@ use crate::foundations::{repr, ty, Content, Scope, Value}; #[allow(clippy::derived_hash_with_manual_eq)] pub struct Module { /// The module's name. - name: EcoString, + name: Option<EcoString>, /// The reference-counted inner fields. inner: Arc<Repr>, } @@ -52,14 +52,22 @@ impl Module { /// Create a new module. pub fn new(name: impl Into<EcoString>, scope: Scope) -> Self { Self { - name: name.into(), + name: Some(name.into()), + inner: Arc::new(Repr { scope, content: Content::empty(), file_id: None }), + } + } + + /// Create a new anonymous module without a name. + pub fn anonymous(scope: Scope) -> Self { + Self { + name: None, inner: Arc::new(Repr { scope, content: Content::empty(), file_id: None }), } } /// Update the module's name. pub fn with_name(mut self, name: impl Into<EcoString>) -> Self { - self.name = name.into(); + self.name = Some(name.into()); self } @@ -82,8 +90,8 @@ impl Module { } /// Get the module's name. - pub fn name(&self) -> &EcoString { - &self.name + pub fn name(&self) -> Option<&EcoString> { + self.name.as_ref() } /// Access the module's scope. @@ -105,8 +113,9 @@ impl Module { /// Try to access a definition in the module. pub fn field(&self, name: &str) -> StrResult<&Value> { - self.scope().get(name).ok_or_else(|| { - eco_format!("module `{}` does not contain `{name}`", self.name()) + self.scope().get(name).ok_or_else(|| match &self.name { + Some(module) => eco_format!("module `{module}` does not contain `{name}`"), + None => eco_format!("module does not contain `{name}`"), }) } @@ -131,7 +140,10 @@ impl Debug for Module { impl repr::Repr for Module { fn repr(&self) -> EcoString { - eco_format!("<module {}>", self.name()) + match &self.name { + Some(module) => eco_format!("<module {module}>"), + None => "<module>".into(), + } } } |
