summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorT0mstone <39707032+T0mstone@users.noreply.github.com>2024-07-08 21:32:35 +0200
committerGitHub <noreply@github.com>2024-07-08 19:32:35 +0000
commit86af5b5f61d4cbfdf0c40a84784b5c890c8b9a45 (patch)
tree513e18a6f44e7d07e5a39557a9af254b1cb4b119
parent59374f737079472aa5694b69ebced8c4cd5f9dfc (diff)
Allow non-utf8 values for `input` and `output` (#4517)
-rw-r--r--crates/typst-cli/src/args.rs44
1 files changed, 24 insertions, 20 deletions
diff --git a/crates/typst-cli/src/args.rs b/crates/typst-cli/src/args.rs
index ef12c48a..09189de8 100644
--- a/crates/typst-cli/src/args.rs
+++ b/crates/typst-cli/src/args.rs
@@ -5,7 +5,7 @@ use std::path::PathBuf;
use std::str::FromStr;
use chrono::{DateTime, Utc};
-use clap::builder::ValueParser;
+use clap::builder::{TypedValueParser, ValueParser};
use clap::{ArgAction, Args, ColorChoice, Parser, Subcommand, ValueEnum};
use semver::Version;
@@ -77,7 +77,7 @@ pub struct CompileCommand {
/// must be present if the source document renders to multiple pages. Use `{p}` for page
/// numbers, `{0p}` for zero padded page numbers and `{t}` for page count. For example,
/// `page-{0p}-of-{t}.png` creates `page-01-of-10.png`, `page-02-of-10.png` and so on.
- #[clap(required_if_eq("input", "-"), value_parser = ValueParser::new(output_value_parser))]
+ #[clap(required_if_eq("input", "-"), value_parser = make_output_value_parser())]
pub output: Option<Output>,
/// Which pages to export. When unspecified, all document pages are exported.
@@ -177,7 +177,7 @@ pub enum SerializationFormat {
#[derive(Debug, Clone, Args)]
pub struct SharedArgs {
/// Path to input Typst file. Use `-` to read input from stdin
- #[clap(value_parser = input_value_parser)]
+ #[clap(value_parser = make_input_value_parser())]
pub input: Input,
/// Configures the project root (for absolute paths)
@@ -279,26 +279,30 @@ impl Display for Output {
}
/// The clap value parser used by `SharedArgs.input`
-fn input_value_parser(value: &str) -> Result<Input, clap::error::Error> {
- if value.is_empty() {
- Err(clap::Error::new(clap::error::ErrorKind::InvalidValue))
- } else if value == "-" {
- Ok(Input::Stdin)
- } else {
- Ok(Input::Path(value.into()))
- }
+fn make_input_value_parser() -> impl TypedValueParser<Value = Input> {
+ clap::builder::OsStringValueParser::new().try_map(|value| {
+ if value.is_empty() {
+ Err(clap::Error::new(clap::error::ErrorKind::InvalidValue))
+ } else if value == "-" {
+ Ok(Input::Stdin)
+ } else {
+ Ok(Input::Path(value.into()))
+ }
+ })
}
/// The clap value parser used by `CompileCommand.output`
-fn output_value_parser(value: &str) -> Result<Output, clap::error::Error> {
- // Empty value also handled by clap for `Option<Output>`
- if value.is_empty() {
- Err(clap::Error::new(clap::error::ErrorKind::InvalidValue))
- } else if value == "-" {
- Ok(Output::Stdout)
- } else {
- Ok(Output::Path(value.into()))
- }
+fn make_output_value_parser() -> impl TypedValueParser<Value = Output> {
+ clap::builder::OsStringValueParser::new().try_map(|value| {
+ // Empty value also handled by clap for `Option<Output>`
+ if value.is_empty() {
+ Err(clap::Error::new(clap::error::ErrorKind::InvalidValue))
+ } else if value == "-" {
+ Ok(Output::Stdout)
+ } else {
+ Ok(Output::Path(value.into()))
+ }
+ })
}
/// Parses key/value pairs split by the first equal sign.