diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-04-26 13:46:42 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-04-26 15:37:21 +0200 |
| commit | 3680c854a21db665d64cdb8f31aa0f9a1af16ceb (patch) | |
| tree | 39dfa33059293251f1e2890f9b3d0e3dc178ed03 /library/src/compute | |
| parent | 59957746e91c1322a8ca6d228bcaa0f31941ee1b (diff) | |
Touch up docs
Diffstat (limited to 'library/src/compute')
| -rw-r--r-- | library/src/compute/calc.rs | 22 | ||||
| -rw-r--r-- | library/src/compute/data.rs | 47 | ||||
| -rw-r--r-- | library/src/compute/foundations.rs | 3 |
3 files changed, 44 insertions, 28 deletions
diff --git a/library/src/compute/calc.rs b/library/src/compute/calc.rs index b6b2442f..708d795d 100644 --- a/library/src/compute/calc.rs +++ b/library/src/compute/calc.rs @@ -41,6 +41,7 @@ pub fn module() -> Module { scope.define("even", even); scope.define("odd", odd); scope.define("rem", rem); + scope.define("mod", mod_); scope.define("quo", quo); scope.define("inf", Value::Float(f64::INFINITY)); scope.define("nan", Value::Float(f64::NAN)); @@ -912,6 +913,27 @@ pub fn rem( dividend.apply2(divisor.v, Rem::rem, Rem::rem).value() } +/// Calculate the modulus of two numbers. (Deprecated) +/// +/// **This function is deprecated in favor of `rem`. It will be removed in +/// a future update.** +/// +/// Display: Modulus +/// Category: calculate +/// Returns: integer or float +#[func] +pub fn mod_( + /// The dividend of the remainder. + dividend: Num, + /// The divisor of the remainder. + divisor: Spanned<Num>, +) -> Value { + if divisor.v.float() == 0.0 { + bail!(divisor.span, "divisor must not be zero"); + } + dividend.apply2(divisor.v, Rem::rem, Rem::rem).value() +} + /// Calculate the quotient of two numbers. /// /// ## Example diff --git a/library/src/compute/data.rs b/library/src/compute/data.rs index 02586ddc..14ae304b 100644 --- a/library/src/compute/data.rs +++ b/library/src/compute/data.rs @@ -201,7 +201,6 @@ fn convert_json(value: serde_json::Value) -> Value { } /// Format the user-facing JSON error message. -#[track_caller] fn format_json_error(error: serde_json::Error) -> EcoString { assert!(error.is_syntax() || error.is_eof()); eco_format!("failed to parse json file: syntax error in line {}", error.line()) @@ -209,30 +208,24 @@ fn format_json_error(error: serde_json::Error) -> EcoString { /// Read structured data from a TOML file. /// -/// The file must contain a valid TOML table. Tables will be +/// The file must contain a valid TOML table. TOML tables will be /// converted into Typst dictionaries, and TOML arrays will be converted into /// Typst arrays. Strings and booleans will be converted into the Typst -/// equivalents, numbers will be converted to floats or integers depending on -/// whether they are whole numbers. TOML DateTim will be converted to strings. +/// equivalents and numbers will be converted to floats or integers depending on +/// whether they are whole numbers. For the time being, datetimes will be +/// converted to strings as Typst does not have a built-in datetime yet. /// -/// The function returns a dictionary. -/// -/// The JSON files in the example contain objects with the keys `temperature`, -/// `unit`, and `weather`. +/// The TOML file in the example consists of a table with the keys `title`, +/// `version`, and `authors`. /// /// ## Example /// ```example -/// #let informations(content) = { -/// [This work is made by #content.authors.join(", ", last: " and "). We are currently at version #content.version. -/// The favorites series of the audience are ] -/// for serie in content.series [ -/// - #serie.name with #serie.fans fans. -/// ] -/// [We need to submit our work in #content.informations.location, we currently have #content.informations.pages pages.] -/// } -/// +/// #let details = toml("details.toml") /// -/// #informations(toml("informations.toml")) +/// Title: #details.title \ +/// Version: #details.version \ +/// Authors: #(details.authors +/// .join(", ", last: " and ")) /// ``` /// /// Display: TOML @@ -268,23 +261,22 @@ fn convert_toml(value: toml::Value) -> Value { .map(|(key, value)| (key.into(), convert_toml(value))) .collect(), ), - // Todo: make it use native date/time object(s) once it is implemented. + // TODO: Make it use native date/time object(s) once it is implemented. toml::Value::Datetime(v) => Value::Str(v.to_string().into()), } } /// Format the user-facing TOML error message. -#[track_caller] -fn format_toml_error(error: toml::de::Error) -> String { +fn format_toml_error(error: toml::de::Error) -> EcoString { if let Some(range) = error.span() { - format!( - "failed to parse toml file: {message}, index {start}-{end}", - message = error.message(), - start = range.start, - end = range.end + eco_format!( + "failed to parse toml file: {}, index {}-{}", + error.message(), + range.start, + range.end ) } else { - format!("failed to parse toml file: {message}", message = error.message()) + eco_format!("failed to parse toml file: {}", error.message()) } } @@ -373,7 +365,6 @@ fn convert_yaml_key(key: serde_yaml::Value) -> Option<Str> { } /// Format the user-facing YAML error message. -#[track_caller] fn format_yaml_error(error: serde_yaml::Error) -> EcoString { eco_format!("failed to parse yaml file: {}", error.to_string().trim()) } diff --git a/library/src/compute/foundations.rs b/library/src/compute/foundations.rs index d5397e60..bad9f8ab 100644 --- a/library/src/compute/foundations.rs +++ b/library/src/compute/foundations.rs @@ -31,6 +31,9 @@ pub fn type_( /// in monospace with syntax-highlighting. The exceptions are `{none}`, /// integers, floats, strings, content, and functions. /// +/// **Note:** This function is for debugging purposes. Its output should not be +/// considered stable and may change at any time! +/// /// ## Example /// ```example /// #none vs #repr(none) \ |
