|
| 1 | +use brack_pdk_rs::{metadata::Metadata, types::Type, values::Value}; |
| 2 | +use extism_pdk::{plugin_fn, FnResult, Json, WithReturnCode}; |
| 3 | + |
| 4 | +pub(crate) fn metadata_headings() -> Vec<Metadata> { |
| 5 | + let mut metadata = Vec::new(); |
| 6 | + for i in 1..=6 { |
| 7 | + metadata.push(Metadata { |
| 8 | + command_name: "*".repeat(i), |
| 9 | + call_name: format!("headings_level{}", i), |
| 10 | + argument_types: vec![("text".to_string(), Type::TInline)], |
| 11 | + return_type: Type::TBlock, |
| 12 | + }); |
| 13 | + } |
| 14 | + metadata |
| 15 | +} |
| 16 | + |
| 17 | +#[plugin_fn] |
| 18 | +pub fn headings_level1(Json(args): Json<Vec<Value>>) -> FnResult<String> { |
| 19 | + if args.len() != 1 { |
| 20 | + return Err(WithReturnCode::new( |
| 21 | + anyhow::anyhow!("Usage: {{std.* text}}"), |
| 22 | + 1, |
| 23 | + )); |
| 24 | + } |
| 25 | + let text = match &args[0] { |
| 26 | + Value::Text(t) => t, |
| 27 | + _ => { |
| 28 | + return Err(WithReturnCode::new( |
| 29 | + anyhow::anyhow!("text must be Value::Text"), |
| 30 | + 1, |
| 31 | + )) |
| 32 | + } |
| 33 | + }; |
| 34 | + Ok(format!( |
| 35 | + "{{ |
| 36 | + \"t\": \"Header\", |
| 37 | + \"c\": [1, [], [ |
| 38 | + {} |
| 39 | + ]] |
| 40 | +}},", |
| 41 | + text |
| 42 | + )) |
| 43 | +} |
| 44 | + |
| 45 | +#[plugin_fn] |
| 46 | +pub fn headings_level2(Json(args): Json<Vec<Value>>) -> FnResult<String> { |
| 47 | + if args.len() != 1 { |
| 48 | + return Err(WithReturnCode::new( |
| 49 | + anyhow::anyhow!("Usage: {{std.** text}}"), |
| 50 | + 1, |
| 51 | + )); |
| 52 | + } |
| 53 | + let text = match &args[0] { |
| 54 | + Value::Text(t) => t, |
| 55 | + _ => { |
| 56 | + return Err(WithReturnCode::new( |
| 57 | + anyhow::anyhow!("text must be Value::Text"), |
| 58 | + 1, |
| 59 | + )) |
| 60 | + } |
| 61 | + }; |
| 62 | + Ok(format!( |
| 63 | + "{{ |
| 64 | + \"t\": \"Header\", |
| 65 | + \"c\": [2, [], [ |
| 66 | + {} |
| 67 | + ]] |
| 68 | +}},", |
| 69 | + text |
| 70 | + )) |
| 71 | +} |
| 72 | + |
| 73 | +#[plugin_fn] |
| 74 | +pub fn headings_level3(Json(args): Json<Vec<Value>>) -> FnResult<String> { |
| 75 | + if args.len() != 1 { |
| 76 | + return Err(WithReturnCode::new( |
| 77 | + anyhow::anyhow!("Usage: {{std.*** text}}"), |
| 78 | + 1, |
| 79 | + )); |
| 80 | + } |
| 81 | + let text = match &args[0] { |
| 82 | + Value::Text(t) => t, |
| 83 | + _ => { |
| 84 | + return Err(WithReturnCode::new( |
| 85 | + anyhow::anyhow!("text must be Value::Text"), |
| 86 | + 1, |
| 87 | + )) |
| 88 | + } |
| 89 | + }; |
| 90 | + Ok(format!( |
| 91 | + "{{ |
| 92 | + \"t\": \"Header\", |
| 93 | + \"c\": [3, [], [ |
| 94 | + {} |
| 95 | + ]] |
| 96 | +}},", |
| 97 | + text |
| 98 | + )) |
| 99 | +} |
| 100 | + |
| 101 | +#[plugin_fn] |
| 102 | +pub fn headings_level4(Json(args): Json<Vec<Value>>) -> FnResult<String> { |
| 103 | + if args.len() != 1 { |
| 104 | + return Err(WithReturnCode::new( |
| 105 | + anyhow::anyhow!("Usage: {{std.**** text}}"), |
| 106 | + 1, |
| 107 | + )); |
| 108 | + } |
| 109 | + let text = match &args[0] { |
| 110 | + Value::Text(t) => t, |
| 111 | + _ => { |
| 112 | + return Err(WithReturnCode::new( |
| 113 | + anyhow::anyhow!("text must be Value::Text"), |
| 114 | + 1, |
| 115 | + )) |
| 116 | + } |
| 117 | + }; |
| 118 | + Ok(format!( |
| 119 | + "{{ |
| 120 | + \"t\": \"Header\", |
| 121 | + \"c\": [4, [], [ |
| 122 | + {} |
| 123 | + ]] |
| 124 | +}},", |
| 125 | + text |
| 126 | + )) |
| 127 | +} |
| 128 | + |
| 129 | +#[plugin_fn] |
| 130 | +pub fn headings_level5(Json(args): Json<Vec<Value>>) -> FnResult<String> { |
| 131 | + if args.len() != 1 { |
| 132 | + return Err(WithReturnCode::new( |
| 133 | + anyhow::anyhow!("Usage: {{std.***** text}}"), |
| 134 | + 1, |
| 135 | + )); |
| 136 | + } |
| 137 | + let text = match &args[0] { |
| 138 | + Value::Text(t) => t, |
| 139 | + _ => { |
| 140 | + return Err(WithReturnCode::new( |
| 141 | + anyhow::anyhow!("text must be Value::Text"), |
| 142 | + 1, |
| 143 | + )) |
| 144 | + } |
| 145 | + }; |
| 146 | + Ok(format!( |
| 147 | + "{{ |
| 148 | + \"t\": \"Header\", |
| 149 | + \"c\": [5, [], [ |
| 150 | + {} |
| 151 | + ]] |
| 152 | +}},", |
| 153 | + text |
| 154 | + )) |
| 155 | +} |
| 156 | + |
| 157 | +#[plugin_fn] |
| 158 | +pub fn headings_level6(Json(args): Json<Vec<Value>>) -> FnResult<String> { |
| 159 | + if args.len() != 1 { |
| 160 | + return Err(WithReturnCode::new( |
| 161 | + anyhow::anyhow!("Usage: {{std.****** text}}"), |
| 162 | + 1, |
| 163 | + )); |
| 164 | + } |
| 165 | + let text = match &args[0] { |
| 166 | + Value::Text(t) => t, |
| 167 | + _ => { |
| 168 | + return Err(WithReturnCode::new( |
| 169 | + anyhow::anyhow!("text must be Value::Text"), |
| 170 | + 1, |
| 171 | + )) |
| 172 | + } |
| 173 | + }; |
| 174 | + Ok(format!( |
| 175 | + "{{ |
| 176 | + \"t\": \"Header\", |
| 177 | + \"c\": [6, [], [ |
| 178 | + {} |
| 179 | + ]] |
| 180 | +}},", |
| 181 | + text |
| 182 | + )) |
| 183 | +} |
0 commit comments