diff options
| author | Laurenz <laurmaedje@gmail.com> | 2025-07-09 11:28:26 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-09 09:28:26 +0000 |
| commit | e71674f6b3db0768c3e9d6e0271628377f8c82d8 (patch) | |
| tree | 8ea00050e3441f19c5da952ec0eadb1fecf2d14e /crates/typst-library | |
| parent | e5e1dcd9c01341d2cd3473ac94a70223d5966086 (diff) | |
Construct library via extension trait instead of default & inherent impl (#6576)
Diffstat (limited to 'crates/typst-library')
| -rw-r--r-- | crates/typst-library/src/lib.rs | 36 | ||||
| -rw-r--r-- | crates/typst-library/src/routines.rs | 7 |
2 files changed, 27 insertions, 16 deletions
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(..)") + } + } }; } |
