|
| 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_image() -> FnResult<Json<MetaData>> { |
| 6 | + Ok(Json(MetaData { |
| 7 | + command_name: "img".to_string(), |
| 8 | + call_name: "image".to_string(), |
| 9 | + argument_types: vec![ |
| 10 | + ("src".to_string(), Type::TInline), |
| 11 | + ("alt".to_string(), Type::TInline), |
| 12 | + ( |
| 13 | + "caption".to_string(), |
| 14 | + Type::TOption(Box::new(Type::TInline)), |
| 15 | + ), |
| 16 | + ], |
| 17 | + return_type: Type::TBlock, |
| 18 | + })) |
| 19 | +} |
| 20 | + |
| 21 | +#[plugin_fn] |
| 22 | +pub fn image(Json(args): Json<Vec<Value>>) -> FnResult<String> { |
| 23 | + if args.len() != 3 { |
| 24 | + return Err(WithReturnCode::new( |
| 25 | + anyhow::anyhow!( |
| 26 | + "Usage: |
| 27 | + 1. {{std.img src, alt}} |
| 28 | + 2. {{std.img src, alt, caption}}" |
| 29 | + ), |
| 30 | + 1, |
| 31 | + )); |
| 32 | + } |
| 33 | + let src = match &args[0] { |
| 34 | + Value::Text(t) => t, |
| 35 | + _ => { |
| 36 | + return Err(WithReturnCode::new( |
| 37 | + anyhow::anyhow!("src must be Value::Text"), |
| 38 | + 1, |
| 39 | + )) |
| 40 | + } |
| 41 | + }; |
| 42 | + let alt = match &args[1] { |
| 43 | + Value::Text(t) => t, |
| 44 | + _ => { |
| 45 | + return Err(WithReturnCode::new( |
| 46 | + anyhow::anyhow!("alt must be Value::Text"), |
| 47 | + 1, |
| 48 | + )) |
| 49 | + } |
| 50 | + }; |
| 51 | + let caption = match &args[2] { |
| 52 | + Value::TextOption(to) => to, |
| 53 | + _ => { |
| 54 | + return Err(WithReturnCode::new( |
| 55 | + anyhow::anyhow!("caption must be Value::TextOption"), |
| 56 | + 1, |
| 57 | + )) |
| 58 | + } |
| 59 | + }; |
| 60 | + match caption { |
| 61 | + Some(cap) => Ok(format!( |
| 62 | + "<figure><img src=\"{}\" alt=\"{}\" /><figcaption>{}</figcaption></figire>", |
| 63 | + src, alt, cap |
| 64 | + )), |
| 65 | + None => Ok(format!( |
| 66 | + "<figure><img src=\"{}\" alt=\"{}\" /></figire>", |
| 67 | + src, alt |
| 68 | + )), |
| 69 | + } |
| 70 | +} |
0 commit comments