Address review comments r=nalexander.
* Bump rust version number. * Use `bail` when throwing errors. * Improve edn parser. * Remove references to unused `more` flag. * Improve naming of query and transact commands.
This commit is contained in:
parent
e0548e9be2
commit
00af587682
3 changed files with 22 additions and 23 deletions
|
@ -16,7 +16,7 @@ use rustc_version::version_matches;
|
||||||
|
|
||||||
/// MIN_VERSION should be changed when there's a new minimum version of rustc required
|
/// MIN_VERSION should be changed when there's a new minimum version of rustc required
|
||||||
/// to build the project.
|
/// to build the project.
|
||||||
static MIN_VERSION: &'static str = ">= 1.15.1";
|
static MIN_VERSION: &'static str = ">= 1.17.0";
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
if !version_matches(MIN_VERSION) {
|
if !version_matches(MIN_VERSION) {
|
||||||
|
|
|
@ -9,7 +9,9 @@
|
||||||
// specific language governing permissions and limitations under the License.
|
// specific language governing permissions and limitations under the License.
|
||||||
|
|
||||||
use combine::{
|
use combine::{
|
||||||
|
any,
|
||||||
eof,
|
eof,
|
||||||
|
look_ahead,
|
||||||
many1,
|
many1,
|
||||||
parser,
|
parser,
|
||||||
satisfy,
|
satisfy,
|
||||||
|
@ -36,10 +38,10 @@ use edn;
|
||||||
pub static HELP_COMMAND: &'static str = &"help";
|
pub static HELP_COMMAND: &'static str = &"help";
|
||||||
pub static OPEN_COMMAND: &'static str = &"open";
|
pub static OPEN_COMMAND: &'static str = &"open";
|
||||||
pub static CLOSE_COMMAND: &'static str = &"close";
|
pub static CLOSE_COMMAND: &'static str = &"close";
|
||||||
pub static QUERY_COMMAND: &'static str = &"query";
|
pub static LONG_QUERY_COMMAND: &'static str = &"query";
|
||||||
pub static ALT_QUERY_COMMAND: &'static str = &"q";
|
pub static SHORT_QUERY_COMMAND: &'static str = &"q";
|
||||||
pub static TRANSACT_COMMAND: &'static str = &"transact";
|
pub static LONG_TRANSACT_COMMAND: &'static str = &"transact";
|
||||||
pub static ALT_TRANSACT_COMMAND: &'static str = &"t";
|
pub static SHORT_TRANSACT_COMMAND: &'static str = &"t";
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
pub enum Command {
|
pub enum Command {
|
||||||
|
@ -83,10 +85,10 @@ pub fn command(s: &str) -> Result<Command, cli::Error> {
|
||||||
.with(arguments())
|
.with(arguments())
|
||||||
.map(|args| {
|
.map(|args| {
|
||||||
if args.len() < 1 {
|
if args.len() < 1 {
|
||||||
return Err(cli::ErrorKind::CommandParse("Missing required argument".to_string()).into());
|
bail!(cli::ErrorKind::CommandParse("Missing required argument".to_string()));
|
||||||
}
|
}
|
||||||
if args.len() > 1 {
|
if args.len() > 1 {
|
||||||
return Err(cli::ErrorKind::CommandParse(format!("Unrecognized argument {:?}", args[1])).into());
|
bail!(cli::ErrorKind::CommandParse(format!("Unrecognized argument {:?}", args[1])));
|
||||||
}
|
}
|
||||||
Ok(Command::Open(args[0].clone()))
|
Ok(Command::Open(args[0].clone()))
|
||||||
});
|
});
|
||||||
|
@ -97,26 +99,26 @@ pub fn command(s: &str) -> Result<Command, cli::Error> {
|
||||||
.skip(eof())
|
.skip(eof())
|
||||||
.map(|args| {
|
.map(|args| {
|
||||||
if args.len() > 0 {
|
if args.len() > 0 {
|
||||||
return Err(cli::ErrorKind::CommandParse(format!("Unrecognized argument {:?}", args[0])).into());
|
bail!(cli::ErrorKind::CommandParse(format!("Unrecognized argument {:?}", args[0])) );
|
||||||
}
|
}
|
||||||
Ok(Command::Close)
|
Ok(Command::Close)
|
||||||
});
|
});
|
||||||
|
|
||||||
let edn_arg_parser = || spaces()
|
|
||||||
.with(try(string("["))
|
|
||||||
.or(try(string("{")))
|
|
||||||
.then(|d| parser(move |input| {
|
|
||||||
let _: &str = input;
|
|
||||||
Ok((d.to_string() + input, Consumed::Empty(input)))
|
|
||||||
})));
|
|
||||||
|
|
||||||
let query_parser = try(string(QUERY_COMMAND)).or(try(string(ALT_QUERY_COMMAND)))
|
let edn_arg_parser = || spaces()
|
||||||
|
.with(look_ahead(string("[").or(string("{")))
|
||||||
|
.with(many1::<Vec<_>, _>(try(any())))
|
||||||
|
.and_then(|args| -> Result<String, cli::Error> {
|
||||||
|
Ok(args.iter().collect())
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
let query_parser = try(string(LONG_QUERY_COMMAND)).or(try(string(SHORT_QUERY_COMMAND)))
|
||||||
.with(edn_arg_parser())
|
.with(edn_arg_parser())
|
||||||
.map(|x| {
|
.map(|x| {
|
||||||
Ok(Command::Query(x))
|
Ok(Command::Query(x))
|
||||||
});
|
});
|
||||||
|
|
||||||
let transact_parser = try(string(TRANSACT_COMMAND)).or(try(string(ALT_TRANSACT_COMMAND)))
|
let transact_parser = try(string(LONG_TRANSACT_COMMAND)).or(try(string(SHORT_TRANSACT_COMMAND)))
|
||||||
.with(edn_arg_parser())
|
.with(edn_arg_parser())
|
||||||
.map( |x| {
|
.map( |x| {
|
||||||
Ok(Command::Transact(x))
|
Ok(Command::Transact(x))
|
||||||
|
|
|
@ -52,7 +52,6 @@ impl Repl {
|
||||||
|
|
||||||
/// Runs the REPL interactively.
|
/// Runs the REPL interactively.
|
||||||
pub fn run(&mut self) {
|
pub fn run(&mut self) {
|
||||||
let mut more = false;
|
|
||||||
let mut input = InputReader::new();
|
let mut input = InputReader::new();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
@ -61,13 +60,11 @@ impl Repl {
|
||||||
match res {
|
match res {
|
||||||
Ok(MetaCommand(cmd)) => {
|
Ok(MetaCommand(cmd)) => {
|
||||||
debug!("read command: {:?}", cmd);
|
debug!("read command: {:?}", cmd);
|
||||||
more = false;
|
|
||||||
self.handle_command(cmd);
|
self.handle_command(cmd);
|
||||||
},
|
},
|
||||||
Ok(Empty) => more = false,
|
Ok(Empty) |
|
||||||
Ok(More) => { more = true; },
|
Ok(More) => (),
|
||||||
Ok(Eof) => {
|
Ok(Eof) => {
|
||||||
more = false;
|
|
||||||
if input.is_tty() {
|
if input.is_tty() {
|
||||||
println!("");
|
println!("");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue