refactor(tvix/repl): Abstract out REPL command handling

Prepare for introducing additional REPL commands by splitting out
the *parsing* of the repl commands, which returns a new ReplCommand
type, from the actual *handling* of the commands.

Change-Id: If81a53c1e2d90204d26ce3bb2ea9eebf7bb3fd51
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11734
Autosubmit: aspen <root@gws.fyi>
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
This commit is contained in:
Aspen Smith 2024-05-27 15:49:08 -04:00 committed by clbot
parent 23589a1db3
commit 70c4b512c2

View file

@ -1,4 +1,5 @@
use std::{path::PathBuf, rc::Rc};
use std::path::PathBuf;
use std::rc::Rc;
use rustyline::{error::ReadlineError, Editor};
use tvix_glue::tvix_store_io::TvixStoreIO;
@ -13,6 +14,22 @@ fn state_dir() -> Option<PathBuf> {
path
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ReplCommand<'a> {
Expr(&'a str),
Explain(&'a str),
}
impl<'a> ReplCommand<'a> {
pub fn parse(input: &'a str) -> Self {
if let Some(without_prefix) = input.strip_prefix(":d ") {
Self::Explain(without_prefix)
} else {
Self::Expr(input)
}
}
}
#[derive(Debug)]
pub struct Repl {
/// In-progress multiline input, when the input so far doesn't parse as a complete expression
@ -69,24 +86,23 @@ impl Repl {
&line
};
let res = if let Some(without_prefix) = input.strip_prefix(":d ") {
interpret(
Rc::clone(&io_handle),
without_prefix,
None,
args,
true,
AllowIncomplete::Allow,
)
} else {
interpret(
let res = match ReplCommand::parse(input) {
ReplCommand::Expr(input) => interpret(
Rc::clone(&io_handle),
input,
None,
args,
false,
AllowIncomplete::Allow,
)
),
ReplCommand::Explain(input) => interpret(
Rc::clone(&io_handle),
input,
None,
args,
true,
AllowIncomplete::Allow,
),
};
match res {