From a8a2014422789e8a8117656efc16391c3a38b968 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Tue, 10 Jun 2025 14:28:49 +0200 Subject: [PATCH] Directly use string conversion methods on `Bytes` This lets `read` reuse the string for `Str`-backed `Bytes`. Also makes `within` more generally applicable just like `at`. --- crates/typst-library/src/diag.rs | 10 ++++++++-- crates/typst-library/src/loading/mod.rs | 4 ---- crates/typst-library/src/loading/read.rs | 4 ++-- crates/typst-library/src/loading/toml.rs | 2 +- crates/typst-library/src/loading/xml.rs | 2 +- crates/typst-library/src/model/bibliography.rs | 2 +- 6 files changed, 13 insertions(+), 11 deletions(-) diff --git a/crates/typst-library/src/diag.rs b/crates/typst-library/src/diag.rs index bbf05dcf9..f1243f4f0 100644 --- a/crates/typst-library/src/diag.rs +++ b/crates/typst-library/src/diag.rs @@ -617,9 +617,15 @@ pub trait LoadedWithin { fn within(self, loaded: &Loaded) -> SourceResult; } -impl LoadedWithin for Result { +impl LoadedWithin for Result +where + E: Into, +{ fn within(self, loaded: &Loaded) -> SourceResult { - self.map_err(|err| load_err_in_text(loaded, err.pos, err.message)) + self.map_err(|err| { + let LoadError { pos, message } = err.into(); + load_err_in_text(loaded, pos, message) + }) } } diff --git a/crates/typst-library/src/loading/mod.rs b/crates/typst-library/src/loading/mod.rs index c1cf8aef7..4f8eb0a4e 100644 --- a/crates/typst-library/src/loading/mod.rs +++ b/crates/typst-library/src/loading/mod.rs @@ -131,10 +131,6 @@ impl Loaded { pub fn new(source: Spanned, bytes: Bytes) -> Self { Self { source, data: bytes } } - - pub fn load_str(&self) -> SourceResult<&str> { - self.data.as_str().map_err(Into::into).within(self) - } } /// A loaded [`DataSource`]. diff --git a/crates/typst-library/src/loading/read.rs b/crates/typst-library/src/loading/read.rs index f28313775..91e6e4366 100644 --- a/crates/typst-library/src/loading/read.rs +++ b/crates/typst-library/src/loading/read.rs @@ -1,7 +1,7 @@ use ecow::EcoString; use typst_syntax::Spanned; -use crate::diag::SourceResult; +use crate::diag::{LoadedWithin, SourceResult}; use crate::engine::Engine; use crate::foundations::{func, Cast}; use crate::loading::{DataSource, Load, Readable}; @@ -38,7 +38,7 @@ pub fn read( let loaded = path.map(DataSource::Path).load(engine.world)?; Ok(match encoding { None => Readable::Bytes(loaded.data), - Some(Encoding::Utf8) => Readable::Str(loaded.load_str()?.into()), + Some(Encoding::Utf8) => Readable::Str(loaded.data.to_str().within(&loaded)?), }) } diff --git a/crates/typst-library/src/loading/toml.rs b/crates/typst-library/src/loading/toml.rs index c1264ee2d..a4252feca 100644 --- a/crates/typst-library/src/loading/toml.rs +++ b/crates/typst-library/src/loading/toml.rs @@ -33,7 +33,7 @@ pub fn toml( source: Spanned, ) -> SourceResult { let loaded = source.load(engine.world)?; - let raw = loaded.load_str()?; + let raw = loaded.data.as_str().within(&loaded)?; ::toml::from_str(raw).map_err(format_toml_error).within(&loaded) } diff --git a/crates/typst-library/src/loading/xml.rs b/crates/typst-library/src/loading/xml.rs index 12b9c74a9..0023c5df5 100644 --- a/crates/typst-library/src/loading/xml.rs +++ b/crates/typst-library/src/loading/xml.rs @@ -61,7 +61,7 @@ pub fn xml( source: Spanned, ) -> SourceResult { let loaded = source.load(engine.world)?; - let text = loaded.load_str()?; + let text = loaded.data.as_str().within(&loaded)?; let document = roxmltree::Document::parse_with_options( text, ParsingOptions { allow_dtd: true, ..Default::default() }, diff --git a/crates/typst-library/src/model/bibliography.rs b/crates/typst-library/src/model/bibliography.rs index b15e6459a..114356575 100644 --- a/crates/typst-library/src/model/bibliography.rs +++ b/crates/typst-library/src/model/bibliography.rs @@ -356,7 +356,7 @@ impl Debug for Bibliography { /// Decode on library from one data source. fn decode_library(loaded: &Loaded) -> SourceResult { - let data = loaded.load_str()?; + let data = loaded.data.as_str().within(loaded)?; if let LoadSource::Path(file_id) = loaded.source.v { // If we got a path, use the extension to determine whether it is