From ebfdb1dafa430786db10dad2ef7d5467c1bdbed1 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sun, 2 Jul 2023 19:59:52 +0200 Subject: Move everything into `crates/` directory --- src/eval/module.rs | 98 ------------------------------------------------------ 1 file changed, 98 deletions(-) delete mode 100644 src/eval/module.rs (limited to 'src/eval/module.rs') diff --git a/src/eval/module.rs b/src/eval/module.rs deleted file mode 100644 index 0bc6bf38..00000000 --- a/src/eval/module.rs +++ /dev/null @@ -1,98 +0,0 @@ -use std::fmt::{self, Debug, Formatter}; -use std::sync::Arc; - -use ecow::{eco_format, EcoString}; - -use super::{Content, Scope, Value}; -use crate::diag::StrResult; - -/// An evaluated module, ready for importing or typesetting. -/// -/// Values of this type are cheap to clone and hash. -#[derive(Clone, Hash)] -#[allow(clippy::derived_hash_with_manual_eq)] -pub struct Module { - /// The module's name. - name: EcoString, - /// The reference-counted inner fields. - inner: Arc, -} - -/// The internal representation. -#[derive(Clone, Hash)] -struct Repr { - /// The top-level definitions that were bound in this module. - scope: Scope, - /// The module's layoutable contents. - content: Content, -} - -impl Module { - /// Create a new module. - pub fn new(name: impl Into) -> Self { - Self { - name: name.into(), - inner: Arc::new(Repr { scope: Scope::new(), content: Content::empty() }), - } - } - - /// Update the module's name. - pub fn with_name(mut self, name: impl Into) -> Self { - self.name = name.into(); - self - } - - /// Update the module's scope. - pub fn with_scope(mut self, scope: Scope) -> Self { - Arc::make_mut(&mut self.inner).scope = scope; - self - } - - /// Update the module's content. - pub fn with_content(mut self, content: Content) -> Self { - Arc::make_mut(&mut self.inner).content = content; - self - } - - /// Get the module's name. - pub fn name(&self) -> &EcoString { - &self.name - } - - /// Access the module's scope. - pub fn scope(&self) -> &Scope { - &self.inner.scope - } - - /// Access the module's scope, mutably. - pub fn scope_mut(&mut self) -> &mut Scope { - &mut Arc::make_mut(&mut self.inner).scope - } - - /// Try to access a definition in the module. - pub fn get(&self, name: &str) -> StrResult<&Value> { - self.scope().get(name).ok_or_else(|| { - eco_format!("module `{}` does not contain `{name}`", self.name()) - }) - } - - /// Extract the module's content. - pub fn content(self) -> Content { - match Arc::try_unwrap(self.inner) { - Ok(repr) => repr.content, - Err(arc) => arc.content.clone(), - } - } -} - -impl Debug for Module { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "", self.name()) - } -} - -impl PartialEq for Module { - fn eq(&self, other: &Self) -> bool { - self.name == other.name && Arc::ptr_eq(&self.inner, &other.inner) - } -} -- cgit v1.2.3