too much fucking refactoring.
This commit is contained in:
1 parent
c8ae3d3ff9
commit
920ab5d188
7 files changed
+107
-108
No files matched your search
+60
-68
@@ -1,7 +1,7 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use clap::{Arg, ArgAction, ArgMatches, Command, value_parser};
|
||||
use clap::parser::ValuesRef;
|
||||
use clap::{value_parser, Arg, ArgAction, ArgMatches, Command};
|
||||
use log::{debug, error, info};
|
||||
use rusqlite::Connection;
|
||||
use serde::Serialize;
|
||||
@@ -12,6 +12,7 @@ use db::securities::list_securities;
|
||||
use crate::db::users::{create_user, list_users};
|
||||
use crate::event_processor::process_event_files;
|
||||
use crate::migration::apply_migrations;
|
||||
use crate::util::{CustomResult, Error};
|
||||
use crate::webui::web_ui_main;
|
||||
|
||||
mod db;
|
||||
@@ -37,15 +38,15 @@ impl<T> Output<T> {
|
||||
}
|
||||
|
||||
trait CommandLineUserLogic {
|
||||
fn create_user(&mut self, conn: &mut Connection, name: &String, public_key: &String);
|
||||
fn create_user(&mut self, conn: &mut Connection, name: &String, public_key: &String) -> CustomResult<()>;
|
||||
|
||||
fn list_user(&mut self, conn: &Connection);
|
||||
fn list_user(&mut self, conn: &Connection) -> CustomResult<()>;
|
||||
|
||||
fn list_securities(&mut self, conn: &Connection);
|
||||
fn list_securities(&mut self, conn: &Connection) -> CustomResult<()>;
|
||||
|
||||
fn process_events(&mut self, conn: &mut Connection, files: ValuesRef<PathBuf>);
|
||||
fn process_events(&mut self, conn: &mut Connection, files: ValuesRef<PathBuf>) -> CustomResult<()>;
|
||||
|
||||
fn run_webui(&mut self, conn: &mut Connection, port: u16, enable_template_autoreload: bool);
|
||||
fn run_webui(&mut self, conn: &mut Connection, port: u16, enable_template_autoreload: bool) -> CustomResult<()>;
|
||||
}
|
||||
|
||||
fn verbosity_arg() -> Arg {
|
||||
@@ -178,34 +179,34 @@ fn process_new_user_command_line(
|
||||
conn: &mut Connection,
|
||||
m: &ArgMatches,
|
||||
logic: &mut dyn CommandLineUserLogic,
|
||||
) {
|
||||
) -> CustomResult<()> {
|
||||
debug!("Processing new user command line {:?}.", m);
|
||||
let name = m.get_one::<String>("name").unwrap();
|
||||
let pk = m.get_one::<String>("public_key").unwrap();
|
||||
logic.create_user(conn, name, pk);
|
||||
logic.create_user(conn, name, pk)
|
||||
}
|
||||
|
||||
fn process_list_user_command_line(
|
||||
conn: &Connection,
|
||||
m: &ArgMatches,
|
||||
logic: &mut dyn CommandLineUserLogic,
|
||||
) {
|
||||
) -> CustomResult<()>{
|
||||
debug!("Processing list users command line {:?}.", m);
|
||||
logic.list_user(conn);
|
||||
logic.list_user(conn)
|
||||
}
|
||||
|
||||
fn process_user_command_line(
|
||||
conn: &mut Connection,
|
||||
m: &ArgMatches,
|
||||
logic: &mut dyn CommandLineUserLogic,
|
||||
) {
|
||||
) -> CustomResult<()> {
|
||||
debug!("Processing user command line {:?}.", m);
|
||||
match m.subcommand() {
|
||||
Some(("new", new_user_match)) => {
|
||||
process_new_user_command_line(conn, new_user_match, logic);
|
||||
process_new_user_command_line(conn, new_user_match, logic)
|
||||
}
|
||||
Some(("ls", list_user_match)) => {
|
||||
process_list_user_command_line(conn, list_user_match, logic);
|
||||
process_list_user_command_line(conn, list_user_match, logic)
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
@@ -215,46 +216,46 @@ fn process_list_securities_command_line(
|
||||
conn: &Connection,
|
||||
m: &ArgMatches,
|
||||
logic: &mut dyn CommandLineUserLogic,
|
||||
) {
|
||||
) -> CustomResult<()>{
|
||||
debug!("Processing list securities command line {:?}.", m);
|
||||
logic.list_securities(conn);
|
||||
logic.list_securities(conn)
|
||||
}
|
||||
|
||||
fn process_process_events_command_line(
|
||||
conn: &mut Connection,
|
||||
m: &ArgMatches,
|
||||
logic: &mut dyn CommandLineUserLogic,
|
||||
) {
|
||||
) -> CustomResult<()> {
|
||||
debug!("Processing process event command line {:?}.", m);
|
||||
let mut files = m.get_many::<PathBuf>("PATH").unwrap();
|
||||
if files.any(|x| !x.exists()) {
|
||||
error!("Some passed files do not exists. Existing right now.");
|
||||
return;
|
||||
return Err(Error::UserInputError("Some passed files do not exists. Existing right now.".to_string()));
|
||||
}
|
||||
|
||||
logic.process_events(conn, files);
|
||||
logic.process_events(conn, files)
|
||||
}
|
||||
|
||||
fn process_run_wui_command_line(
|
||||
conn: &mut Connection,
|
||||
m: &ArgMatches,
|
||||
logic: &mut dyn CommandLineUserLogic,
|
||||
) {
|
||||
) -> CustomResult<()> {
|
||||
debug!("Processing run wui command line {:?}.", m);
|
||||
let port = m.get_one::<u16>("port").unwrap_or(&2222);
|
||||
let enable_template_autoreload = m.get_flag("enable-template-autoreload");
|
||||
logic.run_webui(conn, *port, enable_template_autoreload);
|
||||
logic.run_webui(conn, *port, enable_template_autoreload)
|
||||
}
|
||||
|
||||
fn process_securities_command_line(
|
||||
conn: &Connection,
|
||||
m: &ArgMatches,
|
||||
logic: &mut dyn CommandLineUserLogic,
|
||||
) {
|
||||
) -> CustomResult<()>{
|
||||
debug!("Processing securities command line {:?}.", m);
|
||||
match m.subcommand() {
|
||||
Some(("ls", list_securities_match)) => {
|
||||
process_list_securities_command_line(conn, list_securities_match, logic);
|
||||
process_list_securities_command_line(conn, list_securities_match, logic)
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
@@ -264,11 +265,11 @@ fn process_events_command_line(
|
||||
conn: &mut Connection,
|
||||
m: &ArgMatches,
|
||||
logic: &mut dyn CommandLineUserLogic,
|
||||
) {
|
||||
) -> CustomResult<()>{
|
||||
debug!("Processing events command line {:?}.", m);
|
||||
match m.subcommand() {
|
||||
Some(("process", process_events_match)) => {
|
||||
process_process_events_command_line(conn, process_events_match, logic);
|
||||
process_process_events_command_line(conn, process_events_match, logic)
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
@@ -278,11 +279,11 @@ fn process_wui_command_line(
|
||||
conn: &mut Connection,
|
||||
m: &ArgMatches,
|
||||
logic: &mut dyn CommandLineUserLogic,
|
||||
) {
|
||||
) -> CustomResult<()> {
|
||||
debug!("Processing wui command line {:?}.", m);
|
||||
match m.subcommand() {
|
||||
Some(("run", run_wui_match)) => {
|
||||
process_run_wui_command_line(conn, run_wui_match, logic);
|
||||
process_run_wui_command_line(conn, run_wui_match, logic)
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
@@ -292,20 +293,20 @@ fn process_command_line(
|
||||
conn: &mut Connection,
|
||||
m: &ArgMatches,
|
||||
logic: &mut dyn CommandLineUserLogic,
|
||||
) {
|
||||
) -> CustomResult<()> {
|
||||
debug!("Processing command line {:?}.", m);
|
||||
match m.subcommand() {
|
||||
Some(("users", user_match)) => {
|
||||
process_user_command_line(conn, user_match, logic);
|
||||
process_user_command_line(conn, user_match, logic)
|
||||
}
|
||||
Some(("securities", securities_match)) => {
|
||||
process_securities_command_line(conn, securities_match, logic);
|
||||
process_securities_command_line(conn, securities_match, logic)
|
||||
}
|
||||
Some(("events", events_match)) => {
|
||||
process_events_command_line(conn, events_match, logic);
|
||||
process_events_command_line(conn, events_match, logic)
|
||||
}
|
||||
Some(("wui", wui_match)) => {
|
||||
process_wui_command_line(conn, wui_match, logic);
|
||||
process_wui_command_line(conn, wui_match, logic)
|
||||
}
|
||||
_ => {
|
||||
println!("Received un parsable command. {:?}", m.subcommand());
|
||||
@@ -339,20 +340,24 @@ mod cli_test {
|
||||
}
|
||||
|
||||
impl CommandLineUserLogic for TestCommandLineLogic {
|
||||
fn create_user(&mut self, _conn: &mut Connection, _name: &String, _public_key: &String) {
|
||||
fn create_user(&mut self, _conn: &mut Connection, _name: &String, _public_key: &String) -> CustomResult<()> {
|
||||
self.create_user_count += 1;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn list_user(&mut self, _conn: &Connection) {
|
||||
fn list_user(&mut self, _conn: &Connection) -> CustomResult<()>{
|
||||
self.list_user_count += 1;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn list_securities(&mut self, _conn: &Connection) {
|
||||
fn list_securities(&mut self, _conn: &Connection) -> CustomResult<()>{
|
||||
self.list_securities_count += 1;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn process_events(&mut self, _conn: &mut Connection, _files: ValuesRef<PathBuf>) {
|
||||
fn process_events(&mut self, _conn: &mut Connection, _files: ValuesRef<PathBuf>) -> CustomResult<()> {
|
||||
self.process_events_count += 1;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn run_webui(
|
||||
@@ -360,8 +365,9 @@ mod cli_test {
|
||||
_conn: &mut Connection,
|
||||
_port: u16,
|
||||
_enable_template_autoreload: bool,
|
||||
) {
|
||||
) -> CustomResult<()> {
|
||||
self.run_webui_count += 1;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -465,47 +471,33 @@ impl ProCommandLineLogic {
|
||||
}
|
||||
|
||||
impl CommandLineUserLogic for ProCommandLineLogic {
|
||||
fn create_user(&mut self, conn: &mut Connection, name: &String, public_key: &String) {
|
||||
let tx = conn.transaction().unwrap();
|
||||
create_user(&tx, name, public_key).unwrap();
|
||||
tx.commit().unwrap();
|
||||
fn create_user(&mut self, conn: &mut Connection, name: &String, public_key: &String) -> CustomResult<()> {
|
||||
let tx = conn.transaction().map_err(Error::SqlError)?;
|
||||
create_user(&tx, name, public_key)?;
|
||||
tx.commit().map_err(Error::SqlError)
|
||||
}
|
||||
|
||||
fn list_user(&mut self, conn: &Connection) {
|
||||
let users = list_users(conn);
|
||||
match users {
|
||||
Ok(u) => {
|
||||
let out = Output::success(u);
|
||||
println!("{}", serde_json::to_string(&out).unwrap());
|
||||
}
|
||||
Err(e) => {
|
||||
println!("{:?}", e);
|
||||
}
|
||||
}
|
||||
fn list_user(&mut self, conn: &Connection) -> CustomResult<()> {
|
||||
let users = list_users(conn)?;
|
||||
let out = Output::success(users);
|
||||
println!("{}", serde_json::to_string(&out).unwrap());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn list_securities(&mut self, conn: &Connection) {
|
||||
let securities = list_securities(conn);
|
||||
match securities {
|
||||
Ok(s) => {
|
||||
let out = Output::success(s);
|
||||
println!("{}", serde_json::to_string(&out).unwrap());
|
||||
}
|
||||
Err(e) => {
|
||||
println!("{:?}", e);
|
||||
}
|
||||
};
|
||||
fn list_securities(&mut self, conn: &Connection) -> CustomResult<()> {
|
||||
let securities = list_securities(conn)?;
|
||||
let out = Output::success(securities);
|
||||
println!("{}", serde_json::to_string(&out).unwrap());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn process_events(&mut self, conn: &mut Connection, files: ValuesRef<PathBuf>) {
|
||||
process_event_files(conn, files).unwrap();
|
||||
|
||||
println!("fuck me");
|
||||
fn process_events(&mut self, conn: &mut Connection, files: ValuesRef<PathBuf>) -> CustomResult<()> {
|
||||
process_event_files(conn, files)
|
||||
}
|
||||
|
||||
fn run_webui(&mut self, _conn: &mut Connection, port: u16, enable_template_autoreload: bool) {
|
||||
fn run_webui(&mut self, _conn: &mut Connection, port: u16, enable_template_autoreload: bool) -> CustomResult<()> {
|
||||
info!("Serving web-ui on port {:?}", port);
|
||||
web_ui_main(port, enable_template_autoreload).unwrap();
|
||||
web_ui_main(port, enable_template_autoreload)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user