summaryrefslogtreecommitdiff
path: root/crates/typst-cli
diff options
context:
space:
mode:
authorMyriad-Dreamin <35292584+Myriad-Dreamin@users.noreply.github.com>2024-07-14 21:14:21 +0800
committerGitHub <noreply@github.com>2024-07-14 13:14:21 +0000
commita3f3a1a83330e3fe9a686fbe4c0eda9f1e9e99b2 (patch)
tree0c6b10649101d75573e453498901907bdec5a8b3 /crates/typst-cli
parent4d8976b619fbb2ab19c1e46fccbca4294c0c2d0b (diff)
Change the signature of `World::main` (#4531)
Co-authored-by: Laurenz <laurmaedje@gmail.com>
Diffstat (limited to 'crates/typst-cli')
-rw-r--r--crates/typst-cli/src/compile.rs67
-rw-r--r--crates/typst-cli/src/world.rs4
2 files changed, 5 insertions, 66 deletions
diff --git a/crates/typst-cli/src/compile.rs b/crates/typst-cli/src/compile.rs
index 4b87c3bd..d1526425 100644
--- a/crates/typst-cli/src/compile.rs
+++ b/crates/typst-cli/src/compile.rs
@@ -5,16 +5,16 @@ use std::path::{Path, PathBuf};
use chrono::{Datelike, Timelike};
use codespan_reporting::diagnostic::{Diagnostic, Label};
use codespan_reporting::term;
-use ecow::{eco_format, eco_vec, EcoString, EcoVec};
+use ecow::{eco_format, EcoString};
use parking_lot::RwLock;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
-use typst::diag::{bail, FileError, Severity, SourceDiagnostic, StrResult, Warned};
+use typst::diag::{bail, Severity, SourceDiagnostic, StrResult, Warned};
use typst::foundations::{Datetime, Smart};
use typst::layout::{Frame, PageRanges};
use typst::model::Document;
use typst::syntax::{FileId, Source, Span};
use typst::visualize::Color;
-use typst::{World, WorldExt};
+use typst::WorldExt;
use crate::args::{
CompileCommand, DiagnosticFormat, Input, Output, OutputFormat, PageRangeArgument,
@@ -96,21 +96,6 @@ pub fn compile_once(
Status::Compiling.print(command).unwrap();
}
- if let Err(errors) = world
- .source(world.main())
- .map_err(|err| hint_invalid_main_file(err, &command.common.input))
- {
- set_failed();
- if watching {
- Status::Error.print(command).unwrap();
- }
-
- print_diagnostics(world, &errors, &[], command.common.diagnostic_format)
- .map_err(|err| eco_format!("failed to print diagnostics ({err})"))?;
-
- return Ok(());
- }
-
let Warned { output, warnings } = typst::compile(world);
match output {
@@ -498,52 +483,6 @@ fn open_file(open: Option<&str>, path: &Path) -> StrResult<()> {
}
}
-/// Adds useful hints when the main source file couldn't be read
-/// and returns the final diagnostic.
-fn hint_invalid_main_file(
- file_error: FileError,
- input: &Input,
-) -> EcoVec<SourceDiagnostic> {
- let is_utf8_error = matches!(file_error, FileError::InvalidUtf8);
- let mut diagnostic =
- SourceDiagnostic::error(Span::detached(), EcoString::from(file_error));
-
- // Attempt to provide helpful hints for UTF-8 errors.
- // Perhaps the user mistyped the filename.
- // For example, they could have written "file.pdf" instead of
- // "file.typ".
- if is_utf8_error {
- if let Input::Path(path) = input {
- let extension = path.extension();
- if extension.is_some_and(|extension| extension == "typ") {
- // No hints if the file is already a .typ file.
- // The file is indeed just invalid.
- return eco_vec![diagnostic];
- }
-
- match extension {
- Some(extension) => {
- diagnostic.hint(eco_format!(
- "a file with the `.{}` extension is not usually a Typst file",
- extension.to_string_lossy()
- ));
- }
-
- None => {
- diagnostic
- .hint("a file without an extension is not usually a Typst file");
- }
- };
-
- if path.with_extension("typ").exists() {
- diagnostic.hint("check if you meant to use the `.typ` extension instead");
- }
- }
- }
-
- eco_vec![diagnostic]
-}
-
/// Print diagnostic messages to the terminal.
pub fn print_diagnostics(
world: &SystemWorld,
diff --git a/crates/typst-cli/src/world.rs b/crates/typst-cli/src/world.rs
index 8e8b305f..5a0814a8 100644
--- a/crates/typst-cli/src/world.rs
+++ b/crates/typst-cli/src/world.rs
@@ -192,8 +192,8 @@ impl World for SystemWorld {
&self.book
}
- fn main(&self) -> Source {
- self.source(self.main).unwrap()
+ fn main(&self) -> FileId {
+ self.main
}
fn source(&self, id: FileId) -> FileResult<Source> {