Implement is_complete for transactions and queries

This commit is contained in:
Emily Toop 2017-05-16 16:34:07 +01:00
parent 102e528310
commit a8bb996e4f
5 changed files with 33 additions and 12 deletions

View file

@ -31,3 +31,6 @@ path = "../.."
[dependencies.mentat_parser_utils]
path = "../../parser-utils"
[dependencies.edn]
path = "../../edn"

View file

@ -30,6 +30,8 @@ use combine::combinator::{
use errors as cli;
use edn;
pub static HELP_COMMAND: &'static str = &"help";
pub static OPEN_COMMAND: &'static str = &"open";
pub static CLOSE_COMMAND: &'static str = &"close";
@ -52,8 +54,10 @@ impl Command {
/// TODO: for query and transact commands, they will be considered complete if a parsable EDN has been entered as an argument
pub fn is_complete(&self) -> bool {
match self {
&Command::Query(_) |
&Command::Transact(_) => false,
&Command::Query(ref args) |
&Command::Transact(ref args) => {
edn::parse::value(&args).is_ok()
},
&Command::Help(_) |
&Command::Open(_) |
&Command::Close => true

View file

@ -22,6 +22,12 @@ use command_parser::{
use errors as cli;
/// Starting prompt
const DEFAULT_PROMPT: &'static str = "mentat=> ";
/// Prompt when further input is being read
// TODO: Should this actually reflect the current open brace?
const MORE_PROMPT: &'static str = "mentat.> ";
/// Possible results from reading input from `InputReader`
#[derive(Clone, Debug)]
pub enum InputResult {
@ -66,8 +72,8 @@ impl InputReader {
/// Reads a single command, item, or statement from `stdin`.
/// Returns `More` if further input is required for a complete result.
/// In this case, the input received so far is buffered internally.
pub fn read_input(&mut self, prompt: &str) -> Result<InputResult, cli::Error> {
let line = match self.read_line(prompt) {
pub fn read_input(&mut self, in_process_cmd: Option<Command>) -> Result<InputResult, cli::Error> {
let line = match self.read_line(if in_process_cmd.is_some() { MORE_PROMPT } else { DEFAULT_PROMPT }) {
Some(s) => s,
None => return Ok(Eof),
};
@ -80,7 +86,19 @@ impl InputReader {
self.add_history(&line);
let cmd = try!(command(&self.buffer));
let cmd = match in_process_cmd {
Some(Command::Query(args)) => {
Command::Query(args + " " + &line)
},
Some(Command::Transact(args)) => {
Command::Transact(args + " " + &line)
},
_ => {
try!(command(&self.buffer))
}
};
println!("processing {:?}", cmd);
match cmd {
Command::Query(_) |

View file

@ -21,6 +21,7 @@ extern crate linefeed;
extern crate rusqlite;
extern crate mentat;
extern crate edn;
use getopts::Options;

View file

@ -27,12 +27,6 @@ use store::{
db_output_name
};
/// Starting prompt
const DEFAULT_PROMPT: &'static str = "mentat=> ";
/// Prompt when further input is being read
// TODO: Should this actually reflect the current open brace?
const MORE_PROMPT: &'static str = "mentat.> ";
lazy_static! {
static ref COMMAND_HELP: HashMap<&'static str, &'static str> = {
let mut map = HashMap::new();
@ -62,7 +56,8 @@ impl Repl {
let mut input = InputReader::new();
loop {
let res = input.read_input(if more.is_some() { MORE_PROMPT } else { DEFAULT_PROMPT });
let res = input.read_input(more.clone());
match res {
Ok(MetaCommand(cmd)) => {
debug!("read command: {:?}", cmd);