refactor: rename Data to Loaded

This commit is contained in:
Tobias Schmitz 2025-05-19 09:58:29 +02:00
parent ecda78b17c
commit a79120b668
No known key found for this signature in database
9 changed files with 38 additions and 38 deletions

View File

@ -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<PackageError> 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<SourceDiagnostic> {
let pos = LineCol::one_based(error.pos().row as usize, error.pos().col as usize);

View File

@ -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<SourceDiagnostic> {

View File

@ -74,7 +74,7 @@ pub trait Load {
}
impl Load for Spanned<DataSource> {
type Output = Data;
type Output = Loaded;
fn load(&self, world: Tracked<dyn World + '_>) -> SourceResult<Self::Output> {
self.as_ref().load(world)
@ -82,7 +82,7 @@ impl Load for Spanned<DataSource> {
}
impl Load for Spanned<&DataSource> {
type Output = Data;
type Output = Loaded;
fn load(&self, world: Tracked<dyn World + '_>) -> SourceResult<Self::Output> {
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<OneOrMultiple<DataSource>> {
type Output = Vec<Data>;
type Output = Vec<Loaded>;
fn load(&self, world: Tracked<dyn World + '_>) -> SourceResult<Self::Output> {
self.as_ref().load(world)
@ -109,7 +109,7 @@ impl Load for Spanned<OneOrMultiple<DataSource>> {
}
impl Load for Spanned<&OneOrMultiple<DataSource>> {
type Output = Vec<Data>;
type Output = Vec<Loaded>;
fn load(&self, world: Tracked<dyn World + '_>) -> SourceResult<Self::Output> {
self.v
@ -122,14 +122,14 @@ impl Load for Spanned<&OneOrMultiple<DataSource>> {
/// Data loaded from a [`DataSource`].
#[derive(Clone, Hash)]
pub struct Data {
pub struct Loaded {
pub source: Spanned<LoadSource>,
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<usize> {
}
}
/// 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 {

View File

@ -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<SourceDiagnostic> {
fn format_toml_error(data: &Loaded, error: ::toml::de::Error) -> EcoVec<SourceDiagnostic> {
let pos = error.span().map(ReportPos::Range).unwrap_or_default();
data.err_at(pos, "failed to parse TOML", error.message())
}

View File

@ -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<SourceDiagnostic> {
fn format_xml_error(data: &Loaded, error: roxmltree::Error) -> EcoVec<SourceDiagnostic> {
format_xml_like_error("XML", data, error)
}

View File

@ -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<SourceDiagnostic> {
let pos = error

View File

@ -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<Bibliography> {
fn decode(data: &[Loaded]) -> SourceResult<Bibliography> {
let mut map = IndexMap::new();
// TODO: store spans of entries for duplicate key error messages
let mut duplicates = Vec::<EcoString>::new();
@ -354,7 +354,7 @@ impl Debug for Bibliography {
}
/// Decode on library from one data source.
fn decode_library(data: &Data) -> SourceResult<Library> {
fn decode_library(data: &Loaded) -> SourceResult<Library> {
let str = data.as_str()?;
if let LoadSource::Path(file_id) = data.source.v {
@ -419,7 +419,7 @@ fn decode_library(data: &Data) -> SourceResult<Library> {
/// Format a BibLaTeX loading error.
fn format_biblatex_error(
data: &Data,
data: &Loaded,
errors: Vec<BibLaTeXError>,
) -> EcoVec<SourceDiagnostic> {
// 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<CslStyle> {
pub fn from_data(data: &Loaded) -> SourceResult<CslStyle> {
let text = data.as_str()?;
citationberg::IndependentStyle::from_xml(text)
.map(|style| {

View File

@ -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<RawSyntax> {
fn decode(data: &Loaded) -> SourceResult<RawSyntax> {
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<SourceDiagnostic> {
fn format_syntax_error(data: &Loaded, error: ParseSyntaxError) -> EcoVec<SourceDiagnostic> {
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<RawTheme> {
fn decode(data: &Loaded) -> SourceResult<RawTheme> {
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<SourceDiagnostic> {
let pos = match &error {

View File

@ -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