@@ -4,17 +4,36 @@ use extism_pdk::{plugin_fn, FnResult, Json, WithReturnCode};
4
4
#[ plugin_fn]
5
5
pub fn metadata_unordered_list ( ) -> FnResult < Json < MetaData > > {
6
6
Ok ( Json ( MetaData {
7
- command_name : "list" . to_string ( ) ,
7
+ command_name : "- list" . to_string ( ) ,
8
8
call_name : "unordered_list" . to_string ( ) ,
9
9
argument_types : vec ! [ ( "elems" . to_string( ) , Type :: TArray ( Box :: new( Type :: TInline ) ) ) ] ,
10
10
return_type : Type :: TBlock ,
11
11
} ) )
12
12
}
13
13
14
+ #[ plugin_fn]
15
+ pub fn metadata_ordered_list ( ) -> FnResult < Json < MetaData > > {
16
+ Ok ( Json ( MetaData {
17
+ command_name : "#list" . to_string ( ) ,
18
+ call_name : "ordered_list" . to_string ( ) ,
19
+ argument_types : vec ! [ ( "elems" . to_string( ) , Type :: TArray ( Box :: new( Type :: TInline ) ) ) ] ,
20
+ return_type : Type :: TBlock ,
21
+ } ) )
22
+ }
23
+
24
+ fn is_list ( text : & str ) -> bool {
25
+ bool is_unordered_list = text. starts_with ( "<ul>" ) && text. ends_with ( "</ul>" ) ;
26
+ bool is_ordered_list = text. starts_with ( "<ol>" ) && text. ends_with ( "</ol>" ) ;
27
+ return is_unordered_list && is_ordered_list
28
+ }
29
+
14
30
#[ plugin_fn]
15
31
pub fn unordered_list ( Json ( args) : Json < Vec < Value > > ) -> FnResult < String > {
16
32
if args. len ( ) != 1 {
17
- return Err ( WithReturnCode :: new ( anyhow:: anyhow!( "Usage: [std.list elem1, elem2, ..., elemN]" ) , 1 ) ) ;
33
+ return Err ( WithReturnCode :: new (
34
+ anyhow:: anyhow!( "Usage: {{std.-list elem1, elem2, ..., elemN}}" ) ,
35
+ 1 ,
36
+ ) ) ;
18
37
}
19
38
let elems = match & args[ 0 ] {
20
39
Value :: TextArray ( t) => t,
@@ -27,7 +46,7 @@ pub fn unordered_list(Json(args): Json<Vec<Value>>) -> FnResult<String> {
27
46
} ;
28
47
let mut result = String :: new ( ) ;
29
48
for elem in elems {
30
- if elem . starts_with ( "<ul>" ) && elem . ends_with ( "</ul>" ) {
49
+ if is_list ( text ) {
31
50
result += elem;
32
51
continue ;
33
52
}
@@ -36,3 +55,31 @@ pub fn unordered_list(Json(args): Json<Vec<Value>>) -> FnResult<String> {
36
55
Ok ( format ! ( "<ul>{}</ul>" , result) )
37
56
}
38
57
58
+
59
+ #[ plugin_fn]
60
+ pub fn ordered_list ( Json ( args) : Json < Vec < Value > > ) -> FnResult < String > {
61
+ if args. len ( ) != 1 {
62
+ return Err ( WithReturnCode :: new (
63
+ anyhow:: anyhow!( "Usage: {{std.#list elem1, elem2, ..., elemN}}" ) ,
64
+ 1 ,
65
+ ) ) ;
66
+ }
67
+ let elems = match & args[ 0 ] {
68
+ Value :: TextArray ( t) => t,
69
+ _ => {
70
+ return Err ( WithReturnCode :: new (
71
+ anyhow:: anyhow!( "elems must be Value::TextArray" ) ,
72
+ 1 ,
73
+ ) )
74
+ }
75
+ } ;
76
+ let mut result = String :: new ( ) ;
77
+ for elem in elems {
78
+ if is_list ( elem) {
79
+ result += elem;
80
+ continue ;
81
+ }
82
+ result += & format ! ( "<li>{}</li>" , elem) ;
83
+ }
84
+ Ok ( format ! ( "<ol>{}</ol>" , result) )
85
+ }
0 commit comments