From b96d6d8db3856bf4056a9092185fb9ffbb69a6af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Sun, 25 Jun 2023 10:15:24 +0200 Subject: [PATCH] refactor: remove body of specific commands, do not expose fields of structs publicly... --- src/commands.rs | 3 ++- src/commands/all_command.rs | 28 ++++++++++-------------- src/commands/command.rs | 20 +++++++++++++++-- src/commands/command_argument.rs | 2 -- src/commands/command_handler.rs | 33 ++++++++--------------------- src/commands/hello_world_command.rs | 22 +++++++------------ src/commands/reset_command.rs | 17 ++++++--------- src/commands/set_command.rs | 27 ++++++++++------------- src/main.rs | 15 ++++++------- 9 files changed, 72 insertions(+), 95 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index faf438d792c92e64c6d4c36ff2f21fb2bb0d7fec..0d2f130d61924971312b0bb296c8de9a8e1e972e 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -4,4 +4,5 @@ pub mod command; pub mod command_argument; pub mod set_command; pub mod reset_command; -pub mod all_command; \ No newline at end of file +pub mod all_command; +pub mod command_data; \ No newline at end of file diff --git a/src/commands/all_command.rs b/src/commands/all_command.rs index 4551074283db331bbc4b3b2ac7cfac83308ff44b..ec4ea3f1a84387f68609144ff57f8179c3b6ef67 100644 --- a/src/commands/all_command.rs +++ b/src/commands/all_command.rs @@ -1,29 +1,23 @@ use esp_println::println; -use crate::commands::command_handler::{CommandData, CommandHandleError, SpecificCommandHandler}; +use crate::commands::command_handler::{CommandHandleError, SpecificCommandHandler}; use crate::commands::command_handler::CommandHandleError::WrongArguments; +use crate::commands::command_data::CommandData; -pub struct AllCommand { -} - -impl AllCommand { - pub fn new() -> Self { - Self {} - } -} +#[derive(Default)] +pub struct AllCommand; impl SpecificCommandHandler for AllCommand { - fn handle(&self, command: &mut CommandData) -> Result<(), CommandHandleError> { - let cmd = &command.command; - let map = &mut command.map; + fn handle(&self, command: CommandData) -> Result<(), CommandHandleError> { + let (cmd, map) = command.deconstruct(); - if cmd.parsed_arguments.len() < 4 { + if cmd.parsed_arguments().len() < 4 { println!("Less than 4 args."); return Err(WrongArguments); } - let r = cmd.parsed_arguments[1].try_to_integer(); - let g = cmd.parsed_arguments[2].try_to_integer(); - let b = cmd.parsed_arguments[3].try_to_integer(); + let r = cmd.parsed_arguments()[1].try_to_integer(); + let g = cmd.parsed_arguments()[2].try_to_integer(); + let b = cmd.parsed_arguments()[3].try_to_integer(); if r.is_none() || g.is_none() || b.is_none() { println!("Cold not parse r, g, b."); @@ -38,7 +32,7 @@ impl SpecificCommandHandler for AllCommand { return Err(WrongArguments); } - for led in command.map.get_map_mut() { + for led in map.get_map_mut() { led.r = r as u8; led.g = g as u8; led.b = b as u8; diff --git a/src/commands/command.rs b/src/commands/command.rs index c86f38bafd67d80a650c51db3415ca6f586139ee..dbb50d854b7d7515d5fca61506b07a433f8ea436 100644 --- a/src/commands/command.rs +++ b/src/commands/command.rs @@ -2,6 +2,22 @@ use crate::commands::command_argument::CommandArgument; pub struct Command<'d> { - pub full: &'d [char], - pub parsed_arguments: &'d [CommandArgument<'d>], + full: &'d [char], + parsed_arguments: &'d [CommandArgument<'d>], +} + +impl<'d> Command<'d> { + pub fn new(full: &'d [char], parsed_arguments: &'d [CommandArgument<'d>]) -> Self { + Self { + full, + parsed_arguments + } + } + pub fn full(&self) -> &'d [char] { + self.full + } + + pub fn parsed_arguments(&self) -> &'d [CommandArgument<'d>] { + self.parsed_arguments + } } \ No newline at end of file diff --git a/src/commands/command_argument.rs b/src/commands/command_argument.rs index 53778c3c2c267f6913b082730af7f55a3ac3025d..59d5ba2391393ee98b3da015f679d47dd24e1820 100644 --- a/src/commands/command_argument.rs +++ b/src/commands/command_argument.rs @@ -1,5 +1,3 @@ -use esp_println::{print, println}; - #[derive(Clone, Copy, Eq, PartialEq)] pub struct CommandArgument<'d> { diff --git a/src/commands/command_handler.rs b/src/commands/command_handler.rs index 3e42b19bcbb8ca7e0211940f58b927f1822d8dbe..4d618ebbb9465cf1f6cd8a4c02ce880edfbf71de 100644 --- a/src/commands/command_handler.rs +++ b/src/commands/command_handler.rs @@ -1,24 +1,16 @@ -use core::slice::IterMut; use embedded_hal::serial::{Read, Write}; -use esp_println::{print, println}; +use esp_println::println; use nb::block; use nb::Error::{Other, WouldBlock}; -use crate::command_handler::CommandHandleError::{CommandNotRead, NotFound}; -use crate::command_handler::CommandReadError::{BufferOverflowed, CommandLoadedAlready, UnexpectedEndOfLine}; -use crate::commands::command::Command; -use crate::commands::command_argument::CommandArgument; +use crate::command_handler::{CommandHandleError::{CommandNotRead, NotFound}, CommandReadError::{BufferOverflowed, CommandLoadedAlready, UnexpectedEndOfLine}}; +use crate::commands::{command::Command, command_argument::CommandArgument, command_data::CommandData}; use crate::map::Map; pub trait SpecificCommandHandler { - fn handle(&self, command: &mut CommandData) -> Result<(), CommandHandleError>; + fn handle(&self, command: CommandData) -> Result<(), CommandHandleError>; fn help(&self) -> &'static str; } -pub struct CommandData<'d, 'a> { - pub command: Command<'d>, - pub map: &'d mut Map<'a>, -} - pub struct CommandHandler<'d, const BUFFER_SIZE: usize, const HANDLERS_COUNT: usize> { buffer_position: usize, command_loaded: bool, @@ -158,10 +150,7 @@ impl<'d, const BUFFER_SIZE: usize, const HANDLERS_COUNT: usize> CommandHandler<' } } - Command { - full: buffer, - parsed_arguments: &args[0..length], - } + Command::new(buffer, &args[0..length]) } fn handle_help(&self) -> Result<(), CommandHandleError> @@ -185,12 +174,12 @@ impl<'d, const BUFFER_SIZE: usize, const HANDLERS_COUNT: usize> CommandHandler<' let command = self.parse_command(buffer, &mut args); - if command.parsed_arguments.len() == 0 { + if command.parsed_arguments().len() == 0 { self.reset(); return Err(NotFound); } - let first_argument = command.parsed_arguments[0]; + let first_argument = command.parsed_arguments()[0]; if first_argument.compare("HELP") { let help_handled = self.handle_help(); @@ -203,12 +192,8 @@ impl<'d, const BUFFER_SIZE: usize, const HANDLERS_COUNT: usize> CommandHandler<' continue; } - let mut command_data = CommandData { - command, - map, - }; - - let handled = handler.handle(&mut command_data); + let command_data = CommandData::new(&command, map); + let handled = handler.handle(command_data); self.reset(); return handled; } diff --git a/src/commands/hello_world_command.rs b/src/commands/hello_world_command.rs index 1842a49d51233a4a5a69f7f2741917a17ee0817c..140890b0fcdbfbca28a4146eb5f3d2290b7e3c33 100644 --- a/src/commands/hello_world_command.rs +++ b/src/commands/hello_world_command.rs @@ -1,24 +1,18 @@ -use embedded_hal::serial::{Read, Write}; use esp_println::{print, println}; -use crate::command_handler::{CommandData, CommandHandleError, SpecificCommandHandler}; -use crate::map::Map; +use crate::command_handler::{CommandHandleError, SpecificCommandHandler}; +use crate::commands::command_data::CommandData; -pub struct HelloWorldCommand { - -} - -impl HelloWorldCommand { - pub fn new() -> Self { - Self {} - } -} +#[derive(Default)] +pub struct HelloWorldCommand; impl SpecificCommandHandler for HelloWorldCommand { - fn handle(&self, command: &mut CommandData) -> Result<(), CommandHandleError> + fn handle(&self, command: CommandData) -> Result<(), CommandHandleError> { + let command = command.command(); + let full = command.full(); print!("Hello world!"); - for c in &command.command.full[command.command.parsed_arguments[0].data.len()..] { + for c in &full[command.parsed_arguments()[0].data.len()..] { print!("{}", c); } println!("\r"); diff --git a/src/commands/reset_command.rs b/src/commands/reset_command.rs index 7f7d9311c52c80e209481bf51f0dc6476efba33c..93ecbb18dd8148690f66a7e1a01feaba69f1f425 100644 --- a/src/commands/reset_command.rs +++ b/src/commands/reset_command.rs @@ -1,17 +1,12 @@ -use crate::commands::command_handler::{CommandData, CommandHandleError, SpecificCommandHandler}; -use crate::commands::command_handler::CommandHandleError::WrongArguments; +use crate::commands::command_handler::{CommandHandleError, SpecificCommandHandler}; +use crate::commands::command_data::CommandData; -pub struct ResetCommand {} - -impl ResetCommand { - pub fn new() -> Self { - Self {} - } -} +#[derive(Default)] +pub struct ResetCommand; impl SpecificCommandHandler for ResetCommand { - fn handle(&self, command: &mut CommandData) -> Result<(), CommandHandleError> { - for led in command.map.get_map_mut() { + fn handle(&self, command: CommandData) -> Result<(), CommandHandleError> { + for led in command.map().get_map_mut() { led.r = 0; led.g = 0; led.b = 0; diff --git a/src/commands/set_command.rs b/src/commands/set_command.rs index d54d78c16d5d8988617aebb09a935959e083fd7c..54e52e3722b2949098523488bdfe675c3bb088c4 100644 --- a/src/commands/set_command.rs +++ b/src/commands/set_command.rs @@ -1,26 +1,21 @@ use esp_println::println; -use crate::commands::command_handler::{CommandData, CommandHandleError, SpecificCommandHandler}; +use crate::commands::command_handler::{CommandHandleError, SpecificCommandHandler}; use crate::commands::command_handler::CommandHandleError::WrongArguments; +use crate::commands::command_data::CommandData; -pub struct SetCommand {} - -impl SetCommand { - pub fn new() -> Self { - Self {} - } -} +#[derive(Default)] +pub struct SetCommand; impl SpecificCommandHandler for SetCommand { - fn handle(&self, command: &mut CommandData) -> Result<(), CommandHandleError> { - let cmd = &command.command; - let map = &mut command.map; + fn handle(&self, command: CommandData) -> Result<(), CommandHandleError> { + let (cmd, map) = command.deconstruct(); - if cmd.parsed_arguments.len() < 5 { + if cmd.parsed_arguments().len() < 5 { println!("Less than 5 args."); return Err(WrongArguments); } - let led_id = cmd.parsed_arguments[1]; + let led_id = cmd.parsed_arguments()[1]; let led_id = if let Some(id) = led_id.try_to_integer() { Some(id as usize) } else { @@ -32,9 +27,9 @@ impl SpecificCommandHandler for SetCommand { return Err(WrongArguments); } - let r = cmd.parsed_arguments[2].try_to_integer(); - let g = cmd.parsed_arguments[3].try_to_integer(); - let b = cmd.parsed_arguments[4].try_to_integer(); + let r = cmd.parsed_arguments()[2].try_to_integer(); + let g = cmd.parsed_arguments()[3].try_to_integer(); + let b = cmd.parsed_arguments()[4].try_to_integer(); if r.is_none() || g.is_none() || b.is_none() { println!("Cold not parse r, g, b."); diff --git a/src/main.rs b/src/main.rs index 9db640673c865e4a9fbc97e4e63a449f9845ae72..227ce825880273d9718443bf5896369bbeb692f0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,14 +5,14 @@ mod strip; mod map; mod commands; -use embedded_hal::serial::{Read, Write}; +use embedded_hal::serial::{Write}; use esp_backtrace as _; use esp_println::println; -use hal::{clock::ClockControl, pulse_control::{ClockSource}, peripherals::Peripherals, prelude::*, timer::{TimerGroup, Timer, Timer0}, Rtc, IO, Delay, interrupt, PulseControl, Uart}; +use hal::{clock::ClockControl, peripherals::Peripherals, prelude::*, timer::{TimerGroup}, Rtc, IO, Delay, PulseControl, Uart}; use hal::uart::config::{Config, DataBits, Parity, StopBits}; use hal::uart::TxRxPins; use nb::block; -use nb::Error::{WouldBlock, Other}; +use nb::Error::{Other}; use smart_leds::{RGB8, SmartLedsWrite}; use crate::commands::all_command::AllCommand; use crate::commands::command_handler::{CommandHandler}; @@ -20,7 +20,6 @@ use crate::commands::command_handler; use crate::commands::hello_world_command::HelloWorldCommand; use crate::commands::reset_command::ResetCommand; use crate::commands::set_command::SetCommand; -use crate::map::Map; use crate::strip::StripTiming; const LEDS_COUNT: usize = 72; @@ -93,10 +92,10 @@ fn main() -> ! { let mut rgb_data: [RGB8; 72] = [RGB8 { r: 0, g: 0, b: 0 }; 72]; let mut map = map::Map::new(&map::INDEX_MAP, &mut rgb_data); - let world_command = HelloWorldCommand::new(); - let set_command = SetCommand::new(); - let reset_command = ResetCommand::new(); - let all_command = AllCommand::new(); + let world_command = HelloWorldCommand::default(); + let set_command = SetCommand::default(); + let reset_command = ResetCommand::default(); + let all_command = AllCommand::default(); let mut handler = CommandHandler::new( [ ("HELLO_WORLD", &world_command),