summaryrefslogtreecommitdiff
path: root/cli/src/main.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2023-05-22 15:13:51 +0200
committerLaurenz <laurmaedje@gmail.com>2023-05-22 15:13:51 +0200
commit88553fe3c0459085c8f04e10c7c2ae2501e85b26 (patch)
tree5803dde47c91c0ccd2e8fbf82d33e0f13d65de59 /cli/src/main.rs
parent8aa0ae197d9f77f77dcf9de46b48a4ef7719d562 (diff)
Tidy up CLI
Diffstat (limited to 'cli/src/main.rs')
-rw-r--r--cli/src/main.rs111
1 files changed, 58 insertions, 53 deletions
diff --git a/cli/src/main.rs b/cli/src/main.rs
index 4e9bd43e..eb11cfc6 100644
--- a/cli/src/main.rs
+++ b/cli/src/main.rs
@@ -7,7 +7,6 @@ use std::fs::{self, File};
use std::hash::Hash;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
-use std::process;
use atty::Stream;
use clap::Parser;
@@ -30,12 +29,47 @@ use typst::World;
use walkdir::WalkDir;
use crate::args::{CliArguments, Command, CompileCommand};
-use crate::trace::init_tracing;
type CodespanResult<T> = Result<T, CodespanError>;
type CodespanError = codespan_reporting::files::Error;
-pub fn typst_version() -> &'static str {
+/// Entry point.
+fn main() {
+ let arguments = CliArguments::parse();
+ let _guard = match crate::trace::init_tracing(&arguments) {
+ Ok(guard) => guard,
+ Err(err) => {
+ eprintln!("failed to initialize tracing {}", err);
+ None
+ }
+ };
+
+ let res = match &arguments.command {
+ Command::Compile(_) | Command::Watch(_) => {
+ compile(CompileSettings::with_arguments(arguments))
+ }
+ Command::Fonts(_) => fonts(FontsSettings::with_arguments(arguments)),
+ };
+
+ if let Err(msg) = res {
+ print_error(&msg).expect("failed to print error");
+ }
+}
+
+/// Print an application-level error (independent from a source file).
+fn print_error(msg: &str) -> io::Result<()> {
+ let mut w = StandardStream::stderr(ColorChoice::Auto);
+ let styles = term::Styles::default();
+
+ w.set_color(&styles.header_error)?;
+ write!(w, "error")?;
+
+ w.reset()?;
+ writeln!(w, ": {msg}.")
+}
+
+/// Used by `args.rs`.
+fn typst_version() -> &'static str {
env!("TYPST_VERSION")
}
@@ -62,7 +96,7 @@ struct CompileSettings {
impl CompileSettings {
/// Create a new compile settings from the field values.
- pub fn new(
+ fn new(
input: PathBuf,
output: Option<PathBuf>,
watch: bool,
@@ -81,7 +115,7 @@ impl CompileSettings {
///
/// # Panics
/// Panics if the command is not a compile or watch command.
- pub fn with_arguments(args: CliArguments) -> Self {
+ fn with_arguments(args: CliArguments) -> Self {
let watch = matches!(args.command, Command::Watch(_));
let CompileCommand { input, output, open, .. } = match args.command {
Command::Compile(command) => command,
@@ -102,7 +136,7 @@ struct FontsSettings {
impl FontsSettings {
/// Create font settings from the field values.
- pub fn new(font_paths: Vec<PathBuf>, variants: bool) -> Self {
+ fn new(font_paths: Vec<PathBuf>, variants: bool) -> Self {
Self { font_paths, variants }
}
@@ -110,7 +144,7 @@ impl FontsSettings {
///
/// # Panics
/// Panics if the command is not a fonts command.
- pub fn with_arguments(args: CliArguments) -> Self {
+ fn with_arguments(args: CliArguments) -> Self {
match args.command {
Command::Fonts(command) => Self::new(args.font_paths, command.variants),
_ => unreachable!(),
@@ -118,41 +152,6 @@ impl FontsSettings {
}
}
-/// Entry point.
-fn main() {
- let arguments = CliArguments::parse();
- let _guard = match init_tracing(&arguments) {
- Ok(guard) => guard,
- Err(err) => {
- eprintln!("failed to initialize tracing, reason: {}", err);
- return;
- }
- };
-
- let res = match &arguments.command {
- Command::Compile(_) | Command::Watch(_) => {
- compile(CompileSettings::with_arguments(arguments))
- }
- Command::Fonts(_) => fonts(FontsSettings::with_arguments(arguments)),
- };
-
- if let Err(msg) = res {
- print_error(&msg).expect("failed to print error");
- }
-}
-
-/// Print an application-level error (independent from a source file).
-fn print_error(msg: &str) -> io::Result<()> {
- let mut w = StandardStream::stderr(ColorChoice::Auto);
- let styles = term::Styles::default();
-
- w.set_color(&styles.header_error)?;
- write!(w, "error")?;
-
- w.reset()?;
- writeln!(w, ": {msg}.")
-}
-
/// Execute a compilation command.
fn compile(mut command: CompileSettings) -> StrResult<()> {
let root = if let Some(root) = &command.root {
@@ -173,10 +172,11 @@ fn compile(mut command: CompileSettings) -> StrResult<()> {
let mut world = SystemWorld::new(root, &command.font_paths);
// Perform initial compilation.
- let failed = compile_once(&mut world, &command)?;
+ let ok = compile_once(&mut world, &command)?;
- // open the file if requested, this must be done on the first **successful** compilation
- if !failed {
+ // Open the file if requested, this must be done on the first **successful**
+ // compilation.
+ if ok {
if let Some(open) = command.open.take() {
open_file(open.as_deref(), &command.output)?;
}
@@ -184,8 +184,8 @@ fn compile(mut command: CompileSettings) -> StrResult<()> {
if !command.watch {
// Return with non-zero exit code in case of error.
- if failed {
- process::exit(1);
+ if !ok {
+ std::process::exit(1);
}
return Ok(());
@@ -223,18 +223,23 @@ fn compile(mut command: CompileSettings) -> StrResult<()> {
}
if recompile {
- compile_once(&mut world, &command)?;
+ let ok = compile_once(&mut world, &command)?;
comemo::evict(30);
- // open the file if requested, this must be done on the first **successful** compilation
- if let Some(open) = command.open.take() {
- open_file(open.as_deref(), &command.output)?;
+ // Ipen the file if requested, this must be done on the first
+ // **successful** compilation
+ if ok {
+ if let Some(open) = command.open.take() {
+ open_file(open.as_deref(), &command.output)?;
+ }
}
}
}
}
/// Compile a single time.
+///
+/// Returns whether it compiled without errors.
#[tracing::instrument(skip_all)]
fn compile_once(world: &mut SystemWorld, command: &CompileSettings) -> StrResult<bool> {
tracing::info!("Starting compilation");
@@ -252,7 +257,7 @@ fn compile_once(world: &mut SystemWorld, command: &CompileSettings) -> StrResult
status(command, Status::Success).unwrap();
tracing::info!("Compilation succeeded");
- Ok(false)
+ Ok(true)
}
// Print diagnostics.
@@ -262,7 +267,7 @@ fn compile_once(world: &mut SystemWorld, command: &CompileSettings) -> StrResult
.map_err(|_| "failed to print diagnostics")?;
tracing::info!("Compilation failed");
- Ok(true)
+ Ok(false)
}
}
}