forking stuff

This commit is contained in:
mat 2022-04-17 21:38:57 -05:00
parent d68233e0b1
commit 1d28c7cfb5
5 changed files with 30 additions and 14 deletions

View file

@ -111,7 +111,9 @@ impl<S> ArgumentBuilder<S> {
redirect: self.target,
modifier: self.modifier,
forks: self.forks,
..Default::default()
arguments: Default::default(),
children: Default::default(),
literals: Default::default(),
};
for (_, argument) in &self.arguments.children {

View file

@ -53,8 +53,6 @@ impl<S> CommandContextBuilder<S> {
command: None,
dispatcher,
nodes: vec![],
// rootNode,
// start,
child: None,
modifier: None,
forks: false,
@ -157,6 +155,23 @@ impl<S> Clone for CommandContext<S> {
}
}
impl<S> Debug for CommandContext<S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CommandContext")
// .field("source", &self.source)
.field("input", &self.input)
// .field("arguments", &self.arguments)
// .field("command", &self.command)
// .field("root_node", &self.root_node)
// .field("nodes", &self.nodes)
.field("range", &self.range)
.field("child", &self.child)
// .field("modifier", &self.modifier)
.field("forks", &self.forks)
.finish()
}
}
impl<S> CommandContext<S> {
pub fn copy_for(&self, source: Rc<S>) -> Self {
if Rc::ptr_eq(&source, &self.source) {

View file

@ -190,6 +190,7 @@ impl<S> CommandDispatcher<S> {
for context in contexts.iter() {
let child = &context.child;
if let Some(child) = child {
println!("aaaaaaa {:?}", child);
forked |= child.forks;
if child.has_nodes() {
found_command = true;
@ -235,6 +236,7 @@ impl<S> CommandDispatcher<S> {
);
}
println!("forked: {}, successful forks: {}", forked, successful_forks);
Ok(if forked { successful_forks } else { result })
}
}
@ -248,7 +250,6 @@ impl<S> Clone for CommandDispatcher<S> {
}
}
/*
#[cfg(test)]
mod tests {
use super::*;
@ -258,6 +259,7 @@ mod tests {
parsers::integer,
};
#[derive(Debug, PartialEq)]
struct CommandSource {}
fn input_with_offset(input: &str, offset: usize) -> StringReader {
@ -775,13 +777,13 @@ mod tests {
let source1 = Rc::new(CommandSource {});
let source2 = Rc::new(CommandSource {});
let modifier = move |source: &CommandContext<Rc<CommandSource>>| -> Result<Vec<Rc<CommandSource>>, CommandSyntaxException> {
let modifier = move |source: &CommandContext<CommandSource>| -> Result<Vec<Rc<CommandSource>>, CommandSyntaxException> {
Ok(vec![source1.clone(), source2.clone()])
};
let concrete_node = subject.register(literal("actual").executes(|_| 42));
let redirect_node =
subject.register(literal("redirected").fork(subject.root.clone(), modifier));
subject.register(literal("redirected").fork(subject.root.clone(), Rc::new(modifier)));
let input = "redirected actual";
let parse = subject.parse(input.into(), Rc::new(CommandSource {}));
@ -799,7 +801,8 @@ mod tests {
assert_eq!(parse.context.root, subject.root);
assert_eq!(parent.nodes[0].range, parent.range);
assert_eq!(parent.nodes[0].node, concrete_node);
// assert_eq!(parent.source, Rc::new(CommandSource {}));
assert_eq!(parent.source, Rc::new(CommandSource {}));
assert_eq!(CommandDispatcher::execute_parsed(parse).unwrap(), 2);
}
}
*/

View file

@ -1,12 +1,8 @@
use std::{any::Any, rc::Rc};
use std::rc::Rc;
use crate::{
context::CommandContext, exceptions::command_syntax_exception::CommandSyntaxException,
};
// pub trait RedirectModifier<S> {
// fn apply(&self, context: &CommandContext<S>) -> Result<Vec<S>, CommandSyntaxException>;
// }
pub type RedirectModifier<S> =
dyn Fn(&CommandContext<S>) -> Result<Vec<Rc<S>>, CommandSyntaxException>;

View file

@ -2,7 +2,7 @@ use crate::{
context::CommandContextBuilder, exceptions::command_syntax_exception::CommandSyntaxException,
string_reader::StringReader, tree::CommandNode,
};
use std::{any::Any, collections::HashMap, fmt::Debug, rc::Rc};
use std::{collections::HashMap, fmt::Debug, rc::Rc};
pub struct ParseResults<S> {
pub context: CommandContextBuilder<S>,