summaryrefslogtreecommitdiff
path: root/crates/typst-cli/src
diff options
context:
space:
mode:
authorPgBiel <9021226+PgBiel@users.noreply.github.com>2024-06-04 12:24:39 -0300
committerGitHub <noreply@github.com>2024-06-04 15:24:39 +0000
commit9adcd9a1f894b212e2757277781955a7d5b8bba8 (patch)
tree6e8b67d8ec9482fca04a87793b8d7c7698d78e12 /crates/typst-cli/src
parentd360e753bccf996819392539aaaa031f458559e9 (diff)
Refactor casts to use `HintedStrResult` (#4229)
Diffstat (limited to 'crates/typst-cli/src')
-rw-r--r--crates/typst-cli/src/main.rs30
-rw-r--r--crates/typst-cli/src/query.rs6
2 files changed, 22 insertions, 14 deletions
diff --git a/crates/typst-cli/src/main.rs b/crates/typst-cli/src/main.rs
index c8bd3914..bc1c30a6 100644
--- a/crates/typst-cli/src/main.rs
+++ b/crates/typst-cli/src/main.rs
@@ -20,6 +20,7 @@ use clap::Parser;
use codespan_reporting::term;
use codespan_reporting::term::termcolor::WriteColor;
use once_cell::sync::Lazy;
+use typst::diag::HintedStrResult;
use crate::args::{CliArguments, Command};
use crate::timings::Timer;
@@ -34,25 +35,32 @@ static ARGS: Lazy<CliArguments> = Lazy::new(CliArguments::parse);
/// Entry point.
fn main() -> ExitCode {
- let timer = Timer::new(&ARGS);
-
- let res = match &ARGS.command {
- Command::Compile(command) => crate::compile::compile(timer, command.clone()),
- Command::Watch(command) => crate::watch::watch(timer, command.clone()),
- Command::Init(command) => crate::init::init(command),
- Command::Query(command) => crate::query::query(command),
- Command::Fonts(command) => crate::fonts::fonts(command),
- Command::Update(command) => crate::update::update(command),
- };
+ let res = dispatch();
if let Err(msg) = res {
set_failed();
- print_error(&msg).expect("failed to print error");
+ print_error(msg.message()).expect("failed to print error");
}
EXIT.with(|cell| cell.get())
}
+/// Execute the requested command.
+fn dispatch() -> HintedStrResult<()> {
+ let timer = Timer::new(&ARGS);
+
+ match &ARGS.command {
+ Command::Compile(command) => crate::compile::compile(timer, command.clone())?,
+ Command::Watch(command) => crate::watch::watch(timer, command.clone())?,
+ Command::Init(command) => crate::init::init(command)?,
+ Command::Query(command) => crate::query::query(command)?,
+ Command::Fonts(command) => crate::fonts::fonts(command)?,
+ Command::Update(command) => crate::update::update(command)?,
+ }
+
+ Ok(())
+}
+
/// Ensure a failure exit code.
fn set_failed() {
EXIT.with(|cell| cell.set(ExitCode::FAILURE));
diff --git a/crates/typst-cli/src/query.rs b/crates/typst-cli/src/query.rs
index 6422af94..2e8bf7f8 100644
--- a/crates/typst-cli/src/query.rs
+++ b/crates/typst-cli/src/query.rs
@@ -1,7 +1,7 @@
use comemo::Track;
use ecow::{eco_format, EcoString};
use serde::Serialize;
-use typst::diag::{bail, StrResult};
+use typst::diag::{bail, HintedStrResult, StrResult};
use typst::eval::{eval_string, EvalMode, Tracer};
use typst::foundations::{Content, IntoValue, LocatableSelector, Scope};
use typst::model::Document;
@@ -14,7 +14,7 @@ use crate::set_failed;
use crate::world::SystemWorld;
/// Execute a query command.
-pub fn query(command: &QueryCommand) -> StrResult<()> {
+pub fn query(command: &QueryCommand) -> HintedStrResult<()> {
let mut world = SystemWorld::new(&command.common)?;
// Reset everything and ensure that the main file is present.
@@ -56,7 +56,7 @@ fn retrieve(
world: &dyn World,
command: &QueryCommand,
document: &Document,
-) -> StrResult<Vec<Content>> {
+) -> HintedStrResult<Vec<Content>> {
let selector = eval_string(
world.track(),
&command.selector,