130 lines
4.9 KiB
Rust
130 lines
4.9 KiB
Rust
|
|
use std::error::Error;
|
||
|
|
use ansi_term::Colour;
|
||
|
|
|
||
|
|
/// Парсер команд для интерпретатора
|
||
|
|
pub struct CommandParser;
|
||
|
|
|
||
|
|
impl CommandParser {
|
||
|
|
pub fn parse_command(line: &str) -> Result<Command, Box<dyn Error>> {
|
||
|
|
let parts: Vec<&str> = line.trim().split_whitespace().collect();
|
||
|
|
if parts.is_empty() {
|
||
|
|
return Ok(Command::Empty);
|
||
|
|
}
|
||
|
|
|
||
|
|
match parts[0].to_lowercase().as_str() {
|
||
|
|
"exit" | "quit" => Ok(Command::Exit),
|
||
|
|
"help" => Ok(Command::Help),
|
||
|
|
"backup" => {
|
||
|
|
if parts.len() >= 2 {
|
||
|
|
Ok(Command::Backup {
|
||
|
|
path: parts[1].to_string(),
|
||
|
|
})
|
||
|
|
} else {
|
||
|
|
Err("Usage: backup <path>".into())
|
||
|
|
}
|
||
|
|
}
|
||
|
|
"restore" => {
|
||
|
|
if parts.len() >= 2 {
|
||
|
|
Ok(Command::Restore {
|
||
|
|
path: parts[1].to_string(),
|
||
|
|
})
|
||
|
|
} else {
|
||
|
|
Err("Usage: restore <path>".into())
|
||
|
|
}
|
||
|
|
}
|
||
|
|
"create" => {
|
||
|
|
if parts.len() >= 3 && parts[1].to_lowercase() == "procedure" {
|
||
|
|
if parts.len() >= 4 {
|
||
|
|
Ok(Command::CreateProcedure {
|
||
|
|
name: parts[2].to_string(),
|
||
|
|
code: parts[3..].join(" "),
|
||
|
|
})
|
||
|
|
} else {
|
||
|
|
Err("Usage: create procedure <name> <code>".into())
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
Ok(Command::LuaCode(line.to_string()))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
"call" => {
|
||
|
|
if parts.len() >= 3 && parts[1].to_lowercase() == "procedure" {
|
||
|
|
Ok(Command::CallProcedure {
|
||
|
|
name: parts[2].to_string(),
|
||
|
|
})
|
||
|
|
} else {
|
||
|
|
Ok(Command::LuaCode(line.to_string()))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
"drop" => {
|
||
|
|
if parts.len() >= 3 && parts[1].to_lowercase() == "procedure" {
|
||
|
|
Ok(Command::DropProcedure {
|
||
|
|
name: parts[2].to_string(),
|
||
|
|
})
|
||
|
|
} else {
|
||
|
|
Ok(Command::LuaCode(line.to_string()))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
_ => Ok(Command::LuaCode(line.to_string())),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn print_help() {
|
||
|
|
println!("\n{}", Colour::Cyan.paint("Available commands:"));
|
||
|
|
println!("{}", Colour::Yellow.paint(" General:"));
|
||
|
|
println!(" exit, quit - Exit the interpreter");
|
||
|
|
println!(" help - Show this help message");
|
||
|
|
println!();
|
||
|
|
println!("{}", Colour::Yellow.paint(" Database operations:"));
|
||
|
|
println!(" backup <path> - Create database backup");
|
||
|
|
println!(" restore <path> - Restore database from backup");
|
||
|
|
println!();
|
||
|
|
println!("{}", Colour::Yellow.paint(" Stored procedures:"));
|
||
|
|
println!(" create procedure <name> <code> - Create stored procedure");
|
||
|
|
println!(" call procedure <name> - Execute stored procedure");
|
||
|
|
println!(" drop procedure <name> - Delete stored procedure");
|
||
|
|
println!();
|
||
|
|
println!("{}", Colour::Yellow.paint(" Lua operations:"));
|
||
|
|
println!(" Any valid Lua code - Execute Lua code with DB API");
|
||
|
|
println!();
|
||
|
|
println!("{}", Colour::Cyan.paint("Lua DB API functions:"));
|
||
|
|
println!(" create_space(name) - Create space");
|
||
|
|
println!(" delete_space(name) - Delete space");
|
||
|
|
println!(" insert(space, key, value) - Insert document");
|
||
|
|
println!(" get(space, key) - Get document");
|
||
|
|
println!(" update(space, key, value) - Update document");
|
||
|
|
println!(" delete(space, key) - Delete document");
|
||
|
|
println!(" create_procedure(name, code) - Create stored procedure");
|
||
|
|
println!(" drop_procedure(name) - Drop stored procedure");
|
||
|
|
println!(" call_procedure(name) - Call stored procedure");
|
||
|
|
println!(" create_backup(path) - Create backup");
|
||
|
|
println!(" restore_backup(path) - Restore backup");
|
||
|
|
println!(" enable_replication() - Enable replication");
|
||
|
|
println!(" disable_replication() - Disable replication");
|
||
|
|
println!(" sync_replication() - Sync replication");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Команды интерпретатора
|
||
|
|
pub enum Command {
|
||
|
|
Exit,
|
||
|
|
Help,
|
||
|
|
Empty,
|
||
|
|
LuaCode(String),
|
||
|
|
Backup {
|
||
|
|
path: String,
|
||
|
|
},
|
||
|
|
Restore {
|
||
|
|
path: String,
|
||
|
|
},
|
||
|
|
CreateProcedure {
|
||
|
|
name: String,
|
||
|
|
code: String,
|
||
|
|
},
|
||
|
|
CallProcedure {
|
||
|
|
name: String,
|
||
|
|
},
|
||
|
|
DropProcedure {
|
||
|
|
name: String,
|
||
|
|
},
|
||
|
|
}
|