diff --git a/library/src/compute/calc.rs b/library/src/compute/calc.rs index b5bc0fea4..830f50338 100644 --- a/library/src/compute/calc.rs +++ b/library/src/compute/calc.rs @@ -51,6 +51,8 @@ castable! { /// The sequence of values from which to extract the minimum. /// Must not be empty. /// +/// - returns: any +/// /// ## Category /// calculate #[func] @@ -72,6 +74,8 @@ pub fn min(args: &mut Args) -> SourceResult { /// The sequence of values from which to extract the maximum. /// Must not be empty. /// +/// - returns: any +/// /// ## Category /// calculate #[func] @@ -114,6 +118,8 @@ fn minmax(args: &mut Args, goal: Ordering) -> SourceResult { /// - value: i64 (positional, required) /// The number to check for evenness. /// +/// - returns: boolean +/// /// ## Category /// calculate #[func] @@ -136,6 +142,8 @@ pub fn even(args: &mut Args) -> SourceResult { /// - value: i64 (positional, required) /// The number to check for oddness. /// +/// - returns: boolean +/// /// ## Category /// calculate #[func] @@ -159,6 +167,8 @@ pub fn odd(args: &mut Args) -> SourceResult { /// - divisor: ToMod (positional, required) /// The divisor of the modulus. /// +/// - returns: integer or float +/// /// ## Category /// calculate #[func] diff --git a/library/src/compute/construct.rs b/library/src/compute/construct.rs index bfe42cbe1..81a8b9904 100644 --- a/library/src/compute/construct.rs +++ b/library/src/compute/construct.rs @@ -23,6 +23,8 @@ use crate::prelude::*; /// - value: ToInt (positional, required) /// The value that should be converted to an integer. /// +/// - returns: integer +/// /// ## Category /// construct #[func] @@ -62,6 +64,8 @@ castable! { /// - value: ToFloat (positional, required) /// The value that should be converted to a float. /// +/// - returns: float +/// /// ## Category /// construct #[func] @@ -94,6 +98,8 @@ castable! { /// - gray: Component (positional, required) /// The gray component. /// +/// - returns: color +/// /// ## Category /// construct #[func] @@ -146,6 +152,8 @@ pub fn luma(args: &mut Args) -> SourceResult { /// - alpha: Component (positional) /// The alpha component. /// +/// - returns: color +/// /// ## Category /// construct #[func] @@ -207,6 +215,8 @@ castable! { /// - key: RatioComponent (positional, required) /// The key component. /// +/// - returns: color +/// /// ## Category /// construct #[func] @@ -249,6 +259,8 @@ castable! { /// - value: ToStr (positional, required) /// The value that should be converted to a string. /// +/// - returns: string +/// /// ## Category /// construct #[func] @@ -291,6 +303,8 @@ castable! { /// - name: EcoString (positional, required) /// The name of the label. /// +/// - returns: label +/// /// ## Category /// construct #[func] @@ -330,6 +344,8 @@ pub fn label(args: &mut Args) -> SourceResult { /// Typst (e.g. `[\\]`), you need to escape twice. Thus, to match a verbatim /// backslash, you would need to write `{regex("\\\\")}`. /// +/// - returns: regex +/// /// ## Category /// construct #[func] @@ -364,6 +380,8 @@ pub fn regex(args: &mut Args) -> SourceResult { /// - step: i64 (named) /// The distance between the generated numbers. /// +/// - returns: array +/// /// ## Category /// construct #[func] diff --git a/library/src/compute/data.rs b/library/src/compute/data.rs index 43d067530..ee2090742 100644 --- a/library/src/compute/data.rs +++ b/library/src/compute/data.rs @@ -26,11 +26,14 @@ use crate::prelude::*; /// ## Parameters /// - path: EcoString (positional, required) /// Path to a CSV file. +/// /// - delimiter: Delimiter (named) /// The delimiter that separates columns in the CSV file. /// Must be a single ASCII character. /// Defaults to a comma. /// +/// - returns: array +/// /// ## Category /// data-loading #[func] @@ -140,6 +143,8 @@ fn format_csv_error(error: csv::Error) -> String { /// - path: EcoString (positional, required) /// Path to a JSON file. /// +/// - returns: dictionary or array +/// /// ## Category /// data-loading #[func] @@ -236,6 +241,8 @@ fn format_json_error(error: serde_json::Error) -> String { /// - path: EcoString (positional, required) /// Path to an XML file. /// +/// - returns: array +/// /// ## Category /// data-loading #[func] diff --git a/library/src/compute/foundations.rs b/library/src/compute/foundations.rs index 06795887a..4e8b9d822 100644 --- a/library/src/compute/foundations.rs +++ b/library/src/compute/foundations.rs @@ -7,7 +7,7 @@ use typst::syntax::Source; /// # Type /// Determine a value's type. /// -/// Returns the name of the value's type as a string. +/// Returns the name of the value's type. /// /// ## Example /// ``` @@ -23,6 +23,8 @@ use typst::syntax::Source; /// - value: Value (positional, required) /// The value whose type's to determine. /// +/// - returns: string +/// /// ## Category /// foundations #[func] @@ -49,6 +51,8 @@ pub fn type_(args: &mut Args) -> SourceResult { /// - value: Value (positional, required) /// The value whose string representation to produce. /// +/// - returns: string +/// /// ## Category /// foundations #[func] @@ -99,6 +103,8 @@ pub fn assert(args: &mut Args) -> SourceResult { /// /// The markup and code in the string cannot interact with the file system. /// +/// - returns: content +/// /// ## Category /// foundations #[func] diff --git a/library/src/compute/utility.rs b/library/src/compute/utility.rs index be260a5fc..78cf39533 100644 --- a/library/src/compute/utility.rs +++ b/library/src/compute/utility.rs @@ -24,6 +24,8 @@ use crate::text::Case; /// - words: usize (positional, required) /// The length of the blind text in words. /// +/// - returns: string +/// /// ## Category /// utility #[func] @@ -71,6 +73,8 @@ pub fn lorem(args: &mut Args) -> SourceResult { /// If more numbers than counting symbols are given, the last counting symbol /// with its prefix is repeated. /// +/// - returns: string +/// /// ## Category /// utility #[func] @@ -139,11 +143,7 @@ impl FromStr for NumberingPattern { }; let prefix = pattern[handled..i].into(); - let case = if c.is_uppercase() { - Case::Upper - } else { - Case::Lower - }; + let case = if c.is_uppercase() { Case::Upper } else { Case::Lower }; pieces.push((prefix, kind, case)); handled = i + 1; } diff --git a/macros/src/func.rs b/macros/src/func.rs index 1f6bb626e..fe06749b0 100644 --- a/macros/src/func.rs +++ b/macros/src/func.rs @@ -17,7 +17,7 @@ pub fn func(item: syn::Item) -> Result { let mut docs = docs[first.len()..].to_string(); let example = example(&mut docs, 2); - let params = params(&mut docs)?; + let (params, returns) = params(&mut docs)?; let syntax = quote_option(section(&mut docs, "Syntax", 2)); let category = section(&mut docs, "Category", 2).expect("missing category"); let example = quote_option(example); @@ -36,6 +36,7 @@ pub fn func(item: syn::Item) -> Result { example: #example, syntax: #syntax, params: ::std::vec![#(#params),*], + returns: ::std::vec![#(#returns),*] } }; @@ -119,10 +120,14 @@ pub fn example(docs: &mut String, level: usize) -> Option { } /// Parse the parameter section. -fn params(docs: &mut String) -> Result> { - let Some(section) = section(docs, "Parameters", 2) else { return Ok(vec![]) }; +fn params(docs: &mut String) -> Result<(Vec, Vec)> { + let Some(section) = section(docs, "Parameters", 2) else { + return Ok((vec![], vec![])); + }; + let mut s = Scanner::new(§ion); let mut infos = vec![]; + let mut returns = vec![]; while s.eat_if('-') { let mut named = false; @@ -134,6 +139,18 @@ fn params(docs: &mut String) -> Result> { s.eat_whitespace(); let name = s.eat_until(':'); s.expect(": "); + + if name == "returns" { + returns = s + .eat_until('\n') + .split(" or ") + .map(str::trim) + .map(Into::into) + .collect(); + s.eat_whitespace(); + continue; + } + let ty: syn::Type = syn::parse_str(s.eat_until(char::is_whitespace))?; s.eat_whitespace(); s.expect('('); @@ -182,5 +199,5 @@ fn params(docs: &mut String) -> Result> { s.eat_whitespace(); } - Ok(infos) + Ok((infos, returns)) } diff --git a/src/model/func.rs b/src/model/func.rs index e47594a19..f04b864e3 100644 --- a/src/model/func.rs +++ b/src/model/func.rs @@ -217,6 +217,8 @@ pub struct FuncInfo { pub syntax: Option<&'static str>, /// Details about the function's parameters. pub params: Vec, + /// Valid types for the return value. + pub returns: Vec<&'static str>, } impl FuncInfo {