mirror of
https://github.com/typst/typst
synced 2025-05-20 03:55:29 +08:00
Add ability to choose between minified and pretty-printed JSON (#4161)
This commit is contained in:
parent
b73b3ca335
commit
6d07f702e1
@ -153,6 +153,10 @@ pub struct QueryCommand {
|
||||
/// The format to serialize in
|
||||
#[clap(long = "format", default_value = "json")]
|
||||
pub format: SerializationFormat,
|
||||
|
||||
/// Whether to pretty-print the serialized output
|
||||
#[clap(long)]
|
||||
pub pretty: bool,
|
||||
}
|
||||
|
||||
// Output file format for query command
|
||||
|
@ -99,20 +99,28 @@ fn format(elements: Vec<Content>, command: &QueryCommand) -> StrResult<String> {
|
||||
let Some(value) = mapped.first() else {
|
||||
bail!("no such field found for element");
|
||||
};
|
||||
serialize(value, command.format)
|
||||
serialize(value, command.format, command.pretty)
|
||||
} else {
|
||||
serialize(&mapped, command.format)
|
||||
serialize(&mapped, command.format, command.pretty)
|
||||
}
|
||||
}
|
||||
|
||||
/// Serialize data to the output format.
|
||||
fn serialize(data: &impl Serialize, format: SerializationFormat) -> StrResult<String> {
|
||||
fn serialize(
|
||||
data: &impl Serialize,
|
||||
format: SerializationFormat,
|
||||
pretty: bool,
|
||||
) -> StrResult<String> {
|
||||
match format {
|
||||
SerializationFormat::Json => {
|
||||
serde_json::to_string_pretty(data).map_err(|e| eco_format!("{e}"))
|
||||
if pretty {
|
||||
serde_json::to_string_pretty(data).map_err(|e| eco_format!("{e}"))
|
||||
} else {
|
||||
serde_json::to_string(data).map_err(|e| eco_format!("{e}"))
|
||||
}
|
||||
}
|
||||
SerializationFormat::Yaml => {
|
||||
serde_yaml::to_string(&data).map_err(|e| eco_format!("{e}"))
|
||||
serde_yaml::to_string(data).map_err(|e| eco_format!("{e}"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user