Skip to content

Commit

Permalink
add: headings commands
Browse files Browse the repository at this point in the history
  • Loading branch information
momeemt committed Oct 18, 2024
1 parent 6952d4c commit ceef7e2
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 0 deletions.
183 changes: 183 additions & 0 deletions src/headings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
use brack_pdk_rs::{metadata::Metadata, types::Type, values::Value};
use extism_pdk::{plugin_fn, FnResult, Json, WithReturnCode};

pub(crate) fn metadata_headings() -> Vec<Metadata> {
let mut metadata = Vec::new();
for i in 1..=6 {
metadata.push(Metadata {
command_name: "*".repeat(i),
call_name: format!("headings_level{}", i),
argument_types: vec![("text".to_string(), Type::TInline)],
return_type: Type::TBlock,
});
}
metadata
}

#[plugin_fn]
pub fn headings_level1(Json(args): Json<Vec<Value>>) -> FnResult<String> {
if args.len() != 1 {
return Err(WithReturnCode::new(
anyhow::anyhow!("Usage: {{std.* text}}"),
1,
));
}
let text = match &args[0] {
Value::Text(t) => t,
_ => {
return Err(WithReturnCode::new(
anyhow::anyhow!("text must be Value::Text"),
1,
))
}
};
Ok(format!(
"{{
\"t\": \"Header\",
\"c\": [1, [], [
{}
]]
}},",
text
))
}

#[plugin_fn]
pub fn headings_level2(Json(args): Json<Vec<Value>>) -> FnResult<String> {
if args.len() != 1 {
return Err(WithReturnCode::new(
anyhow::anyhow!("Usage: {{std.** text}}"),
1,
));
}
let text = match &args[0] {
Value::Text(t) => t,
_ => {
return Err(WithReturnCode::new(
anyhow::anyhow!("text must be Value::Text"),
1,
))
}
};
Ok(format!(
"{{
\"t\": \"Header\",
\"c\": [2, [], [
{}
]]
}},",
text
))
}

#[plugin_fn]
pub fn headings_level3(Json(args): Json<Vec<Value>>) -> FnResult<String> {
if args.len() != 1 {
return Err(WithReturnCode::new(
anyhow::anyhow!("Usage: {{std.*** text}}"),
1,
));
}
let text = match &args[0] {
Value::Text(t) => t,
_ => {
return Err(WithReturnCode::new(
anyhow::anyhow!("text must be Value::Text"),
1,
))
}
};
Ok(format!(
"{{
\"t\": \"Header\",
\"c\": [3, [], [
{}
]]
}},",
text
))
}

#[plugin_fn]
pub fn headings_level4(Json(args): Json<Vec<Value>>) -> FnResult<String> {
if args.len() != 1 {
return Err(WithReturnCode::new(
anyhow::anyhow!("Usage: {{std.**** text}}"),
1,
));
}
let text = match &args[0] {
Value::Text(t) => t,
_ => {
return Err(WithReturnCode::new(
anyhow::anyhow!("text must be Value::Text"),
1,
))
}
};
Ok(format!(
"{{
\"t\": \"Header\",
\"c\": [4, [], [
{}
]]
}},",
text
))
}

#[plugin_fn]
pub fn headings_level5(Json(args): Json<Vec<Value>>) -> FnResult<String> {
if args.len() != 1 {
return Err(WithReturnCode::new(
anyhow::anyhow!("Usage: {{std.***** text}}"),
1,
));
}
let text = match &args[0] {
Value::Text(t) => t,
_ => {
return Err(WithReturnCode::new(
anyhow::anyhow!("text must be Value::Text"),
1,
))
}
};
Ok(format!(
"{{
\"t\": \"Header\",
\"c\": [5, [], [
{}
]]
}},",
text
))
}

#[plugin_fn]
pub fn headings_level6(Json(args): Json<Vec<Value>>) -> FnResult<String> {
if args.len() != 1 {
return Err(WithReturnCode::new(
anyhow::anyhow!("Usage: {{std.****** text}}"),
1,
));
}
let text = match &args[0] {
Value::Text(t) => t,
_ => {
return Err(WithReturnCode::new(
anyhow::anyhow!("text must be Value::Text"),
1,
))
}
};
Ok(format!(
"{{
\"t\": \"Header\",
\"c\": [6, [], [
{}
]]
}},",
text
))
}
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ use bold::metadata_bold;
use brack_pdk_rs::metadata::Metadata;
use document::metadata_document;
use extism_pdk::{plugin_fn, FnResult, Json};
use headings::metadata_headings;
use stmt::metadata_stmt;
use text::metadata_text;

pub mod document;
pub mod bold;
pub mod stmt;
pub mod text;
pub mod headings;

#[plugin_fn]
pub fn get_metadata() -> FnResult<Json<Vec<Metadata>>> {
Expand All @@ -17,5 +19,8 @@ pub fn get_metadata() -> FnResult<Json<Vec<Metadata>>> {
metadata.push(metadata_document());
metadata.push(metadata_stmt());
metadata.push(metadata_text());
for m in metadata_headings() {
metadata.push(m);
}
Ok(Json(metadata))
}

0 comments on commit ceef7e2

Please sign in to comment.