Skip to content

Commit f748463

Browse files
refactor: rename 'handler' to 'printer'
1 parent 3e0c4ac commit f748463

2 files changed

Lines changed: 44 additions & 44 deletions

File tree

src/cli/response_handling.rs

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,50 @@
1+
use colored::Colorize;
12
use rw3d_core::packet::WitcherPacket;
23
use rw3d_core::utils::{scripts_execute_formatter, scripts_root_path_formatter, mod_list_formatter, opcode_formatter, var_list_formatter, scripts_reload_formatter, scripts_reload_response_type, ScriptsReloadResponseType};
34

45

56

67
pub(crate) trait HandleResponse {
7-
fn handle(&mut self, response: WitcherPacket, verbose_print: bool);
8-
fn should_exit(&self) -> bool;
8+
fn handle_response(&mut self, response: WitcherPacket, verbose_print: bool);
9+
fn is_done(&self) -> bool;
910
}
1011

1112

1213

13-
pub(crate) struct ScriptsReloadHandler {
14+
pub(crate) struct ScriptsReloadPrinter {
1415
has_finished: bool,
1516
warnings: Vec<String>,
1617
errors: Vec<String>
1718
}
1819

19-
impl ScriptsReloadHandler {
20+
impl ScriptsReloadPrinter {
2021
fn print_summary(&self) {
21-
println!("{} Errors, {} Warnings\n", self.errors.len(), self.warnings.len());
22+
println!("========== {} Errors, {} Warnings ==========\n", self.errors.len(), self.warnings.len());
2223

2324
for e in &self.errors {
24-
println!("{}", e);
25+
println!("{}", e.red());
2526
}
2627

2728
println!(""); // empty line between errors and warnings
2829

2930
for w in &self.warnings {
30-
println!("{}", w);
31+
println!("{}", w.yellow());
3132
}
3233
}
3334
}
3435

35-
impl Default for ScriptsReloadHandler {
36+
impl Default for ScriptsReloadPrinter {
3637
fn default() -> Self {
37-
ScriptsReloadHandler {
38+
ScriptsReloadPrinter {
3839
has_finished: false,
3940
warnings: Vec::new(),
4041
errors: Vec::new(),
4142
}
4243
}
4344
}
4445

45-
impl HandleResponse for ScriptsReloadHandler {
46-
fn handle(&mut self, response: WitcherPacket, verbose_print: bool) {
46+
impl HandleResponse for ScriptsReloadPrinter {
47+
fn handle_response(&mut self, response: WitcherPacket, verbose_print: bool) {
4748
let msg = scripts_reload_formatter(&response);
4849

4950
if let Ok(response_type) = scripts_reload_response_type(&response) {
@@ -75,97 +76,97 @@ impl HandleResponse for ScriptsReloadHandler {
7576
}
7677
}
7778

78-
fn should_exit(&self) -> bool {
79+
fn is_done(&self) -> bool {
7980
self.has_finished
8081
}
8182
}
8283

8384

8485

85-
pub(crate) struct ScriptsExecuteHandler();
86+
pub(crate) struct ScriptsExecutePrinter();
8687

87-
impl HandleResponse for ScriptsExecuteHandler {
88-
fn handle(&mut self, response: WitcherPacket, verbose_print: bool) {
88+
impl HandleResponse for ScriptsExecutePrinter {
89+
fn handle_response(&mut self, response: WitcherPacket, verbose_print: bool) {
8990
if verbose_print {
9091
println!("{:?}", response);
9192
} else {
9293
println!("{}", scripts_execute_formatter(&response));
9394
}
9495
}
9596

96-
fn should_exit(&self) -> bool {
97+
fn is_done(&self) -> bool {
9798
true // only one packet
9899
}
99100
}
100101

101102

102103

103-
pub(crate) struct ScriptsRootpathHandler();
104+
pub(crate) struct ScriptsRootpathPrinter();
104105

105-
impl HandleResponse for ScriptsRootpathHandler {
106-
fn handle(&mut self, response: WitcherPacket, verbose_print: bool) {
106+
impl HandleResponse for ScriptsRootpathPrinter {
107+
fn handle_response(&mut self, response: WitcherPacket, verbose_print: bool) {
107108
if verbose_print {
108109
println!("{:?}", response);
109110
} else {
110111
println!("{}", scripts_root_path_formatter(&response));
111112
}
112113
}
113114

114-
fn should_exit(&self) -> bool {
115+
fn is_done(&self) -> bool {
115116
true // only one packet
116117
}
117118
}
118119

119120

120121

121-
pub(crate) struct ModlistHandler();
122+
pub(crate) struct ModlistPrinter();
122123

123-
impl HandleResponse for ModlistHandler {
124-
fn handle(&mut self, response: WitcherPacket, verbose_print: bool) {
124+
impl HandleResponse for ModlistPrinter {
125+
fn handle_response(&mut self, response: WitcherPacket, verbose_print: bool) {
125126
if verbose_print {
126127
println!("{:?}", response);
127128
} else {
128129
println!("{}", mod_list_formatter(&response));
129130
}
130131
}
131132

132-
fn should_exit(&self) -> bool {
133+
fn is_done(&self) -> bool {
133134
true // only one packet
134135
}
135136
}
136137

137138

138139

139-
pub(crate) struct OpcodeHandler();
140+
pub(crate) struct OpcodePrinter();
140141

141-
impl HandleResponse for OpcodeHandler {
142-
fn handle(&mut self, response: WitcherPacket, verbose_print: bool) {
142+
impl HandleResponse for OpcodePrinter {
143+
fn handle_response(&mut self, response: WitcherPacket, verbose_print: bool) {
143144
if verbose_print {
144145
println!("{:?}", response);
145146
} else {
146147
println!("{}", opcode_formatter(&response));
147148
}
148149
}
149150

150-
fn should_exit(&self) -> bool {
151+
fn is_done(&self) -> bool {
151152
true // only one packet
152153
}
153154
}
154155

155156

156157

157-
pub(crate) struct VarlistHandler();
158+
pub(crate) struct VarlistPrinter();
158159

159-
impl HandleResponse for VarlistHandler {
160-
fn handle(&mut self, response: WitcherPacket, verbose_print: bool) {
160+
impl HandleResponse for VarlistPrinter {
161+
fn handle_response(&mut self, response: WitcherPacket, verbose_print: bool) {
161162
if verbose_print {
162163
println!("{:?}", response);
163164
} else {
164165
println!("{}", var_list_formatter(&response));
165166
}
166167
}
167168

168-
fn should_exit(&self) -> bool {
169+
fn is_done(&self) -> bool {
169170
true // only one packet
170171
}
171172
}

src/cli/server_subcommands.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{thread, net::{Shutdown, TcpStream}, sync::mpsc::{Receiver, TryRecvErro
22

33
use clap::Subcommand;
44

5-
use crate::{input_waiter::input_waiter, CliOptions, response_handling::{HandleResponse, ScriptsReloadHandler, ScriptsExecuteHandler, ScriptsRootpathHandler, ModlistHandler, OpcodeHandler, VarlistHandler}};
5+
use crate::{input_waiter::input_waiter, CliOptions, response_handling::{HandleResponse, ScriptsReloadPrinter, ScriptsExecutePrinter, ScriptsRootpathPrinter, ModlistPrinter, OpcodePrinter, VarlistPrinter}};
66

77
/// Subcommands that require connection to game's socket and sending messages to it
88
#[derive(Subcommand)]
@@ -74,33 +74,32 @@ pub(crate) fn handle_server_subcommand( cmd: ServerSubcommands, options: CliOpti
7474
if !options.no_wait { thread::sleep( Duration::from_millis(1000) ) }
7575
println!("Handling the command...");
7676

77-
let handler: Box<dyn HandleResponse>;
77+
let response_handler: Box<dyn HandleResponse>;
7878
let p = match cmd {
7979
ServerSubcommands::Reload => {
80-
//TODO maybe colored text for errors
81-
handler = Box::new(ScriptsReloadHandler::default());
80+
response_handler = Box::new(ScriptsReloadPrinter::default());
8281
rw3d_core::commands::scripts_reload()
8382
}
8483
ServerSubcommands::Exec { cmd } => {
85-
handler = Box::new(ScriptsExecuteHandler());
84+
response_handler = Box::new(ScriptsExecutePrinter());
8685
rw3d_core::commands::scripts_execute(cmd)
8786
}
8887
ServerSubcommands::Rootpath => {
89-
handler = Box::new(ScriptsRootpathHandler());
88+
response_handler = Box::new(ScriptsRootpathPrinter());
9089
rw3d_core::commands::scripts_root_path()
9190
}
9291
ServerSubcommands::Modlist => {
9392
//TODO would be nice to have these mod actually sorted alphabetically at least
94-
handler = Box::new(ModlistHandler());
93+
response_handler = Box::new(ModlistPrinter());
9594
rw3d_core::commands::mod_list()
9695
}
9796
ServerSubcommands::Opcode { func_name, class_name } => {
98-
handler = Box::new(OpcodeHandler());
97+
response_handler = Box::new(OpcodePrinter());
9998
rw3d_core::commands::opcode(func_name, class_name)
10099
}
101100
ServerSubcommands::Varlist { section, name } => {
102101
//TODO would be nice to have some option to sort those values
103-
handler = Box::new(VarlistHandler());
102+
response_handler = Box::new(VarlistPrinter());
104103
rw3d_core::commands::var_list(section, name)
105104
}
106105
// ServerSubcommands::Varset { section, name, value } => {
@@ -129,7 +128,7 @@ pub(crate) fn handle_server_subcommand( cmd: ServerSubcommands, options: CliOpti
129128

130129
// This function can either finish by itself by the means of response timeout
131130
// or be stopped by input waiter thread if that one sends him a signal
132-
read_responses(&mut stream, options.response_timeout, reader_rcv, options.verbose, handler);
131+
read_responses(&mut stream, options.response_timeout, reader_rcv, options.verbose, response_handler);
133132

134133
} else {
135134
// Wait a little bit to not finish the connection abruptly
@@ -202,9 +201,9 @@ fn read_responses(stream: &mut TcpStream, response_timeout: i64, cancel_token: R
202201
if packet_available {
203202
match rw3d_core::packet::WitcherPacket::from_stream(stream) {
204203
Ok(packet) => {
205-
handler.handle(packet, verbose_print);
204+
handler.handle_response(packet, verbose_print);
206205

207-
if handler.should_exit() {
206+
if handler.is_done() {
208207
break;
209208
}
210209
}

0 commit comments

Comments
 (0)