-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathapi_ref.rs
More file actions
103 lines (95 loc) · 3.67 KB
/
api_ref.rs
File metadata and controls
103 lines (95 loc) · 3.67 KB
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
use std::collections::HashMap;
use std::sync::LazyLock;
use clarity_repl::clarity::docs::{
make_api_reference, make_define_reference, make_keyword_reference, FunctionAPI,
};
use clarity_repl::clarity::functions::define::DefineFunctions;
use clarity_repl::clarity::functions::NativeFunctions;
use clarity_repl::clarity::variables::NativeVariables;
use clarity_repl::clarity::ClarityVersion;
fn code(code: &str) -> String {
["```clarity", code.trim(), "```"].join("\n")
}
fn format_removed_in(max_version: Option<ClarityVersion>) -> String {
max_version
.map(|max_version| {
format!(
"Removed in **{}**",
match max_version {
ClarityVersion::Clarity1 => ClarityVersion::Clarity2,
ClarityVersion::Clarity2 => ClarityVersion::Clarity3,
ClarityVersion::Clarity3 => ClarityVersion::Clarity4,
ClarityVersion::Clarity4 => ClarityVersion::Clarity5,
ClarityVersion::Clarity5 => unreachable!(),
}
)
})
.unwrap_or_default()
}
pub static API_REF: LazyLock<HashMap<String, (String, Option<FunctionAPI>)>> =
LazyLock::new(|| {
let mut api_references: HashMap<String, (String, Option<FunctionAPI>)> = HashMap::new();
let separator = "- - -";
for define_function in DefineFunctions::ALL {
let reference = make_define_reference(define_function);
api_references.insert(
define_function.to_string(),
(
Vec::from([
&code(&reference.signature),
separator,
&reference.description,
separator,
"**Example**",
&code(&reference.example),
])
.join("\n"),
Some(reference),
),
);
}
for native_function in NativeFunctions::ALL {
let reference = make_api_reference(native_function);
api_references.insert(
native_function.to_string(),
(
Vec::from([
&code(
format!("{} -> {}", &reference.signature, &reference.output_type)
.as_str(),
),
separator,
&reference.description,
separator,
&format!("Introduced in **{}** ", &reference.min_version),
&format_removed_in(reference.max_version),
separator,
"**Example**",
&code(&reference.example),
])
.join("\n"),
Some(reference),
),
);
}
for native_keyword in NativeVariables::ALL {
let reference = make_keyword_reference(native_keyword).unwrap();
api_references.insert(
native_keyword.to_string(),
(
Vec::from([
reference.description,
separator,
&format!("Introduced in **{}** ", &reference.min_version),
&format_removed_in(reference.max_version),
separator,
"**Example**",
&code(reference.example),
])
.join("\n"),
None,
),
);
}
api_references
});