From a79120b668fc50cf3710b2628069df494bd400bf Mon Sep 17 00:00:00 2001 From: Tobias Schmitz Date: Mon, 19 May 2025 09:58:29 +0200 Subject: [PATCH] refactor: rename Data to Loaded --- crates/typst-library/src/diag.rs | 4 +-- crates/typst-library/src/loading/csv.rs | 4 +-- crates/typst-library/src/loading/mod.rs | 32 +++++++++---------- crates/typst-library/src/loading/toml.rs | 4 +-- crates/typst-library/src/loading/xml.rs | 4 +-- crates/typst-library/src/loading/yaml.rs | 4 +-- .../typst-library/src/model/bibliography.rs | 10 +++--- crates/typst-library/src/text/raw.rs | 10 +++--- .../typst-library/src/visualize/image/svg.rs | 4 +-- 9 files changed, 38 insertions(+), 38 deletions(-) diff --git a/crates/typst-library/src/diag.rs b/crates/typst-library/src/diag.rs index 75ae1b45d..75dc52798 100644 --- a/crates/typst-library/src/diag.rs +++ b/crates/typst-library/src/diag.rs @@ -12,7 +12,7 @@ use typst_syntax::package::{PackageSpec, PackageVersion}; use typst_syntax::{Span, Spanned, SyntaxError}; use crate::engine::Engine; -use crate::loading::{Data, LineCol}; +use crate::loading::{Loaded, LineCol}; use crate::{World, WorldExt}; /// Early-return with a [`StrResult`] or [`SourceResult`]. @@ -572,7 +572,7 @@ impl From for EcoString { /// Format a user-facing error message for an XML-like file format. pub fn format_xml_like_error( format: &str, - data: &Data, + data: &Loaded, error: roxmltree::Error, ) -> EcoVec { let pos = LineCol::one_based(error.pos().row as usize, error.pos().col as usize); diff --git a/crates/typst-library/src/loading/csv.rs b/crates/typst-library/src/loading/csv.rs index d2892fb63..56e316e14 100644 --- a/crates/typst-library/src/loading/csv.rs +++ b/crates/typst-library/src/loading/csv.rs @@ -4,7 +4,7 @@ use typst_syntax::Spanned; use crate::diag::{bail, SourceDiagnostic, SourceResult}; use crate::engine::Engine; use crate::foundations::{cast, func, scope, Array, Dict, IntoValue, Type, Value}; -use crate::loading::{Data, DataSource, LineCol, Load, Readable, ReportPos}; +use crate::loading::{Loaded, DataSource, LineCol, Load, Readable, ReportPos}; /// Reads structured data from a CSV file. /// @@ -164,7 +164,7 @@ cast! { /// Format the user-facing CSV error message. fn format_csv_error( - data: &Data, + data: &Loaded, err: ::csv::Error, line: usize, ) -> EcoVec { diff --git a/crates/typst-library/src/loading/mod.rs b/crates/typst-library/src/loading/mod.rs index 28e2fb45f..580a27294 100644 --- a/crates/typst-library/src/loading/mod.rs +++ b/crates/typst-library/src/loading/mod.rs @@ -74,7 +74,7 @@ pub trait Load { } impl Load for Spanned { - type Output = Data; + type Output = Loaded; fn load(&self, world: Tracked) -> SourceResult { self.as_ref().load(world) @@ -82,7 +82,7 @@ impl Load for Spanned { } impl Load for Spanned<&DataSource> { - type Output = Data; + type Output = Loaded; fn load(&self, world: Tracked) -> SourceResult { match &self.v { @@ -90,18 +90,18 @@ impl Load for Spanned<&DataSource> { let file_id = self.span.resolve_path(path).at(self.span)?; let bytes = world.file(file_id).at(self.span)?; let source = Spanned::new(LoadSource::Path(file_id), self.span); - Ok(Data::new(source, bytes)) + Ok(Loaded::new(source, bytes)) } DataSource::Bytes(bytes) => { let source = Spanned::new(LoadSource::Bytes, self.span); - Ok(Data::new(source, bytes.clone())) + Ok(Loaded::new(source, bytes.clone())) } } } } impl Load for Spanned> { - type Output = Vec; + type Output = Vec; fn load(&self, world: Tracked) -> SourceResult { self.as_ref().load(world) @@ -109,7 +109,7 @@ impl Load for Spanned> { } impl Load for Spanned<&OneOrMultiple> { - type Output = Vec; + type Output = Vec; fn load(&self, world: Tracked) -> SourceResult { self.v @@ -122,14 +122,14 @@ impl Load for Spanned<&OneOrMultiple> { /// Data loaded from a [`DataSource`]. #[derive(Clone, Hash)] -pub struct Data { +pub struct Loaded { pub source: Spanned, pub bytes: Bytes, } -impl Data { +impl Loaded { pub fn dummy() -> Self { - Data::new( + Loaded::new( typst_syntax::Spanned::new(LoadSource::Bytes, Span::detached()), Bytes::new([]), ) @@ -187,6 +187,13 @@ impl Data { } } +/// A loaded [`DataSource`]. +#[derive(Clone, Copy, Hash)] +pub enum LoadSource { + Path(FileId), + Bytes, +} + #[derive(Debug, Default)] pub enum ReportPos { /// Contains the range, and the 0-based line/column. @@ -329,13 +336,6 @@ fn col_offset(line_offset: usize, col: usize, bytes: &[u8]) -> Option { } } -/// A loaded [`DataSource`]. -#[derive(Clone, Copy, Hash)] -pub enum LoadSource { - Path(FileId), - Bytes, -} - /// A value that can be read from a file. #[derive(Debug, Clone, PartialEq, Hash)] pub enum Readable { diff --git a/crates/typst-library/src/loading/toml.rs b/crates/typst-library/src/loading/toml.rs index 0bca7cc3d..791f664e9 100644 --- a/crates/typst-library/src/loading/toml.rs +++ b/crates/typst-library/src/loading/toml.rs @@ -4,7 +4,7 @@ use typst_syntax::Spanned; use crate::diag::{At, SourceDiagnostic, SourceResult}; use crate::engine::Engine; use crate::foundations::{func, scope, Str, Value}; -use crate::loading::{Data, DataSource, Load, Readable, ReportPos}; +use crate::loading::{Loaded, DataSource, Load, Readable, ReportPos}; /// Reads structured data from a TOML file. /// @@ -69,7 +69,7 @@ impl toml { } /// Format the user-facing TOML error message. -fn format_toml_error(data: &Data, error: ::toml::de::Error) -> EcoVec { +fn format_toml_error(data: &Loaded, error: ::toml::de::Error) -> EcoVec { let pos = error.span().map(ReportPos::Range).unwrap_or_default(); data.err_at(pos, "failed to parse TOML", error.message()) } diff --git a/crates/typst-library/src/loading/xml.rs b/crates/typst-library/src/loading/xml.rs index d2aa97dbc..54fc9062e 100644 --- a/crates/typst-library/src/loading/xml.rs +++ b/crates/typst-library/src/loading/xml.rs @@ -5,7 +5,7 @@ use typst_syntax::Spanned; use crate::diag::{format_xml_like_error, SourceDiagnostic, SourceResult}; use crate::engine::Engine; use crate::foundations::{dict, func, scope, Array, Dict, IntoValue, Str, Value}; -use crate::loading::{Data, DataSource, Load, Readable}; +use crate::loading::{Loaded, DataSource, Load, Readable}; /// Reads structured data from an XML file. /// @@ -110,6 +110,6 @@ fn convert_xml(node: roxmltree::Node) -> Value { } /// Format the user-facing XML error message. -fn format_xml_error(data: &Data, error: roxmltree::Error) -> EcoVec { +fn format_xml_error(data: &Loaded, error: roxmltree::Error) -> EcoVec { format_xml_like_error("XML", data, error) } diff --git a/crates/typst-library/src/loading/yaml.rs b/crates/typst-library/src/loading/yaml.rs index 8e0a266d5..bc0009960 100644 --- a/crates/typst-library/src/loading/yaml.rs +++ b/crates/typst-library/src/loading/yaml.rs @@ -4,7 +4,7 @@ use typst_syntax::Spanned; use crate::diag::{At, SourceDiagnostic, SourceResult}; use crate::engine::Engine; use crate::foundations::{func, scope, Str, Value}; -use crate::loading::{Data, DataSource, LineCol, Load, Readable, ReportPos}; +use crate::loading::{Loaded, DataSource, LineCol, Load, Readable, ReportPos}; /// Reads structured data from a YAML file. /// @@ -77,7 +77,7 @@ impl yaml { } pub fn format_yaml_error( - data: &Data, + data: &Loaded, error: serde_yaml::Error, ) -> EcoVec { let pos = error diff --git a/crates/typst-library/src/model/bibliography.rs b/crates/typst-library/src/model/bibliography.rs index e1b9af77a..20ddcabf9 100644 --- a/crates/typst-library/src/model/bibliography.rs +++ b/crates/typst-library/src/model/bibliography.rs @@ -33,7 +33,7 @@ use crate::layout::{ BlockBody, BlockElem, Em, GridCell, GridChild, GridElem, GridItem, HElem, PadElem, Sides, Sizing, TrackSizings, }; -use crate::loading::{format_yaml_error, Data, DataSource, Load, LoadSource, ReportPos}; +use crate::loading::{format_yaml_error, Loaded, DataSource, Load, LoadSource, ReportPos}; use crate::model::{ CitationForm, CiteGroup, Destination, FootnoteElem, HeadingElem, LinkElem, ParElem, Url, @@ -304,7 +304,7 @@ impl Bibliography { /// Decode a bibliography from loaded data sources. #[comemo::memoize] #[typst_macros::time(name = "load bibliography")] - fn decode(data: &[Data]) -> SourceResult { + fn decode(data: &[Loaded]) -> SourceResult { let mut map = IndexMap::new(); // TODO: store spans of entries for duplicate key error messages let mut duplicates = Vec::::new(); @@ -354,7 +354,7 @@ impl Debug for Bibliography { } /// Decode on library from one data source. -fn decode_library(data: &Data) -> SourceResult { +fn decode_library(data: &Loaded) -> SourceResult { let str = data.as_str()?; if let LoadSource::Path(file_id) = data.source.v { @@ -419,7 +419,7 @@ fn decode_library(data: &Data) -> SourceResult { /// Format a BibLaTeX loading error. fn format_biblatex_error( - data: &Data, + data: &Loaded, errors: Vec, ) -> EcoVec { // TODO: return multiple errors? @@ -471,7 +471,7 @@ impl CslStyle { /// Load a CSL style from file contents. #[comemo::memoize] - pub fn from_data(data: &Data) -> SourceResult { + pub fn from_data(data: &Loaded) -> SourceResult { let text = data.as_str()?; citationberg::IndependentStyle::from_xml(text) .map(|style| { diff --git a/crates/typst-library/src/text/raw.rs b/crates/typst-library/src/text/raw.rs index 43a069605..e3860c64b 100644 --- a/crates/typst-library/src/text/raw.rs +++ b/crates/typst-library/src/text/raw.rs @@ -19,7 +19,7 @@ use crate::foundations::{ }; use crate::html::{tag, HtmlElem}; use crate::layout::{BlockBody, BlockElem, Em, HAlignment}; -use crate::loading::{Data, DataSource, LineCol, Load, ReportPos}; +use crate::loading::{Loaded, DataSource, LineCol, Load, ReportPos}; use crate::model::{Figurable, ParElem}; use crate::text::{FontFamily, FontList, LinebreakElem, LocalName, TextElem, TextSize}; use crate::visualize::Color; @@ -547,7 +547,7 @@ impl RawSyntax { /// Decode a syntax from a loaded source. #[comemo::memoize] #[typst_macros::time(name = "load syntaxes")] - fn decode(data: &Data) -> SourceResult { + fn decode(data: &Loaded) -> SourceResult { let str = data.as_str()?; let syntax = SyntaxDefinition::load_from_str(str, false, None) @@ -568,7 +568,7 @@ impl RawSyntax { } } -fn format_syntax_error(data: &Data, error: ParseSyntaxError) -> EcoVec { +fn format_syntax_error(data: &Loaded, error: ParseSyntaxError) -> EcoVec { let pos = syntax_error_pos(&error); data.err_at(pos, "failed to parse syntax", error) } @@ -603,7 +603,7 @@ impl RawTheme { /// Decode a theme from bytes. #[comemo::memoize] - fn decode(data: &Data) -> SourceResult { + fn decode(data: &Loaded) -> SourceResult { let mut cursor = std::io::Cursor::new(data.bytes.as_slice()); let theme = synt::ThemeSet::load_from_reader(&mut cursor) .map_err(|err| format_theme_error(data, err))?; @@ -617,7 +617,7 @@ impl RawTheme { } fn format_theme_error( - data: &Data, + data: &Loaded, error: syntect::LoadingError, ) -> EcoVec { let pos = match &error { diff --git a/crates/typst-library/src/visualize/image/svg.rs b/crates/typst-library/src/visualize/image/svg.rs index 8683dc37e..ad22d5fd3 100644 --- a/crates/typst-library/src/visualize/image/svg.rs +++ b/crates/typst-library/src/visualize/image/svg.rs @@ -9,7 +9,7 @@ use siphasher::sip128::{Hasher128, SipHasher13}; use crate::diag::{format_xml_like_error, StrResult}; use crate::foundations::Bytes; use crate::layout::Axes; -use crate::loading::Data; +use crate::loading::Loaded; use crate::text::{ Font, FontBook, FontFlags, FontStretch, FontStyle, FontVariant, FontWeight, }; @@ -135,7 +135,7 @@ fn format_usvg_error(error: usvg::Error) -> EcoString { "failed to parse SVG (width, height, or viewbox is invalid)".into() } usvg::Error::ParsingFailed(error) => { - format_xml_like_error("SVG", &Data::dummy(), error) + format_xml_like_error("SVG", &Loaded::dummy(), error) .pop() .unwrap() .message