Annotate return values

This commit is contained in:
Laurenz 2022-12-22 00:34:03 +01:00
parent 4c92ab4ace
commit 990224c0f8
7 changed files with 70 additions and 10 deletions

View File

@ -51,6 +51,8 @@ castable! {
/// The sequence of values from which to extract the minimum. /// The sequence of values from which to extract the minimum.
/// Must not be empty. /// Must not be empty.
/// ///
/// - returns: any
///
/// ## Category /// ## Category
/// calculate /// calculate
#[func] #[func]
@ -72,6 +74,8 @@ pub fn min(args: &mut Args) -> SourceResult<Value> {
/// The sequence of values from which to extract the maximum. /// The sequence of values from which to extract the maximum.
/// Must not be empty. /// Must not be empty.
/// ///
/// - returns: any
///
/// ## Category /// ## Category
/// calculate /// calculate
#[func] #[func]
@ -114,6 +118,8 @@ fn minmax(args: &mut Args, goal: Ordering) -> SourceResult<Value> {
/// - value: i64 (positional, required) /// - value: i64 (positional, required)
/// The number to check for evenness. /// The number to check for evenness.
/// ///
/// - returns: boolean
///
/// ## Category /// ## Category
/// calculate /// calculate
#[func] #[func]
@ -136,6 +142,8 @@ pub fn even(args: &mut Args) -> SourceResult<Value> {
/// - value: i64 (positional, required) /// - value: i64 (positional, required)
/// The number to check for oddness. /// The number to check for oddness.
/// ///
/// - returns: boolean
///
/// ## Category /// ## Category
/// calculate /// calculate
#[func] #[func]
@ -159,6 +167,8 @@ pub fn odd(args: &mut Args) -> SourceResult<Value> {
/// - divisor: ToMod (positional, required) /// - divisor: ToMod (positional, required)
/// The divisor of the modulus. /// The divisor of the modulus.
/// ///
/// - returns: integer or float
///
/// ## Category /// ## Category
/// calculate /// calculate
#[func] #[func]

View File

@ -23,6 +23,8 @@ use crate::prelude::*;
/// - value: ToInt (positional, required) /// - value: ToInt (positional, required)
/// The value that should be converted to an integer. /// The value that should be converted to an integer.
/// ///
/// - returns: integer
///
/// ## Category /// ## Category
/// construct /// construct
#[func] #[func]
@ -62,6 +64,8 @@ castable! {
/// - value: ToFloat (positional, required) /// - value: ToFloat (positional, required)
/// The value that should be converted to a float. /// The value that should be converted to a float.
/// ///
/// - returns: float
///
/// ## Category /// ## Category
/// construct /// construct
#[func] #[func]
@ -94,6 +98,8 @@ castable! {
/// - gray: Component (positional, required) /// - gray: Component (positional, required)
/// The gray component. /// The gray component.
/// ///
/// - returns: color
///
/// ## Category /// ## Category
/// construct /// construct
#[func] #[func]
@ -146,6 +152,8 @@ pub fn luma(args: &mut Args) -> SourceResult<Value> {
/// - alpha: Component (positional) /// - alpha: Component (positional)
/// The alpha component. /// The alpha component.
/// ///
/// - returns: color
///
/// ## Category /// ## Category
/// construct /// construct
#[func] #[func]
@ -207,6 +215,8 @@ castable! {
/// - key: RatioComponent (positional, required) /// - key: RatioComponent (positional, required)
/// The key component. /// The key component.
/// ///
/// - returns: color
///
/// ## Category /// ## Category
/// construct /// construct
#[func] #[func]
@ -249,6 +259,8 @@ castable! {
/// - value: ToStr (positional, required) /// - value: ToStr (positional, required)
/// The value that should be converted to a string. /// The value that should be converted to a string.
/// ///
/// - returns: string
///
/// ## Category /// ## Category
/// construct /// construct
#[func] #[func]
@ -291,6 +303,8 @@ castable! {
/// - name: EcoString (positional, required) /// - name: EcoString (positional, required)
/// The name of the label. /// The name of the label.
/// ///
/// - returns: label
///
/// ## Category /// ## Category
/// construct /// construct
#[func] #[func]
@ -330,6 +344,8 @@ pub fn label(args: &mut Args) -> SourceResult<Value> {
/// Typst (e.g. `[\\]`), you need to escape twice. Thus, to match a verbatim /// Typst (e.g. `[\\]`), you need to escape twice. Thus, to match a verbatim
/// backslash, you would need to write `{regex("\\\\")}`. /// backslash, you would need to write `{regex("\\\\")}`.
/// ///
/// - returns: regex
///
/// ## Category /// ## Category
/// construct /// construct
#[func] #[func]
@ -364,6 +380,8 @@ pub fn regex(args: &mut Args) -> SourceResult<Value> {
/// - step: i64 (named) /// - step: i64 (named)
/// The distance between the generated numbers. /// The distance between the generated numbers.
/// ///
/// - returns: array
///
/// ## Category /// ## Category
/// construct /// construct
#[func] #[func]

View File

@ -26,11 +26,14 @@ use crate::prelude::*;
/// ## Parameters /// ## Parameters
/// - path: EcoString (positional, required) /// - path: EcoString (positional, required)
/// Path to a CSV file. /// Path to a CSV file.
///
/// - delimiter: Delimiter (named) /// - delimiter: Delimiter (named)
/// The delimiter that separates columns in the CSV file. /// The delimiter that separates columns in the CSV file.
/// Must be a single ASCII character. /// Must be a single ASCII character.
/// Defaults to a comma. /// Defaults to a comma.
/// ///
/// - returns: array
///
/// ## Category /// ## Category
/// data-loading /// data-loading
#[func] #[func]
@ -140,6 +143,8 @@ fn format_csv_error(error: csv::Error) -> String {
/// - path: EcoString (positional, required) /// - path: EcoString (positional, required)
/// Path to a JSON file. /// Path to a JSON file.
/// ///
/// - returns: dictionary or array
///
/// ## Category /// ## Category
/// data-loading /// data-loading
#[func] #[func]
@ -236,6 +241,8 @@ fn format_json_error(error: serde_json::Error) -> String {
/// - path: EcoString (positional, required) /// - path: EcoString (positional, required)
/// Path to an XML file. /// Path to an XML file.
/// ///
/// - returns: array
///
/// ## Category /// ## Category
/// data-loading /// data-loading
#[func] #[func]

View File

@ -7,7 +7,7 @@ use typst::syntax::Source;
/// # Type /// # Type
/// Determine a value's 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 /// ## Example
/// ``` /// ```
@ -23,6 +23,8 @@ use typst::syntax::Source;
/// - value: Value (positional, required) /// - value: Value (positional, required)
/// The value whose type's to determine. /// The value whose type's to determine.
/// ///
/// - returns: string
///
/// ## Category /// ## Category
/// foundations /// foundations
#[func] #[func]
@ -49,6 +51,8 @@ pub fn type_(args: &mut Args) -> SourceResult<Value> {
/// - value: Value (positional, required) /// - value: Value (positional, required)
/// The value whose string representation to produce. /// The value whose string representation to produce.
/// ///
/// - returns: string
///
/// ## Category /// ## Category
/// foundations /// foundations
#[func] #[func]
@ -99,6 +103,8 @@ pub fn assert(args: &mut Args) -> SourceResult<Value> {
/// ///
/// The markup and code in the string cannot interact with the file system. /// The markup and code in the string cannot interact with the file system.
/// ///
/// - returns: content
///
/// ## Category /// ## Category
/// foundations /// foundations
#[func] #[func]

View File

@ -24,6 +24,8 @@ use crate::text::Case;
/// - words: usize (positional, required) /// - words: usize (positional, required)
/// The length of the blind text in words. /// The length of the blind text in words.
/// ///
/// - returns: string
///
/// ## Category /// ## Category
/// utility /// utility
#[func] #[func]
@ -71,6 +73,8 @@ pub fn lorem(args: &mut Args) -> SourceResult<Value> {
/// If more numbers than counting symbols are given, the last counting symbol /// If more numbers than counting symbols are given, the last counting symbol
/// with its prefix is repeated. /// with its prefix is repeated.
/// ///
/// - returns: string
///
/// ## Category /// ## Category
/// utility /// utility
#[func] #[func]
@ -139,11 +143,7 @@ impl FromStr for NumberingPattern {
}; };
let prefix = pattern[handled..i].into(); let prefix = pattern[handled..i].into();
let case = if c.is_uppercase() { let case = if c.is_uppercase() { Case::Upper } else { Case::Lower };
Case::Upper
} else {
Case::Lower
};
pieces.push((prefix, kind, case)); pieces.push((prefix, kind, case));
handled = i + 1; handled = i + 1;
} }

View File

@ -17,7 +17,7 @@ pub fn func(item: syn::Item) -> Result<TokenStream> {
let mut docs = docs[first.len()..].to_string(); let mut docs = docs[first.len()..].to_string();
let example = example(&mut docs, 2); 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 syntax = quote_option(section(&mut docs, "Syntax", 2));
let category = section(&mut docs, "Category", 2).expect("missing category"); let category = section(&mut docs, "Category", 2).expect("missing category");
let example = quote_option(example); let example = quote_option(example);
@ -36,6 +36,7 @@ pub fn func(item: syn::Item) -> Result<TokenStream> {
example: #example, example: #example,
syntax: #syntax, syntax: #syntax,
params: ::std::vec![#(#params),*], params: ::std::vec![#(#params),*],
returns: ::std::vec![#(#returns),*]
} }
}; };
@ -119,10 +120,14 @@ pub fn example(docs: &mut String, level: usize) -> Option<String> {
} }
/// Parse the parameter section. /// Parse the parameter section.
fn params(docs: &mut String) -> Result<Vec<TokenStream>> { fn params(docs: &mut String) -> Result<(Vec<TokenStream>, Vec<String>)> {
let Some(section) = section(docs, "Parameters", 2) else { return Ok(vec![]) }; let Some(section) = section(docs, "Parameters", 2) else {
return Ok((vec![], vec![]));
};
let mut s = Scanner::new(&section); let mut s = Scanner::new(&section);
let mut infos = vec![]; let mut infos = vec![];
let mut returns = vec![];
while s.eat_if('-') { while s.eat_if('-') {
let mut named = false; let mut named = false;
@ -134,6 +139,18 @@ fn params(docs: &mut String) -> Result<Vec<TokenStream>> {
s.eat_whitespace(); s.eat_whitespace();
let name = s.eat_until(':'); let name = s.eat_until(':');
s.expect(": "); 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))?; let ty: syn::Type = syn::parse_str(s.eat_until(char::is_whitespace))?;
s.eat_whitespace(); s.eat_whitespace();
s.expect('('); s.expect('(');
@ -182,5 +199,5 @@ fn params(docs: &mut String) -> Result<Vec<TokenStream>> {
s.eat_whitespace(); s.eat_whitespace();
} }
Ok(infos) Ok((infos, returns))
} }

View File

@ -217,6 +217,8 @@ pub struct FuncInfo {
pub syntax: Option<&'static str>, pub syntax: Option<&'static str>,
/// Details about the function's parameters. /// Details about the function's parameters.
pub params: Vec<ParamInfo>, pub params: Vec<ParamInfo>,
/// Valid types for the return value.
pub returns: Vec<&'static str>,
} }
impl FuncInfo { impl FuncInfo {