Skip to content

Commit ff3dace

Browse files
committed
add: table
1 parent f3e65f1 commit ff3dace

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ pub mod list;
99
pub mod quote;
1010
pub mod rules;
1111
pub mod strike;
12+
pub mod table;

src/table.rs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use brack_sdk_rs::{MetaData, Type, Value};
2+
use extism_pdk::{plugin_fn, FnResult, Json, WithReturnCode};
3+
4+
#[plugin_fn]
5+
pub fn metadata_table() -> FnResult<Json<MetaData>> {
6+
Ok(Json(MetaData {
7+
command_name: "table".to_string(),
8+
call_name: "table".to_string(),
9+
argument_types: vec![
10+
("col".to_string(), Type::TInline),
11+
("elems".to_string(), Type::TArray(Box::new(Type::TInline))),
12+
],
13+
return_type: Type::TBlock,
14+
}))
15+
}
16+
17+
#[plugin_fn]
18+
pub fn table(Json(args): Json<Vec<Value>>) -> FnResult<String> {
19+
if args.len() != 2 {
20+
return Err(WithReturnCode::new(
21+
anyhow::anyhow!("Usage: {{std.table col, elems}}"),
22+
1,
23+
));
24+
}
25+
let col = match &args[0] {
26+
Value::Text(t) => t.parse::<i32>(),
27+
_ => {
28+
return Err(WithReturnCode::new(
29+
anyhow::anyhow!("col must be Value::Text"),
30+
1,
31+
))
32+
}
33+
};
34+
let col = match col {
35+
Ok(c) => c,
36+
Err(err) => return Err(WithReturnCode::new(anyhow::anyhow!(err), 1)),
37+
};
38+
let elems = match &args[1] {
39+
Value::TextArray(ta) => ta,
40+
_ => {
41+
return Err(WithReturnCode::new(
42+
anyhow::anyhow!("elems must be Value::TextArray"),
43+
1,
44+
))
45+
}
46+
};
47+
let mut result = String::from("");
48+
for i in 0..(elems.len() as i32 % col) {
49+
let mut tr = String::from("");
50+
for j in 0..col {
51+
tr += &format!("<td>{}</td>", elems[(i * col + j) as usize]);
52+
}
53+
result += &format!("<tr>{}</tr>", tr);
54+
}
55+
Ok(format!("<table><tbody>{}</tbody></table>", result))
56+
}

0 commit comments

Comments
 (0)