restorations from agent session
This commit is contained in:
22
parts/cli/Cargo.toml
Normal file
22
parts/cli/Cargo.toml
Normal file
@@ -0,0 +1,22 @@
|
||||
cargo-features = ["profile-rustflags"]
|
||||
|
||||
[package]
|
||||
name = "mize-part-cli"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[lib]
|
||||
name = "mize_part_cli"
|
||||
crate-type = [ "dylib", "rlib" ]
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "4.6.7", features = [ "string" ] }
|
||||
log = "0.4.34"
|
||||
mize = { workspace = true }
|
||||
|
||||
|
||||
[profile.dev]
|
||||
rustflags = ["-C", "prefer-dynamic", "-C", "link-args=-Wl,--export-dynamic"]
|
||||
|
||||
[profile.release]
|
||||
rustflags = ["-C", "prefer-dynamic"]
|
||||
@@ -1,10 +1,135 @@
|
||||
use mize_macros::{include_native_simple, part_impl};
|
||||
use clap::Command;
|
||||
use clap::{ArgMatches, Command};
|
||||
use mize::item::ItemData;
|
||||
use mize_macros::{include_native, part_impl};
|
||||
use std::collections::HashMap;
|
||||
use mize::prelude::*;
|
||||
|
||||
#[part_impl]
|
||||
pub fn part() -> mize::Instance {
|
||||
// Register the command with the new macro syntax
|
||||
include_native_simple!("Command", clap::Command, [new]);
|
||||
|
||||
mize::Instance::new()
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn part_cli() -> MizeResult<()> {
|
||||
|
||||
include_native!("Command", clap::Command, [new]);
|
||||
|
||||
opt("state")
|
||||
.fun(Box::new(|| {
|
||||
println!("hiiiiii");
|
||||
let mize = get_global_mize().unwrap();
|
||||
let state = CliPartState {
|
||||
cmds: HashMap::new(),
|
||||
current_cmd_name: "marts-cli".to_string(),
|
||||
actions: HashMap::new(),
|
||||
parsers: Some(Vec::new()),
|
||||
mize: mize.clone(),
|
||||
};
|
||||
println!("hooooo");
|
||||
Ok(ItemData::from_native(state)?)
|
||||
}));
|
||||
|
||||
|
||||
method("set_command", |args| {
|
||||
println!("in set command");
|
||||
let mut state = get_config("self.state")?.native_mut::<CliPartState>()?;
|
||||
let cmd = args.arr_nth(0)?.native::<String>()?;
|
||||
state.current_cmd_name = cmd.to_owned();
|
||||
if !state.cmds.contains_key(&*cmd) {
|
||||
state.cmds
|
||||
.insert((*cmd).to_owned(), Command::new((*cmd).to_owned()));
|
||||
}
|
||||
Ok(ItemData::new())
|
||||
});
|
||||
|
||||
method("subcommand", |args| {
|
||||
println!("in subcommand");
|
||||
let mut state = get_config("self.state")?.native_mut::<CliPartState>()?;
|
||||
let subcmd = args.arr_nth(0)?.native_take::<clap::Command>()?;
|
||||
let action = args.arr_nth(1)?.native_take::<Box<dyn Fn(ArgMatches) -> MizeResult<()> + Send + Sync>>()?;
|
||||
let name = subcmd.get_name().to_string();
|
||||
let cur_cmd_name = state.current_cmd_name.clone();
|
||||
let cmd = state
|
||||
.cmds
|
||||
.remove(&cur_cmd_name)
|
||||
.unwrap()
|
||||
.subcommand(subcmd);
|
||||
state.cmds.insert(cur_cmd_name.clone(), cmd);
|
||||
state.actions.insert(name, action);
|
||||
Ok(ItemData::new())
|
||||
});
|
||||
|
||||
// TODO in future....
|
||||
// Register methods from the impl block
|
||||
//CliPartState::methods_from_impl()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub struct CliPartState {
|
||||
cmds: HashMap<String, Command>,
|
||||
current_cmd_name: String,
|
||||
actions:
|
||||
HashMap<String, Box<dyn Fn(ArgMatches) -> MizeResult<()> + Send + Sync + 'static>>,
|
||||
parsers: Option<Vec<Box<dyn Fn(Mize, Vec<String>) -> MizeResult<()> + Send + Sync + 'static>>>,
|
||||
mize: Mize,
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
#[part_impl]
|
||||
impl CliPartState {
|
||||
pub fn set_command(&mut self, cmd: &str) {
|
||||
self.current_cmd_name = cmd.to_owned();
|
||||
if !self.cmds.contains_key(cmd) {
|
||||
self.cmds
|
||||
.insert(cmd.to_owned(), Command::new(cmd.to_owned()));
|
||||
}
|
||||
}
|
||||
pub fn subcommand(
|
||||
&mut self,
|
||||
subcmd: Command,
|
||||
action: Box<dyn Fn(&ArgMatches, Mize) -> MizeResult<()> + Send + Sync + 'static>,
|
||||
) -> MizeResult<()> {
|
||||
let name = subcmd.get_name().to_string();
|
||||
let cmd = self
|
||||
.cmds
|
||||
.remove(&self.current_cmd_name)
|
||||
.unwrap()
|
||||
.subcommand(subcmd);
|
||||
self.cmds.insert(self.current_cmd_name.clone(), cmd);
|
||||
self.actions.insert(name, action);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn add_sub_parser<T: Fn(Mize, Vec<String>) -> MizeResult<()> + Send + Sync + 'static>(
|
||||
&mut self,
|
||||
parser: T,
|
||||
) -> MizeResult<()> {
|
||||
self.parsers.as_mut().unwrap().push(Box::new(parser));
|
||||
Ok(())
|
||||
}
|
||||
*/
|
||||
|
||||
pub fn with_cmd(
|
||||
&mut self,
|
||||
func: fn(cmd: Command) -> MizeResult<Command>,
|
||||
) -> MizeResult<()> {
|
||||
let cmd = self.cmds.remove(&self.current_cmd_name).ok_or(mize_err!(
|
||||
"current command {} not found",
|
||||
self.current_cmd_name
|
||||
))?;
|
||||
let cmd = func(cmd)?;
|
||||
self.cmds.insert(self.current_cmd_name.clone(), cmd);
|
||||
Ok(())
|
||||
}
|
||||
pub fn run(&mut self, cmdline: Vec<String>) -> MizeResult<()> {
|
||||
let cmd_name = cmdline.iter().nth(0).ok_or(mize_err!("empty cmdline"))?;
|
||||
let cmd = self.cmds.remove(cmd_name).ok_or(mize_err!("cmd {} not found in CliPart", cmd_name))?;
|
||||
|
||||
let matches = cmd.get_matches_from(cmdline);
|
||||
let sub_name = matches.subcommand_name().ok_or(mize_err!("subcommand not found"))?;
|
||||
let sub_matches = matches.subcommand_matches(sub_name).ok_or(mize_err!("subcommand matches not found"))?;
|
||||
let action = self.actions.remove(sub_name).ok_or(mize_err!("action for subcommand not found"))?;
|
||||
action(sub_matches, self.mize.clone())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user