-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.rs
82 lines (77 loc) · 2.38 KB
/
list.rs
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
use brack_sdk_rs::{MetaData, Type, Value};
use extism_pdk::{plugin_fn, FnResult, Json, WithReturnCode};
pub(crate) fn metadata_unordered_list() -> MetaData {
MetaData {
command_name: "-list".to_string(),
call_name: "unordered_list".to_string(),
argument_types: vec![("elems".to_string(), Type::TArray(Box::new(Type::TInline)))],
return_type: Type::TBlock,
}
}
pub(crate) fn metadata_ordered_list() -> MetaData {
MetaData {
command_name: "#list".to_string(),
call_name: "ordered_list".to_string(),
argument_types: vec![("elems".to_string(), Type::TArray(Box::new(Type::TInline)))],
return_type: Type::TBlock,
}
}
fn is_list(text: &str) -> bool {
let is_unordered_list = text.starts_with("<ul>") && text.ends_with("</ul>");
let is_ordered_list = text.starts_with("<ol>") && text.ends_with("</ol>");
return is_unordered_list && is_ordered_list;
}
#[plugin_fn]
pub fn unordered_list(Json(args): Json<Vec<Value>>) -> FnResult<String> {
if args.len() != 1 {
return Err(WithReturnCode::new(
anyhow::anyhow!("Usage: {{std.-list elem1, elem2, ..., elemN}}"),
1,
));
}
let elems = match &args[0] {
Value::TextArray(t) => t,
_ => {
return Err(WithReturnCode::new(
anyhow::anyhow!("elems must be Value::TextArray"),
1,
))
}
};
let mut result = String::new();
for elem in elems {
if is_list(elem) {
result += elem;
continue;
}
result += &format!("<li>{}</li>", elem);
}
Ok(format!("<ul>{}</ul>", result))
}
#[plugin_fn]
pub fn ordered_list(Json(args): Json<Vec<Value>>) -> FnResult<String> {
if args.len() != 1 {
return Err(WithReturnCode::new(
anyhow::anyhow!("Usage: {{std.#list elem1, elem2, ..., elemN}}"),
1,
));
}
let elems = match &args[0] {
Value::TextArray(t) => t,
_ => {
return Err(WithReturnCode::new(
anyhow::anyhow!("elems must be Value::TextArray"),
1,
))
}
};
let mut result = String::new();
for elem in elems {
if is_list(elem) {
result += elem;
continue;
}
result += &format!("<li>{}</li>", elem);
}
Ok(format!("<ol>{}</ol>", result))
}