mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +08:00
Allow non-utf8 values for input
and output
(#4517)
This commit is contained in:
parent
59374f7370
commit
86af5b5f61
@ -5,7 +5,7 @@ use std::path::PathBuf;
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use clap::builder::ValueParser;
|
use clap::builder::{TypedValueParser, ValueParser};
|
||||||
use clap::{ArgAction, Args, ColorChoice, Parser, Subcommand, ValueEnum};
|
use clap::{ArgAction, Args, ColorChoice, Parser, Subcommand, ValueEnum};
|
||||||
use semver::Version;
|
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
|
/// 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,
|
/// 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.
|
/// `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>,
|
pub output: Option<Output>,
|
||||||
|
|
||||||
/// Which pages to export. When unspecified, all document pages are exported.
|
/// Which pages to export. When unspecified, all document pages are exported.
|
||||||
@ -177,7 +177,7 @@ pub enum SerializationFormat {
|
|||||||
#[derive(Debug, Clone, Args)]
|
#[derive(Debug, Clone, Args)]
|
||||||
pub struct SharedArgs {
|
pub struct SharedArgs {
|
||||||
/// Path to input Typst file. Use `-` to read input from stdin
|
/// 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,
|
pub input: Input,
|
||||||
|
|
||||||
/// Configures the project root (for absolute paths)
|
/// Configures the project root (for absolute paths)
|
||||||
@ -279,7 +279,8 @@ impl Display for Output {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// The clap value parser used by `SharedArgs.input`
|
/// The clap value parser used by `SharedArgs.input`
|
||||||
fn input_value_parser(value: &str) -> Result<Input, clap::error::Error> {
|
fn make_input_value_parser() -> impl TypedValueParser<Value = Input> {
|
||||||
|
clap::builder::OsStringValueParser::new().try_map(|value| {
|
||||||
if value.is_empty() {
|
if value.is_empty() {
|
||||||
Err(clap::Error::new(clap::error::ErrorKind::InvalidValue))
|
Err(clap::Error::new(clap::error::ErrorKind::InvalidValue))
|
||||||
} else if value == "-" {
|
} else if value == "-" {
|
||||||
@ -287,10 +288,12 @@ fn input_value_parser(value: &str) -> Result<Input, clap::error::Error> {
|
|||||||
} else {
|
} else {
|
||||||
Ok(Input::Path(value.into()))
|
Ok(Input::Path(value.into()))
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The clap value parser used by `CompileCommand.output`
|
/// The clap value parser used by `CompileCommand.output`
|
||||||
fn output_value_parser(value: &str) -> Result<Output, clap::error::Error> {
|
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>`
|
// Empty value also handled by clap for `Option<Output>`
|
||||||
if value.is_empty() {
|
if value.is_empty() {
|
||||||
Err(clap::Error::new(clap::error::ErrorKind::InvalidValue))
|
Err(clap::Error::new(clap::error::ErrorKind::InvalidValue))
|
||||||
@ -299,6 +302,7 @@ fn output_value_parser(value: &str) -> Result<Output, clap::error::Error> {
|
|||||||
} else {
|
} else {
|
||||||
Ok(Output::Path(value.into()))
|
Ok(Output::Path(value.into()))
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parses key/value pairs split by the first equal sign.
|
/// Parses key/value pairs split by the first equal sign.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user