-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from matheuswhite/change_to_vec_u8
Changing String to Bytes on appropriate places
- Loading branch information
Showing
7 changed files
with
638 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,191 @@ | ||
use crate::rich_string::RichText; | ||
use crate::text::ViewData; | ||
use chrono::{DateTime, Local}; | ||
use tui::style::Color; | ||
|
||
pub enum UserTxData { | ||
Exit, | ||
Data(String), | ||
Command(String, String), | ||
HexString(Vec<u8>), | ||
PluginSerialTx(String, Vec<u8>), | ||
Data { | ||
content: String, | ||
}, | ||
Command { | ||
command_name: String, | ||
content: String, | ||
}, | ||
HexString { | ||
content: Vec<u8>, | ||
}, | ||
PluginSerialTx { | ||
plugin_name: String, | ||
content: Vec<u8>, | ||
}, | ||
} | ||
|
||
#[derive(Clone)] | ||
pub enum SerialRxData { | ||
Data(DateTime<Local>, String), | ||
ConfirmData(DateTime<Local>, String), | ||
ConfirmCommand(DateTime<Local>, String, String), | ||
ConfirmHexString(DateTime<Local>, Vec<u8>), | ||
Plugin(DateTime<Local>, String, String), | ||
ConfirmPluginSerialTx(DateTime<Local>, String, Vec<u8>), | ||
FailPlugin(DateTime<Local>, String, String), | ||
FailData(DateTime<Local>, String), | ||
FailCommand(DateTime<Local>, String, String), | ||
FailHexString(DateTime<Local>, Vec<u8>), | ||
FailPluginSerialTx(DateTime<Local>, String, Vec<u8>), | ||
RxData { | ||
timestamp: DateTime<Local>, | ||
content: Vec<u8>, | ||
}, | ||
TxData { | ||
timestamp: DateTime<Local>, | ||
content: String, | ||
is_successful: bool, | ||
}, | ||
Command { | ||
timestamp: DateTime<Local>, | ||
command_name: String, | ||
content: String, | ||
is_successful: bool, | ||
}, | ||
HexString { | ||
timestamp: DateTime<Local>, | ||
content: Vec<u8>, | ||
is_successful: bool, | ||
}, | ||
Plugin { | ||
timestamp: DateTime<Local>, | ||
plugin_name: String, | ||
content: String, | ||
is_successful: bool, | ||
}, | ||
PluginSerialTx { | ||
timestamp: DateTime<Local>, | ||
plugin_name: String, | ||
content: Vec<u8>, | ||
is_successful: bool, | ||
}, | ||
} | ||
|
||
impl SerialRxData { | ||
pub fn is_plugin_serial_tx(&self) -> bool { | ||
matches!( | ||
self, | ||
SerialRxData::ConfirmPluginSerialTx(..) | SerialRxData::FailPluginSerialTx(..) | ||
) | ||
matches!(self, SerialRxData::PluginSerialTx { .. }) | ||
} | ||
} | ||
|
||
#[allow(clippy::from_over_into)] | ||
impl Into<ViewData> for SerialRxData { | ||
fn into(self) -> ViewData { | ||
match self { | ||
SerialRxData::Data(timestamp, content) => { | ||
ViewData::new(timestamp, content, Color::Reset, Color::Reset) | ||
SerialRxData::RxData { timestamp, content } => { | ||
RichText::new(content, Color::Reset, Color::Reset) | ||
.decode_ansi_color() | ||
.highlight_invisible() | ||
.into_view_data(timestamp) | ||
} | ||
SerialRxData::ConfirmData(timestamp, content) => { | ||
ViewData::new(timestamp, content, Color::Black, Color::LightCyan) | ||
} | ||
SerialRxData::ConfirmCommand(timestamp, cmd_name, content) => ViewData::new( | ||
timestamp, | ||
format!("</{}> {}", cmd_name, content), | ||
Color::Black, | ||
Color::LightGreen, | ||
), | ||
SerialRxData::ConfirmHexString(timestamp, bytes) => ViewData::new( | ||
timestamp, | ||
format!("{:02x?}", &bytes), | ||
Color::Black, | ||
Color::Yellow, | ||
), | ||
SerialRxData::Plugin(timestamp, plugin_name, message) => ViewData::new( | ||
timestamp, | ||
format!(" [{plugin_name}] {message} "), | ||
Color::Black, | ||
Color::White, | ||
), | ||
SerialRxData::ConfirmPluginSerialTx(timestamp, plugin_name, message) => ViewData::new( | ||
timestamp, | ||
format!(" [{plugin_name}] => {:02x?} ", message), | ||
Color::Black, | ||
Color::White, | ||
), | ||
SerialRxData::FailData(timestamp, content) => ViewData::new( | ||
SerialRxData::TxData { | ||
timestamp, | ||
format!("Cannot send \"{}\"", content), | ||
Color::White, | ||
Color::LightRed, | ||
), | ||
SerialRxData::FailCommand(timestamp, cmd_name, _content) => ViewData::new( | ||
content, | ||
is_successful, | ||
} => { | ||
if is_successful { | ||
RichText::from_string(content, Color::Black, Color::LightCyan) | ||
.highlight_invisible() | ||
.into_view_data(timestamp) | ||
} else { | ||
RichText::from_string( | ||
format!("Cannot send \"{}\"", content), | ||
Color::White, | ||
Color::LightRed, | ||
) | ||
.highlight_invisible() | ||
.into_view_data(timestamp) | ||
} | ||
} | ||
SerialRxData::Command { | ||
timestamp, | ||
format!("Cannot send </{}>", cmd_name), | ||
Color::White, | ||
Color::LightRed, | ||
), | ||
SerialRxData::FailHexString(timestamp, bytes) => ViewData::new( | ||
command_name, | ||
content, | ||
is_successful, | ||
} => { | ||
if is_successful { | ||
RichText::from_string( | ||
format!("</{}> {}", command_name, content), | ||
Color::Black, | ||
Color::LightGreen, | ||
) | ||
.highlight_invisible() | ||
.into_view_data(timestamp) | ||
} else { | ||
RichText::from_string( | ||
format!("Cannot send </{}>", command_name), | ||
Color::White, | ||
Color::LightRed, | ||
) | ||
.highlight_invisible() | ||
.into_view_data(timestamp) | ||
} | ||
} | ||
SerialRxData::HexString { | ||
timestamp, | ||
format!("Cannot send {:02x?}", &bytes), | ||
Color::White, | ||
Color::LightRed, | ||
), | ||
SerialRxData::FailPlugin(timestamp, plugin_name, message) => ViewData::new( | ||
content, | ||
is_successful, | ||
} => { | ||
if is_successful { | ||
RichText::from_string(format!("{:02x?}", &content), Color::Black, Color::Yellow) | ||
.highlight_invisible() | ||
.into_view_data(timestamp) | ||
} else { | ||
RichText::from_string( | ||
format!("Cannot send {:02x?}", &content), | ||
Color::White, | ||
Color::LightRed, | ||
) | ||
.highlight_invisible() | ||
.into_view_data(timestamp) | ||
} | ||
} | ||
SerialRxData::Plugin { | ||
timestamp, | ||
format!(" [{plugin_name}] {message} "), | ||
Color::White, | ||
Color::Red, | ||
), | ||
SerialRxData::FailPluginSerialTx(timestamp, pluging_name, message) => ViewData::new( | ||
plugin_name, | ||
content, | ||
is_successful, | ||
} => { | ||
if is_successful { | ||
RichText::from_string( | ||
format!(" [{plugin_name}] {content} "), | ||
Color::Black, | ||
Color::White, | ||
) | ||
.highlight_invisible() | ||
.into_view_data(timestamp) | ||
} else { | ||
RichText::from_string( | ||
format!(" [{plugin_name}] {content} "), | ||
Color::White, | ||
Color::Red, | ||
) | ||
.highlight_invisible() | ||
.into_view_data(timestamp) | ||
} | ||
} | ||
SerialRxData::PluginSerialTx { | ||
timestamp, | ||
format!(" [{pluging_name}] => Fail to send {:02x?} ", message), | ||
Color::White, | ||
Color::Red, | ||
), | ||
plugin_name, | ||
content, | ||
is_successful, | ||
} => { | ||
if is_successful { | ||
RichText::from_string( | ||
format!(" [{plugin_name}] => {} ", String::from_utf8_lossy(&content)), | ||
Color::Black, | ||
Color::White, | ||
) | ||
.highlight_invisible() | ||
.into_view_data(timestamp) | ||
} else { | ||
RichText::from_string( | ||
format!( | ||
" [{plugin_name}] => Fail to send {} ", | ||
String::from_utf8_lossy(&content) | ||
), | ||
Color::White, | ||
Color::Red, | ||
) | ||
.highlight_invisible() | ||
.into_view_data(timestamp) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.