1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use nadi_plugin::nadi_internal_plugin;
#[nadi_internal_plugin]
mod debug {
use crate::functions::{FunctionArgs, FunctionKwArgs};
use abi_stable::std_types::Tuple2;
use colored::Colorize;
use nadi_plugin::env_func;
/// sleep for given number of milliseconds
#[env_func(time = 1000u64)]
fn sleep(time: u64) {
std::thread::sleep(std::time::Duration::from_millis(time))
}
/// Print the args and kwargs on this function
///
/// This function will just print out the args and kwargs the
/// function is called with. This is for debugging purposes to see
/// if the args/kwargs are identified properly. And can also be
/// used to see how the nadi system takes the input from the
/// function call.
#[env_func]
fn debug(
/// Function arguments
#[args]
args: FunctionArgs,
/// Function Keyword arguments
#[kwargs]
kwargs: FunctionKwArgs,
) {
let mut args_str: Vec<String> = args.iter().map(|a| format!("{a:?}")).collect();
let kwargs_str: Vec<String> = kwargs
.iter()
.map(|Tuple2(k, v)| format!("{}={:?}", k.to_string().blue(), v))
.collect();
args_str.extend(kwargs_str);
println!("Function Call: debug({})", args_str.join(", "));
println!("Args: {args:?}");
println!("KwArgs: {kwargs:?}");
}
/// Echo the string to stdout or stderr
///
/// This simply echoes anything given to it. This can be used in
/// combination with nadi tasks that create files (image, text,
/// etc). The `echo` function can be called to get the link to
/// those files back to the stdout.
///
/// Also useful for nadi preprocessor.
#[env_func(stderr = false, newline = true)]
fn echo(
/// line to print
line: String,
/// print to stderr instead of stdout
stderr: bool,
/// print newline at the end
newline: bool,
) {
match (stderr, newline) {
(false, false) => print!("{line}"),
(false, true) => println!("{line}"),
(true, false) => eprint!("{line}"),
(true, true) => eprintln!("{line}"),
}
}
/// Echo the `----8<----` line for clipping syntax
///
/// This function is a utility function for the generation of nadi
/// book. This prints out the `----8<----` line when called, so
/// that `mdbook` preprocessor for `nadi` knows where to clip the
/// output for displaying it in the book.
///
/// This makes it easier to only show the relevant parts of the
/// output in the documentation instead of having the user see
/// output of other unrelated parts which are necessary for
/// generating the results.
///
/// # Example
/// Given the following tasks file:
/// ```task,ignore
/// net load_file("...")
/// net load_attrs("...")
/// net clip()
/// net render("{_NAME} {attr1}")
/// ```
///
/// The clip function's output will let the preprocessor know that
/// only the parts after that are relevant to the user. Hence,
/// it'll discard outputs before that during documentation
/// generation.
#[env_func(stderr = false)]
fn clip(
/// print in stderr instead of in stdout
stderr: bool,
) {
if stderr {
eprintln!("----8<----");
} else {
println!("----8<----");
}
}
}