1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
mod args;
mod controller;

pub use args::{describe, show};
use clap::Parser;
pub use controller::VitUserInteractionController;
use hersir::controller::interactive::args::explorer;
use hersir::controller::interactive::args::send;
use hersir::controller::UserInteractionController;
use jortestkit::prelude::ConsoleWriter;
use jortestkit::prelude::InteractiveCommandError;
use jortestkit::prelude::InteractiveCommandExec;
use std::ffi::OsStr;

pub struct VitInteractiveCommandExec {
    pub vit_controller: VitUserInteractionController,
    pub controller: UserInteractionController,
}

impl VitInteractiveCommandExec {
    pub fn vit_controller_mut(&mut self) -> &mut VitUserInteractionController {
        &mut self.vit_controller
    }

    pub fn controller_mut(&mut self) -> &mut UserInteractionController {
        &mut self.controller
    }
}

impl VitInteractiveCommandExec {
    pub fn tear_down(self) {
        self.vit_controller.finalize();
        // TODO: what happend to this?
        // self.controller.finalize();
    }
}

impl InteractiveCommandExec for VitInteractiveCommandExec {
    fn parse_and_exec(
        &mut self,
        tokens: Vec<String>,
        console: ConsoleWriter,
    ) -> std::result::Result<(), InteractiveCommandError> {
        match VitInteractiveCommand::try_parse_from(tokens.iter().map(OsStr::new)) {
            Ok(interactive) => {
                if let Err(err) = {
                    match interactive {
                        VitInteractiveCommand::Show(show) => {
                            show.exec(self);
                            Ok(())
                        }
                        VitInteractiveCommand::Exit => Ok(()),
                        VitInteractiveCommand::Describe(describe) => describe.exec(self),
                        VitInteractiveCommand::Send(send) => {
                            send.exec(self.controller_mut()).map_err(Into::into)
                        }
                        VitInteractiveCommand::Explorer(explorer) => {
                            explorer.exec(self.controller_mut()).map_err(Into::into)
                        }
                    }
                } {
                    console.format_error(InteractiveCommandError::UserError(err.to_string()));
                }
            }
            Err(err) => console.show_help(InteractiveCommandError::UserError(err.to_string())),
        }
        Ok(())
    }
}

#[derive(Parser, Debug)]
pub enum VitInteractiveCommand {
    // Prints nodes related data, like stats,fragments etc.
    #[clap(subcommand)]
    Show(show::Show),
    /// Sends Explorer queries
    #[clap(subcommand)]
    Explorer(explorer::Explorer),
    /// Exit interactive mode
    Exit,
    /// Prints wallets, nodes which can be used. Draw topology
    #[clap(subcommand)]
    Describe(describe::Describe),
    /// send fragments
    #[clap(subcommand)]
    Send(send::Send),
}