Fix data loading display names

This commit is contained in:
Laurenz 2023-08-30 17:15:49 +02:00
parent d3ca2ff4ec
commit 5b36b46230
3 changed files with 30 additions and 50 deletions

View File

@ -6,3 +6,4 @@ mapping: { '1': "one", '2': "two"}
seq: [1, 2, 3, 4] seq: [1, 2, 3, 4]
bool: false bool: false
true: bool true: bool
1: ok

View File

@ -137,7 +137,7 @@ pub fn csv(
/// Reads structured data from a CSV string/bytes. /// Reads structured data from a CSV string/bytes.
/// ///
/// Display: CSV /// Display: Decode CSV
/// Category: data-loading /// Category: data-loading
#[func] #[func]
pub fn csv_decode( pub fn csv_decode(
@ -277,15 +277,14 @@ pub fn json_decode(
data: Spanned<Readable>, data: Spanned<Readable>,
) -> SourceResult<Value> { ) -> SourceResult<Value> {
let Spanned { v: data, span } = data; let Spanned { v: data, span } = data;
let value: Value = serde_json::from_slice(data.as_slice()) serde_json::from_slice(data.as_slice())
.map_err(format_json_error) .map_err(format_json_error)
.at(span)?; .at(span)
Ok(value)
} }
/// Encode structured data into a JSON string. /// Encodes structured data into a JSON string.
/// ///
/// Display: JSON /// Display: Encode JSON
/// Category: data-loading /// Category: data-loading
#[func] #[func]
pub fn json_encode( pub fn json_encode(
@ -297,7 +296,6 @@ pub fn json_encode(
pretty: bool, pretty: bool,
) -> SourceResult<Str> { ) -> SourceResult<Str> {
let Spanned { v: value, span } = value; let Spanned { v: value, span } = value;
if pretty { if pretty {
serde_json::to_string_pretty(&value) serde_json::to_string_pretty(&value)
} else { } else {
@ -316,11 +314,11 @@ fn format_json_error(error: serde_json::Error) -> EcoString {
/// Reads structured data from a TOML file. /// Reads structured data from a TOML file.
/// ///
/// The file must contain a valid TOML table. TOML tables will be /// The file must contain a valid TOML table. TOML tables will be converted into
/// converted into Typst dictionaries, and TOML arrays will be converted into /// Typst dictionaries, and TOML arrays will be converted into Typst arrays.
/// Typst arrays. Strings, booleans and datetimes will be converted into the Typst /// Strings, booleans and datetimes will be converted into the Typst equivalents
/// equivalents and numbers will be converted to floats or integers depending on /// and numbers will be converted to floats or integers depending on whether
/// whether they are whole numbers. /// they are whole numbers.
/// ///
/// The TOML file in the example consists of a table with the keys `title`, /// The TOML file in the example consists of a table with the keys `title`,
/// `version`, and `authors`. /// `version`, and `authors`.
@ -352,13 +350,12 @@ pub fn toml(
let Spanned { v: path, span } = path; let Spanned { v: path, span } = path;
let id = vm.resolve_path(&path).at(span)?; let id = vm.resolve_path(&path).at(span)?;
let data = vm.world().file(id).at(span)?; let data = vm.world().file(id).at(span)?;
toml_decode(Spanned::new(Readable::Bytes(data), span)) toml_decode(Spanned::new(Readable::Bytes(data), span))
} }
/// Reads structured data from a TOML string/bytes. /// Reads structured data from a TOML string/bytes.
/// ///
/// Display: TOML /// Display: Decode TOML
/// Category: data-loading /// Category: data-loading
#[func] #[func]
pub fn toml_decode( pub fn toml_decode(
@ -369,26 +366,23 @@ pub fn toml_decode(
let raw = std::str::from_utf8(data.as_slice()) let raw = std::str::from_utf8(data.as_slice())
.map_err(|_| "file is not valid utf-8") .map_err(|_| "file is not valid utf-8")
.at(span)?; .at(span)?;
toml::from_str(raw).map_err(format_toml_error).at(span)
let value: Value = toml::from_str(raw).map_err(format_toml_error).at(span)?;
Ok(value)
} }
/// Encode structured data into a TOML string. /// Encodes structured data into a TOML string.
/// ///
/// Display: TOML /// Display: Encode TOML
/// Category: data-loading /// Category: data-loading
#[func] #[func]
pub fn toml_encode( pub fn toml_encode(
/// Value to be encoded. /// Value to be encoded.
value: Spanned<Value>, value: Spanned<Value>,
/// Apply a default pretty policy to the document. /// Whether to pretty-print the resulting TOML.
#[named] #[named]
#[default(true)] #[default(true)]
pretty: bool, pretty: bool,
) -> SourceResult<Str> { ) -> SourceResult<Str> {
let Spanned { v: value, span } = value; let Spanned { v: value, span } = value;
if pretty { toml::to_string_pretty(&value) } else { toml::to_string(&value) } if pretty { toml::to_string_pretty(&value) } else { toml::to_string(&value) }
.map(|v| v.into()) .map(|v| v.into())
.map_err(|e| eco_format!("failed to encode value as toml: {e}")) .map_err(|e| eco_format!("failed to encode value as toml: {e}"))
@ -416,16 +410,8 @@ fn format_toml_error(error: toml::de::Error) -> EcoString {
/// Typst arrays. Strings and booleans will be converted into the Typst /// Typst arrays. Strings and booleans will be converted into the Typst
/// equivalents, null-values (`null`, `~` or empty ``) will be converted into /// equivalents, null-values (`null`, `~` or empty ``) will be converted into
/// `{none}`, and numbers will be converted to floats or integers depending on /// `{none}`, and numbers will be converted to floats or integers depending on
/// whether they are whole numbers. /// whether they are whole numbers. Custom YAML tags are ignored, though the
/// /// loaded value will still be present.
/// Note that mapping keys that are not a string cause the entry to be
/// discarded.
///
/// Custom YAML tags are ignored, though the loaded value will still be
/// present.
///
/// The function returns a dictionary or value or an array, depending on
/// the YAML file.
/// ///
/// The YAML files in the example contain objects with authors as keys, /// The YAML files in the example contain objects with authors as keys,
/// each with a sequence of their own submapping with the keys /// each with a sequence of their own submapping with the keys
@ -469,7 +455,7 @@ pub fn yaml(
/// Reads structured data from a YAML string/bytes. /// Reads structured data from a YAML string/bytes.
/// ///
/// Display: YAML /// Display: Decode YAML
/// Category: data-loading /// Category: data-loading
#[func] #[func]
pub fn yaml_decode( pub fn yaml_decode(
@ -477,15 +463,14 @@ pub fn yaml_decode(
data: Spanned<Readable>, data: Spanned<Readable>,
) -> SourceResult<Value> { ) -> SourceResult<Value> {
let Spanned { v: data, span } = data; let Spanned { v: data, span } = data;
let value: Value = serde_yaml::from_slice(data.as_slice()) serde_yaml::from_slice(data.as_slice())
.map_err(format_yaml_error) .map_err(format_yaml_error)
.at(span)?; .at(span)
Ok(value)
} }
/// Encode structured data into a yaml string. /// Encode structured data into a YAML string.
/// ///
/// Display: YAML /// Display: Encode YAML
/// Category: data-loading /// Category: data-loading
#[func] #[func]
pub fn yaml_encode( pub fn yaml_encode(
@ -493,7 +478,6 @@ pub fn yaml_encode(
value: Spanned<Value>, value: Spanned<Value>,
) -> SourceResult<Str> { ) -> SourceResult<Str> {
let Spanned { v: value, span } = value; let Spanned { v: value, span } = value;
serde_yaml::to_string(&value) serde_yaml::to_string(&value)
.map(|v| v.into()) .map(|v| v.into())
.map_err(|e| eco_format!("failed to encode value as yaml: {e}")) .map_err(|e| eco_format!("failed to encode value as yaml: {e}"))
@ -514,9 +498,6 @@ fn format_yaml_error(error: serde_yaml::Error) -> EcoString {
/// `{none}`, and numbers will be converted to floats or integers depending on /// `{none}`, and numbers will be converted to floats or integers depending on
/// whether they are whole numbers. /// whether they are whole numbers.
/// ///
/// The function returns a dictionary or value or an array, depending on
/// the input.
///
/// Display: CBOR /// Display: CBOR
/// Category: data-loading /// Category: data-loading
#[func] #[func]
@ -539,7 +520,7 @@ pub fn cbor(
/// Reads structured data from CBOR bytes. /// Reads structured data from CBOR bytes.
/// ///
/// Display: CBOR /// Display: Decode CBOR
/// Category: data-loading /// Category: data-loading
#[func] #[func]
pub fn cbor_decode( pub fn cbor_decode(
@ -547,15 +528,14 @@ pub fn cbor_decode(
data: Spanned<Bytes>, data: Spanned<Bytes>,
) -> SourceResult<Value> { ) -> SourceResult<Value> {
let Spanned { v: data, span } = data; let Spanned { v: data, span } = data;
let value: Value = ciborium::from_reader(data.as_slice()) ciborium::from_reader(data.as_slice())
.map_err(|e| eco_format!("failed to parse cbor: {e}")) .map_err(|e| eco_format!("failed to parse cbor: {e}"))
.at(span)?; .at(span)
Ok(value)
} }
/// Encode structured data into CBOR bytes. /// Encode structured data into CBOR bytes.
/// ///
/// Display: CBOR /// Display: Encode CBOR
/// Category: data-loading /// Category: data-loading
#[func] #[func]
pub fn cbor_encode( pub fn cbor_encode(
@ -563,7 +543,6 @@ pub fn cbor_encode(
value: Spanned<Value>, value: Spanned<Value>,
) -> SourceResult<Bytes> { ) -> SourceResult<Bytes> {
let Spanned { v: value, span } = value; let Spanned { v: value, span } = value;
let mut res = Vec::new(); let mut res = Vec::new();
ciborium::into_writer(&value, &mut res) ciborium::into_writer(&value, &mut res)
.map(|_| res.into()) .map(|_| res.into())
@ -641,7 +620,7 @@ pub fn xml(
/// Reads structured data from an XML string/bytes. /// Reads structured data from an XML string/bytes.
/// ///
/// Display: XML /// Display: Decode XML
/// Category: data-loading /// Category: data-loading
#[func] #[func]
pub fn xml_decode( pub fn xml_decode(

View File

@ -86,7 +86,7 @@
--- ---
// Test reading YAML data // Test reading YAML data
#let data = yaml("/files/yaml-types.yaml") #let data = yaml("/files/yaml-types.yaml")
#test(data.len(), 8) #test(data.len(), 9)
#test(data.null_key, (none, none)) #test(data.null_key, (none, none))
#test(data.string, "text") #test(data.string, "text")
#test(data.integer, 5) #test(data.integer, 5)
@ -95,7 +95,7 @@
#test(data.seq, (1,2,3,4)) #test(data.seq, (1,2,3,4))
#test(data.bool, false) #test(data.bool, false)
#test(data.keys().contains("true"), true) #test(data.keys().contains("true"), true)
--- #test(data.at("1"), "ok")
--- ---
// Error: 7-24 failed to parse yaml file: while parsing a flow sequence, expected ',' or ']' at line 2 column 1 // Error: 7-24 failed to parse yaml file: while parsing a flow sequence, expected ',' or ']' at line 2 column 1