Skip to content

Commit 8308bf7

Browse files
feat!(stdlib): add return_kind and return_rules to Function (#1628)
* Automatically add return_kind and return_rules to all stdlib functions * Add return_kind to undocumented functions * format
1 parent c1d8744 commit 8308bf7

File tree

201 files changed

+1164
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

201 files changed

+1164
-0
lines changed

src/compiler/expression/function_call.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,10 @@ mod tests {
12761276
"Test function"
12771277
}
12781278

1279+
fn return_kind(&self) -> u16 {
1280+
kind::NULL
1281+
}
1282+
12791283
fn examples(&self) -> &'static [crate::compiler::function::Example] {
12801284
&[]
12811285
}

src/compiler/function.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ pub trait Function: Send + Sync + fmt::Debug {
4040
fn internal_failure_reasons(&self) -> &'static [&'static str] {
4141
&[]
4242
}
43+
44+
/// The return type kind(s) this function can return.
45+
fn return_kind(&self) -> u16;
46+
47+
/// Human-readable rules describing the return value of the function.
48+
///
49+
/// This returns an empty slice by default, indicating no return rules
50+
/// are documented for this function.
51+
fn return_rules(&self) -> &'static [&'static str] {
52+
&[]
53+
}
54+
4355
/// One or more examples demonstrating usage of the function in VRL source
4456
/// code.
4557
fn examples(&self) -> &'static [Example];

src/stdlib/abs.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ impl Function for Abs {
2424
"Computes the absolute value of `value`."
2525
}
2626

27+
fn return_kind(&self) -> u16 {
28+
kind::INTEGER | kind::FLOAT
29+
}
30+
31+
fn return_rules(&self) -> &'static [&'static str] {
32+
&["Returns the absolute value."]
33+
}
34+
2735
fn parameters(&self) -> &'static [Parameter] {
2836
&[Parameter {
2937
keyword: "value",

src/stdlib/append.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ impl Function for Append {
1919
"Appends each item in the `items` array to the end of the `value` array."
2020
}
2121

22+
fn return_kind(&self) -> u16 {
23+
kind::ARRAY
24+
}
25+
2226
fn parameters(&self) -> &'static [Parameter] {
2327
&[
2428
Parameter {

src/stdlib/array.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ impl Function for Array {
2323
&["`value` is not an array."]
2424
}
2525

26+
fn return_kind(&self) -> u16 {
27+
kind::ARRAY
28+
}
29+
30+
fn return_rules(&self) -> &'static [&'static str] {
31+
&[
32+
"Returns the `value` if it's an array.",
33+
"Raises an error if not an array.",
34+
]
35+
}
36+
2637
fn parameters(&self) -> &'static [Parameter] {
2738
&[Parameter {
2839
keyword: "value",

src/stdlib/assert.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ impl Function for Assert {
3535
&["`condition` evaluates to `false`."]
3636
}
3737

38+
fn return_kind(&self) -> u16 {
39+
kind::BOOLEAN
40+
}
41+
3842
fn parameters(&self) -> &'static [Parameter] {
3943
&[
4044
Parameter {

src/stdlib/assert_eq.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ impl Function for AssertEq {
2929
"Asserts that two expressions, `left` and `right`, have the same value. The program is aborted with `message` if they do not have the same value."
3030
}
3131

32+
fn return_kind(&self) -> u16 {
33+
kind::BOOLEAN
34+
}
35+
3236
fn parameters(&self) -> &'static [Parameter] {
3337
&[
3438
Parameter {

src/stdlib/basename.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ impl Function for BaseName {
2626
&["`value` is not a valid string."]
2727
}
2828

29+
fn return_kind(&self) -> u16 {
30+
kind::BYTES | kind::NULL
31+
}
32+
2933
fn parameters(&self) -> &'static [Parameter] {
3034
&[Parameter {
3135
keyword: "value",

src/stdlib/boolean.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ impl Function for Boolean {
2323
&["`value` is not a Boolean."]
2424
}
2525

26+
fn return_kind(&self) -> u16 {
27+
kind::BOOLEAN
28+
}
29+
30+
fn return_rules(&self) -> &'static [&'static str] {
31+
&[
32+
"Returns `value` if it's a Boolean.",
33+
"Raises an error if not a Boolean.",
34+
]
35+
}
36+
2637
fn parameters(&self) -> &'static [Parameter] {
2738
&[Parameter {
2839
keyword: "value",

src/stdlib/casing/camelcase.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ impl Function for Camelcase {
1515
"Takes the `value` string, and turns it into camelCase. Optionally, you can pass in the existing case of the function, or else an attempt is made to determine the case automatically."
1616
}
1717

18+
fn return_kind(&self) -> u16 {
19+
kind::BYTES
20+
}
21+
1822
fn parameters(&self) -> &'static [Parameter] {
1923
&[
2024
Parameter {

0 commit comments

Comments
 (0)