From 9adcd9a1f894b212e2757277781955a7d5b8bba8 Mon Sep 17 00:00:00 2001 From: PgBiel <9021226+PgBiel@users.noreply.github.com> Date: Tue, 4 Jun 2024 12:24:39 -0300 Subject: Refactor casts to use `HintedStrResult` (#4229) --- crates/typst-cli/src/main.rs | 30 +++++++++++++++++++----------- crates/typst-cli/src/query.rs | 6 +++--- 2 files changed, 22 insertions(+), 14 deletions(-) (limited to 'crates/typst-cli/src') 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 = 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> { +) -> HintedStrResult> { let selector = eval_string( world.track(), &command.selector, -- cgit v1.2.3