diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-02-04 11:22:00 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-02-04 11:22:00 +0100 |
| commit | 5c11aa72239ecbdd9577f027bdc7e9468d68414e (patch) | |
| tree | 66d846fb58f38e564eca385a6f86ee8154c503ce /src | |
| parent | f655656fb8cb6135b26e7960ce0b7adf96d6f567 (diff) | |
Adapt for tonty and fix a few bugs 🚧
Diffstat (limited to 'src')
| -rw-r--r-- | src/bin/main.rs | 7 | ||||
| -rw-r--r-- | src/error.rs | 35 | ||||
| -rw-r--r-- | src/func.rs | 35 | ||||
| -rw-r--r-- | src/lib.rs | 32 | ||||
| -rw-r--r-- | src/library/font.rs | 2 | ||||
| -rw-r--r-- | src/syntax/func/keys.rs | 2 | ||||
| -rw-r--r-- | src/syntax/func/values.rs | 4 |
7 files changed, 60 insertions, 57 deletions
diff --git a/src/bin/main.rs b/src/bin/main.rs index 11f30880..92a83dff 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -1,9 +1,10 @@ +use std::error::Error; use std::fs::{File, read_to_string}; use std::io::BufWriter; use std::path::{Path, PathBuf}; use futures_executor::block_on; -use typstc::{Typesetter, DynErrorProvider}; +use typstc::{Typesetter, DebugErrorProvider}; use typstc::toddle::query::fs::EagerFsProvider; use typstc::export::pdf; @@ -15,7 +16,7 @@ fn main() { } } -fn run() -> Result<(), Box<dyn std::error::Error>> { +fn run() -> Result<(), Box<dyn Error>> { let args: Vec<String> = std::env::args().collect(); if args.len() < 2 || args.len() > 3 { println!("usage: {} source [destination]", @@ -38,7 +39,7 @@ fn run() -> Result<(), Box<dyn std::error::Error>> { .map_err(|_| "failed to read from source file")?; let (fs, entries) = EagerFsProvider::from_index("../fonts", "index.json")?; - let provider = DynErrorProvider::new(fs); + let provider = DebugErrorProvider::new(fs); let typesetter = Typesetter::new((Box::new(provider), entries)); let layouts = block_on(typesetter.typeset(&src)); diff --git a/src/error.rs b/src/error.rs index 1eb48deb..3a095eef 100644 --- a/src/error.rs +++ b/src/error.rs @@ -35,3 +35,38 @@ impl Error { Error { message: message.into(), severity } } } + +/// Construct an error with formatted message and optionally severity and / or +/// span. +/// +/// # Examples +/// ``` +/// # use typstc::err; +/// # use typstc::syntax::span::Span; +/// # let span = Span::ZERO; +/// # let value = 0; +/// +/// // With span and default severity `Error`. +/// err!(span; "the wrong {}", value); +/// +/// // With no span and severity `Warning`. +/// err!(@Warning: span; "non-fatal!"); +/// +/// // Without span and default severity. +/// err!("no spans here ..."); +/// ``` +#[macro_export] +macro_rules! err { + (@$severity:ident: $span:expr; $($args:tt)*) => { + $crate::syntax::span::Spanned { v: err!(@$severity: $($args)*), span: $span } + }; + + (@$severity:ident: $($args:tt)*) => { + $crate::error::Error { + message: format!($($args)*), + severity: $crate::error::Severity::$severity, + } + }; + + ($($tts:tt)*) => { err!(@Error: $($tts)*) }; +} diff --git a/src/func.rs b/src/func.rs index 63d7e8ef..590d9ed3 100644 --- a/src/func.rs +++ b/src/func.rs @@ -198,38 +198,3 @@ macro_rules! body { } }; } - -/// Construct an error with formatted message and optionally severity and / or -/// span. -/// -/// # Examples -/// ``` -/// # use typstc::err; -/// # use typstc::syntax::span::Span; -/// # let span = Span::ZERO; -/// # let value = 0; -/// -/// // With span and default severity `Error`. -/// err!(span; "the wrong {}", value); -/// -/// // With no span and severity `Warning`. -/// err!(@Warning: span; "non-fatal!"); -/// -/// // Without span and default severity. -/// err!("no spans here ..."); -/// ``` -#[macro_export] -macro_rules! err { - (@$severity:ident: $span:expr; $($args:tt)*) => { - $crate::syntax::span::Spanned { v: err!(@Error: $($args)*), span: $span } - }; - - (@$severity:ident: $($args:tt)*) => { - $crate::error::Error { - message: format!($($args)*), - severity: $crate::error::Severity::$severity, - } - }; - - ($($tts:tt)*) => { err!(@Error: $($tts)*) }; -} @@ -19,7 +19,7 @@ pub use toddle; use std::cell::RefCell; -use std::error::Error; +use std::fmt::Debug; use async_trait::async_trait; use smallvec::smallvec; @@ -40,6 +40,7 @@ macro_rules! pub_use_mod { }; } +#[macro_use] pub mod error; pub mod export; #[macro_use] @@ -71,7 +72,7 @@ pub struct Typesetter { pub type GlobalFontLoader = SharedFontLoader<GlobalProvider>; /// The provider type of font loaders used in the [`Typesetter`]. -pub type GlobalProvider = Box<dyn FontProvider<Data=OwnedData, Error=Box<dyn Error>>>; +pub type GlobalProvider = Box<dyn FontProvider<Data=OwnedData, Error=Box<dyn Debug>>>; impl Typesetter { /// Create a new typesetter. @@ -135,29 +136,30 @@ impl Typesetter { } } -/// Wraps a font provider and transforms its errors into boxed trait objects. -/// This enables font providers that do not return boxed errors to be used with -/// the typesetter. +/// Wraps a font provider and transforms its errors into boxed [`Debug`] trait +/// objects. This enables font providers that do not return these boxed errors +/// to be used with the typesetter. #[derive(Debug)] -pub struct DynErrorProvider<P> { +pub struct DebugErrorProvider<P> { provider: P, } -impl<P> DynErrorProvider<P> -where P: FontProvider, P::Error: Error + 'static { - /// Create a new dynamic error provider from any provider. - pub fn new(provider: P) -> DynErrorProvider<P> { - DynErrorProvider { provider } +impl<P> DebugErrorProvider<P> +where P: FontProvider, P::Error: Debug + 'static { + /// Create a new debug error provider from any provider. + pub fn new(provider: P) -> DebugErrorProvider<P> { + DebugErrorProvider { provider } } } #[async_trait(?Send)] -impl<P> FontProvider for DynErrorProvider<P> -where P: FontProvider, P::Error: Error + 'static { +impl<P> FontProvider for DebugErrorProvider<P> +where P: FontProvider, P::Error: Debug + 'static { type Data = P::Data; - type Error = Box<dyn Error>; + type Error = Box<dyn Debug>; async fn load(&self, index: usize, variant: usize) -> Result<Font<P::Data>, Self::Error> { - Ok(self.provider.load(index, variant).await?) + self.provider.load(index, variant).await + .map_err(|d| Box::new(d) as Box<dyn Debug>) } } diff --git a/src/library/font.rs b/src/library/font.rs index be4d263f..422a68f9 100644 --- a/src/library/font.rs +++ b/src/library/font.rs @@ -15,7 +15,7 @@ function! { FontFamilyFunc { body: body!(opt: body, ctx, errors, decos), list: header.args.pos.get_all::<StringLike>(errors) - .map(Into::into) + .map(|s| s.0.to_lowercase()) .collect(), } } diff --git a/src/syntax/func/keys.rs b/src/syntax/func/keys.rs index cb12ee1b..b8f142ee 100644 --- a/src/syntax/func/keys.rs +++ b/src/syntax/func/keys.rs @@ -105,7 +105,7 @@ key!(AxisKey, /// A key which is equivalent to a [`AxisKey`] but uses typical extent keywords /// instead of axis keywords, e.g. `width` instead of `horizontal`. #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] -pub struct ExtentKey(AxisKey); +pub struct ExtentKey(pub AxisKey); key!(ExtentKey, "width" | "w" => ExtentKey(Specific(Horizontal)), diff --git a/src/syntax/func/values.rs b/src/syntax/func/values.rs index 18d451e3..d2b54a0b 100644 --- a/src/syntax/func/values.rs +++ b/src/syntax/func/values.rs @@ -87,7 +87,7 @@ value!(ScaleSize, "number or size", /// A value type that matches [`Expr::Ident`] and [`Expr::Str`] and implements /// `Into<String>`. -pub struct StringLike(String); +pub struct StringLike(pub String); value!(StringLike, "identifier or string", Expr::Ident(Ident(s)) => StringLike(s), @@ -117,7 +117,7 @@ impl From<StringLike> for String { /// [func: size=default] => None /// [func: size=2cm] => Some(Size::cm(2.0)) /// ``` -pub struct Defaultable<V>(Option<V>); +pub struct Defaultable<V>(pub Option<V>); impl<V: Value> Value for Defaultable<V> { fn parse(expr: Spanned<Expr>) -> Result<Self, Error> { |
