summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2025-07-09 11:28:26 +0200
committerGitHub <noreply@github.com>2025-07-09 09:28:26 +0000
commite71674f6b3db0768c3e9d6e0271628377f8c82d8 (patch)
tree8ea00050e3441f19c5da952ec0eadb1fecf2d14e
parente5e1dcd9c01341d2cd3473ac94a70223d5966086 (diff)
Construct library via extension trait instead of default & inherent impl (#6576)
-rw-r--r--crates/typst-cli/src/world.rs2
-rw-r--r--crates/typst-ide/src/tests.rs2
-rw-r--r--crates/typst-library/src/lib.rs36
-rw-r--r--crates/typst-library/src/routines.rs7
-rw-r--r--crates/typst/src/lib.rs19
-rw-r--r--docs/src/lib.rs4
-rw-r--r--tests/fuzz/src/compile.rs2
-rw-r--r--tests/src/world.rs2
8 files changed, 52 insertions, 22 deletions
diff --git a/crates/typst-cli/src/world.rs b/crates/typst-cli/src/world.rs
index 95bee235..8ad766b1 100644
--- a/crates/typst-cli/src/world.rs
+++ b/crates/typst-cli/src/world.rs
@@ -12,7 +12,7 @@ use typst::foundations::{Bytes, Datetime, Dict, IntoValue};
use typst::syntax::{FileId, Lines, Source, VirtualPath};
use typst::text::{Font, FontBook};
use typst::utils::LazyHash;
-use typst::{Library, World};
+use typst::{Library, LibraryExt, World};
use typst_kit::fonts::{FontSlot, Fonts};
use typst_kit::package::PackageStorage;
use typst_timing::timed;
diff --git a/crates/typst-ide/src/tests.rs b/crates/typst-ide/src/tests.rs
index b3f368f2..168dfc9f 100644
--- a/crates/typst-ide/src/tests.rs
+++ b/crates/typst-ide/src/tests.rs
@@ -10,7 +10,7 @@ use typst::syntax::package::{PackageSpec, PackageVersion};
use typst::syntax::{FileId, Source, VirtualPath};
use typst::text::{Font, FontBook, TextElem, TextSize};
use typst::utils::{singleton, LazyHash};
-use typst::{Feature, Library, World};
+use typst::{Feature, Library, LibraryExt, World};
use crate::IdeWorld;
diff --git a/crates/typst-library/src/lib.rs b/crates/typst-library/src/lib.rs
index fa797788..3e2ce99e 100644
--- a/crates/typst-library/src/lib.rs
+++ b/crates/typst-library/src/lib.rs
@@ -36,6 +36,7 @@ use typst_utils::{LazyHash, SmallBitSet};
use crate::diag::FileResult;
use crate::foundations::{Array, Binding, Bytes, Datetime, Dict, Module, Scope, Styles};
use crate::layout::{Alignment, Dir};
+use crate::routines::Routines;
use crate::text::{Font, FontBook};
use crate::visualize::Color;
@@ -139,6 +140,11 @@ impl<T: World + ?Sized> WorldExt for T {
}
/// Definition of Typst's standard library.
+///
+/// To create and configure the standard library, use the `LibraryExt` trait
+/// and call
+/// - `Library::default()` for a standard configuration
+/// - `Library::builder().build()` if you want to customize the library
#[derive(Debug, Clone, Hash)]
pub struct Library {
/// The module that contains the definitions that are available everywhere.
@@ -154,30 +160,28 @@ pub struct Library {
pub features: Features,
}
-impl Library {
- /// Create a new builder for a library.
- pub fn builder() -> LibraryBuilder {
- LibraryBuilder::default()
- }
-}
-
-impl Default for Library {
- /// Constructs the standard library with the default configuration.
- fn default() -> Self {
- Self::builder().build()
- }
-}
-
/// Configurable builder for the standard library.
///
-/// This struct is created by [`Library::builder`].
-#[derive(Debug, Clone, Default)]
+/// Constructed via the `LibraryExt` trait.
+#[derive(Debug, Clone)]
pub struct LibraryBuilder {
+ #[expect(unused, reason = "will be used in the future")]
+ routines: &'static Routines,
inputs: Option<Dict>,
features: Features,
}
impl LibraryBuilder {
+ /// Creates a new builder.
+ #[doc(hidden)]
+ pub fn from_routines(routines: &'static Routines) -> Self {
+ Self {
+ routines,
+ inputs: None,
+ features: Features::default(),
+ }
+ }
+
/// Configure the inputs visible through `sys.inputs`.
pub fn with_inputs(mut self, inputs: Dict) -> Self {
self.inputs = Some(inputs);
diff --git a/crates/typst-library/src/routines.rs b/crates/typst-library/src/routines.rs
index 4bf8d60c..6db99ba5 100644
--- a/crates/typst-library/src/routines.rs
+++ b/crates/typst-library/src/routines.rs
@@ -1,3 +1,4 @@
+use std::fmt::{self, Debug, Formatter};
use std::hash::{Hash, Hasher};
use comemo::{Tracked, TrackedMut};
@@ -38,6 +39,12 @@ macro_rules! routines {
impl Hash for Routines {
fn hash<H: Hasher>(&self, _: &mut H) {}
}
+
+ impl Debug for Routines {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ f.pad("Routines(..)")
+ }
+ }
};
}
diff --git a/crates/typst/src/lib.rs b/crates/typst/src/lib.rs
index 0673c325..11d5c9e0 100644
--- a/crates/typst/src/lib.rs
+++ b/crates/typst/src/lib.rs
@@ -323,6 +323,25 @@ mod sealed {
}
}
+/// Provides ways to construct a [`Library`].
+pub trait LibraryExt {
+ /// Creates the default library.
+ fn default() -> Library;
+
+ /// Creates a builder for configuring a library.
+ fn builder() -> LibraryBuilder;
+}
+
+impl LibraryExt for Library {
+ fn default() -> Library {
+ Self::builder().build()
+ }
+
+ fn builder() -> LibraryBuilder {
+ LibraryBuilder::from_routines(&ROUTINES)
+ }
+}
+
/// Defines implementation of various Typst compiler routines as a table of
/// function pointers.
///
diff --git a/docs/src/lib.rs b/docs/src/lib.rs
index ddc956e6..e3eb21f9 100644
--- a/docs/src/lib.rs
+++ b/docs/src/lib.rs
@@ -24,7 +24,7 @@ use typst::foundations::{
use typst::layout::{Abs, Margin, PageElem, PagedDocument};
use typst::text::{Font, FontBook};
use typst::utils::LazyHash;
-use typst::{Category, Feature, Library, LibraryBuilder};
+use typst::{Category, Feature, Library, LibraryExt};
use unicode_math_class::MathClass;
macro_rules! load {
@@ -51,7 +51,7 @@ static GROUPS: LazyLock<Vec<GroupData>> = LazyLock::new(|| {
});
static LIBRARY: LazyLock<LazyHash<Library>> = LazyLock::new(|| {
- let mut lib = LibraryBuilder::default()
+ let mut lib = Library::builder()
.with_features([Feature::Html].into_iter().collect())
.build();
let scope = lib.global.scope_mut();
diff --git a/tests/fuzz/src/compile.rs b/tests/fuzz/src/compile.rs
index 3dedfb73..945e9fce 100644
--- a/tests/fuzz/src/compile.rs
+++ b/tests/fuzz/src/compile.rs
@@ -7,7 +7,7 @@ use typst::layout::PagedDocument;
use typst::syntax::{FileId, Source};
use typst::text::{Font, FontBook};
use typst::utils::LazyHash;
-use typst::{Library, World};
+use typst::{Library, LibraryExt, World};
struct FuzzWorld {
library: LazyHash<Library>,
diff --git a/tests/src/world.rs b/tests/src/world.rs
index 9b16d612..4b6cf5a3 100644
--- a/tests/src/world.rs
+++ b/tests/src/world.rs
@@ -19,7 +19,7 @@ use typst::syntax::{FileId, Source, Span};
use typst::text::{Font, FontBook, TextElem, TextSize};
use typst::utils::{singleton, LazyHash};
use typst::visualize::Color;
-use typst::{Feature, Library, World};
+use typst::{Feature, Library, LibraryExt, World};
use typst_syntax::Lines;
/// A world that provides access to the tests environment.