summaryrefslogtreecommitdiff
path: root/crates/typst-library/src
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2024-11-01 21:02:27 +0100
committerGitHub <noreply@github.com>2024-11-01 20:02:27 +0000
commitfac75837458e4f7be4f501e40331b8162d90a1f8 (patch)
tree06ff639e37555c98178934960a5e963f27de2b90 /crates/typst-library/src
parentb357de3c484e0cb320b0e3b778498815489f6a0d (diff)
Basic feature flag mechanism (#5350)
Diffstat (limited to 'crates/typst-library/src')
-rw-r--r--crates/typst-library/src/lib.rs52
1 files changed, 48 insertions, 4 deletions
diff --git a/crates/typst-library/src/lib.rs b/crates/typst-library/src/lib.rs
index 34c2e526..55cd1132 100644
--- a/crates/typst-library/src/lib.rs
+++ b/crates/typst-library/src/lib.rs
@@ -30,7 +30,7 @@ use std::ops::{Deref, Range};
use ecow::EcoString;
use typst_syntax::package::PackageSpec;
use typst_syntax::{FileId, Source, Span};
-use typst_utils::LazyHash;
+use typst_utils::{LazyHash, SmallBitSet};
use crate::diag::FileResult;
use crate::foundations::{Array, Bytes, Datetime, Dict, Module, Scope, Styles, Value};
@@ -161,9 +161,10 @@ pub struct Library {
/// The default style properties (for page size, font selection, and
/// everything else configurable via set and show rules).
pub styles: Styles,
- /// The standard library as a value.
- /// Used to provide the `std` variable.
+ /// The standard library as a value. Used to provide the `std` variable.
pub std: Value,
+ /// In-development features that were enabled.
+ pub features: Features,
}
impl Library {
@@ -186,6 +187,7 @@ impl Default for Library {
#[derive(Debug, Clone, Default)]
pub struct LibraryBuilder {
inputs: Option<Dict>,
+ features: Features,
}
impl LibraryBuilder {
@@ -195,16 +197,58 @@ impl LibraryBuilder {
self
}
+ /// Configure in-development features that should be enabled.
+ ///
+ /// No guarantees whatsover!
+ pub fn with_features(mut self, features: Features) -> Self {
+ self.features = features;
+ self
+ }
+
/// Consumes the builder and returns a `Library`.
pub fn build(self) -> Library {
let math = math::module();
let inputs = self.inputs.unwrap_or_default();
let global = global(math.clone(), inputs);
let std = Value::Module(global.clone());
- Library { global, math, styles: Styles::new(), std }
+ Library {
+ global,
+ math,
+ styles: Styles::new(),
+ std,
+ features: self.features,
+ }
+ }
+}
+
+/// A selection of in-development features that should be enabled.
+///
+/// Can be collected from an iterator of [`Feature`]s.
+#[derive(Debug, Default, Clone, Hash)]
+pub struct Features(SmallBitSet);
+
+impl Features {
+ /// Check whether the given feature is enabled.
+ pub fn is_enabled(&self, feature: Feature) -> bool {
+ self.0.contains(feature as usize)
}
}
+impl FromIterator<Feature> for Features {
+ fn from_iter<T: IntoIterator<Item = Feature>>(iter: T) -> Self {
+ let mut set = SmallBitSet::default();
+ for feature in iter {
+ set.insert(feature as usize);
+ }
+ Self(set)
+ }
+}
+
+/// An in-development feature that should be enabled.
+#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
+#[non_exhaustive]
+pub enum Feature {}
+
/// Construct the module with global definitions.
fn global(math: Module, inputs: Dict) -> Module {
let mut global = Scope::deduplicating();