From 70c4b512c2ca53f7e592c20bd9e200ac6a0ce853 Mon Sep 17 00:00:00 2001 From: Aspen Smith Date: Mon, 27 May 2024 15:49:08 -0400 Subject: 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 Tested-by: BuildkiteCI Reviewed-by: flokli --- tvix/cli/src/repl.rs | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) (limited to 'tvix/cli') diff --git a/tvix/cli/src/repl.rs b/tvix/cli/src/repl.rs index 8dde9d9bfdc0..7d29ae3d2c83 100644 --- a/tvix/cli/src/repl.rs +++ b/tvix/cli/src/repl.rs @@ -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 { 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( + let res = match ReplCommand::parse(input) { + ReplCommand::Expr(input) => interpret( Rc::clone(&io_handle), - without_prefix, + input, None, args, - true, + false, AllowIncomplete::Allow, - ) - } else { - interpret( + ), + ReplCommand::Explain(input) => interpret( Rc::clone(&io_handle), input, None, args, - false, + true, AllowIncomplete::Allow, - ) + ), }; match res { -- cgit 1.4.1