atri_bot icon indicating copy to clipboard operation
atri_bot copied to clipboard

指令系统设想

Open LaoLittle opened this issue 1 year ago • 1 comments

指令系统

单指令

单个可以执行的指令

#[command(name = "x", help="a")]
async fn a(arg1: ..) -> Result<(), ?> {

}

调用方式: /x arg1..

复合指令

#[command(name = "x")]
mod a {
  #[command(name = "y")]
  fn b() {}
  
  #[command(name = "z")]
  mod c {}
}

调用方式: /x y or /x z ...

指令执行器

所有的指令应先注册到执行器 然后由执行器统一执行

指令参数分析器

参数与参数间应有空格 将消息隔开并迭代, 尝试将字符串一个个解析为对应的参数 失败则不执行此指令

定义trait CommandArg, 要求参数实现CommandArg

宏生成后的代码类似:

let mut iter = cmd.split(' ');
a(
  <A as CommandArg>::from_str(iter.next()?)?,
  <B as CommandArg>::from_str(iter.next()?)?,
  <C as CommandArg>::from_str(iter.next()?)?,
).await

对于同步函数,应额外加上block_in_place

FFI

暴露指令注册函数 函数应为同步函数或异步函数(返回FFIFuture)

union CommandUnion {
  fun: extern "C" fn(..),
  async_fun: extern "C" fn(..) -> FFIFuture,
  set: RustVec<FFICommand>,
}

struct FFICommand {
  name: RustStr,
  type: u8, // 0: sync, 1: async, 2: complex
  inner: CommandUnion
}

LaoLittle avatar Dec 16 '22 06:12 LaoLittle