Skip to content

Commit

Permalink
Code Cleanup for noisy warnings
Browse files Browse the repository at this point in the history
Signed-off-by: Harshil Jani <[email protected]>
  • Loading branch information
Harshil-Jani committed Aug 25, 2023
1 parent ac01e60 commit f78417c
Show file tree
Hide file tree
Showing 14 changed files with 19 additions and 1,591 deletions.
65 changes: 0 additions & 65 deletions rust/clients/bridge/src/ble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//! Control functions for the Bluetooth Low Energy Module.
use prost::Message;
use super::rpc::Rpc;

/// include generated protobuf RPC rust definition file
mod proto { include!("../../../libqaul/src/rpc/protobuf_generated/rust/qaul.rpc.ble.rs"); }
Expand All @@ -16,70 +15,6 @@ mod proto_sys { include!("../../../libqaul/src/rpc/protobuf_generated/rust/qaul.
pub struct Ble {}

impl Ble {
/// CLI command interpretation
///
/// The CLI commands of BLE module are processed here
pub fn cli(command: &str) {
match command {
// request BLE device info
cmd if cmd.starts_with("info") => {
// create rpc message
let proto_message = proto::Ble {
message: Some(proto::ble::Message::InfoRequest(
proto::InfoRequest{}
)),
};
// send the message
Self::rpc_send(proto_message);
},
// send start request for BLE module
cmd if cmd.starts_with("start") => {
// create rpc message
let proto_message = proto::Ble {
message: Some(proto::ble::Message::StartRequest(
proto::StartRequest{}
)),
};
// send the message
Self::rpc_send(proto_message);
},
// send stop request for BLE module
cmd if cmd.starts_with("stop") => {
// create rpc message
let proto_message = proto::Ble {
message: Some(proto::ble::Message::StopRequest(
proto::StopRequest{}
)),
};
// send the message
Self::rpc_send(proto_message);
},
// request discovered devices
cmd if cmd.starts_with("discovered") => {
// create rpc message
let proto_message = proto::Ble {
message: Some(proto::ble::Message::DiscoveredRequest(
proto::DiscoveredRequest{}
)),
};
// send the message
Self::rpc_send(proto_message);
},
// unknown command
_ => log::error!("unknown BLE command"),
}
}

/// Send rpc message to libqaul
fn rpc_send(proto_message: proto::Ble) {
// encode message
let mut buf = Vec::with_capacity(proto_message.encoded_len());
proto_message.encode(&mut buf).expect("Vec<u8> provides capacity as needed");

// send message
Rpc::send_message(buf, super::rpc::proto::Modules::Ble.into(), "".to_string());
}

/// Print BLE module information
fn print_info(info: proto::InfoResponse) {
println!("Node small BLE ID: {:?}", info.small_id);
Expand Down
127 changes: 1 addition & 126 deletions rust/clients/bridge/src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use matrix_sdk::{
RoomId,
},
};
use mime::{self, Mime, STAR, STAR_STAR};
use mime::{self, Mime, STAR_STAR};
use prost::Message;
use std::{
collections::HashMap,
Expand All @@ -46,104 +46,8 @@ mod proto_file {

/// chat module function handling
pub struct Chat {}
pub struct QaulMenu {}

impl Chat {
/// CLI command interpretation
///
/// The CLI commands of feed module are processed here
pub fn cli(command: &str) {
match command {
// send chat message
cmd if cmd.starts_with("send ") => {
// get group id
let command_string = cmd.strip_prefix("send ").unwrap().to_string();
let mut iter = command_string.split_whitespace();

if let Some(group_id_str) = iter.next() {
// convert group id from string to binary version
let mut group_id = vec![];
match Self::id_string_to_bin(group_id_str.to_string()) {
Ok(v) => {
group_id = v.clone();
}
_ => match Self::uuid_string_to_bin(group_id_str.to_string()) {
Ok(v) => {
group_id = v.clone();
}
_ => {
log::error!("invalid group id format");
}
},
}
// get message string
if let Some(message) = command_string.strip_prefix(group_id_str) {
// send message
Self::send_chat_message(group_id, message.to_string().trim().to_string());
println!("chat message sent [{}] {}", group_id_str, message);
return;
} else {
log::error!("prefix '{}' not found", group_id_str);
return;
}
} else {
log::error!("chat send command incorrectly formatted");
}
}
// request chat conversation
cmd if cmd.starts_with("conversation") => {
match cmd.strip_prefix("conversation ") {
Some(command_str) => {
let command_string = command_str.to_string();
let mut iter = command_string.split_whitespace();
let mut group_id = Vec::new();
let mut last_index = 0;

// convert group id from string to binary version
if let Some(group_id_str) = iter.next() {
match Self::id_string_to_bin(group_id_str.to_string()) {
Ok(id) => {
group_id = id;
}
Err(_e) => {
match Self::uuid_string_to_bin(group_id_str.to_string()) {
Ok(id) => {
group_id = id;
}
_ => {
log::error!("invalid converstion id");
return;
}
}
}
}
}

// convert last_received index string to number
if let Some(index_str) = iter.next() {
// option: get last_received
if let Ok(index) = index_str.parse::<u64>() {
last_index = index;
} else {
log::error!("chat conversation index is not a valid number");
return;
}
}

// request chat conversation
Self::request_chat_conversation(group_id, last_index);
}
None => {
// request all messages
log::error!("chat conversation command not correctly formatted");
}
}
}
// unknown command
_ => log::error!("unknown chat command"),
}
}

/// Convert Group ID from String to Binary
pub fn id_string_to_bin(id: String) -> Result<Vec<u8>, String> {
// check length
Expand All @@ -169,26 +73,6 @@ impl Chat {
}
}

/// Create and send feed message via rpc
fn send_chat_message(group_id: Vec<u8>, message_text: String) {
// create feed send message
let proto_message = proto::Chat {
message: Some(proto::chat::Message::Send(proto::ChatMessageSend {
group_id,
content: message_text,
})),
};

// encode message
let mut buf = Vec::with_capacity(proto_message.encoded_len());
proto_message
.encode(&mut buf)
.expect("Vec<u8> provides capacity as needed");

// send message
Rpc::send_message(buf, super::rpc::proto::Modules::Chat.into(), "".to_string());
}

/// Request chat conversation via rpc
///
/// This provides all chat messages of a specific conversation.
Expand Down Expand Up @@ -299,7 +183,6 @@ impl Chat {
match chat.message {
Some(proto::chat::Message::ConversationList(proto_conversation)) => {
// Conversation table
let group_id_byte = proto_conversation.clone().group_id;
let group_id =
uuid::Uuid::from_bytes(proto_conversation.group_id.try_into().unwrap());

Expand Down Expand Up @@ -432,14 +315,6 @@ impl Chat {
}
}

impl QaulMenu {
pub fn help(group_id: Vec<u8>) {
let msg = format!("/users : To know all the existing users in matrix room.\n/invite : To invite users into the existing room.");
Chat::send_chat_message(group_id, msg);
//
}
}

fn send_file_to_matrix(file_path: String, room_id: &RoomId, extension: String, file_name: String) {
let path = std::env::current_dir().unwrap();
let mut storage_path = path.as_path().to_str().unwrap().to_string();
Expand Down
122 changes: 0 additions & 122 deletions rust/clients/bridge/src/chatfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use super::rpc::Rpc;
use prost::Message;
use std::fmt;

/// include generated protobuf RPC rust definition file
mod proto {
Expand All @@ -16,104 +15,6 @@ mod proto {
pub struct ChatFile {}

impl ChatFile {
/// CLI command interpretation
///
/// The CLI commands of the chat file module are processed here
pub fn cli(command: &str) {
match command {
// send file
cmd if cmd.starts_with("send ") => {
let command_string = cmd.strip_prefix("send ").unwrap().to_string();
let mut iter = command_string.split_whitespace();

if let Some(group_id_str) = iter.next() {
let group_id;
// convert group id from string to binary version
match Self::id_string_to_bin(group_id_str.to_string()) {
Ok(id) => {
group_id = id.clone();
}
Err(_e) => match Self::uuid_string_to_bin(group_id_str.to_string()) {
Ok(id) => {
group_id = id.clone();
}
_ => {
log::error!("Invalid group id");
return;
}
},
}

if let Some(file_path_name) = iter.next() {
let descr = match iter.next() {
Some(description) => description.to_string(),
_ => "".to_string(),
};

log::trace!(
"send file to group={}, file path={}, description={}",
group_id_str,
file_path_name,
descr
);

Self::send_file(group_id, file_path_name.to_string(), descr);
} else {
log::error!("file pathname is not given");
}
} else {
log::error!("chat send command incorrectly formatted");
}
}

// request chat file history list
cmd if cmd.starts_with("history") => {
let mut offset: i32 = 0;
let mut limit: i32 = 10;

if cmd.starts_with("history ") {
let command_string = cmd.strip_prefix("history ").unwrap().to_string();
let mut iter = command_string.split_whitespace();

if let Some(offset_str) = iter.next() {
offset = offset_str.to_string().parse().unwrap();
if let Some(limit_str) = iter.next() {
limit = limit_str.to_string().parse().unwrap();
}
}
}

Self::send_file_history_command(offset as u32, limit as u32);
}
// unknown command
_ => log::error!("unknown file command"),
}
}

/// Convert Group ID from String to Binary
fn id_string_to_bin(id: String) -> Result<Vec<u8>, String> {
// check length
if id.len() < 52 {
return Err("Group ID not long enough".to_string());
}

// convert input
match bs58::decode(id).into_vec() {
Ok(id_bin) => Ok(id_bin),
Err(e) => {
let err = fmt::format(format_args!("{}", e));
Err(err)
}
}
}

/// Convert Group ID from String to Binary
fn uuid_string_to_bin(id_str: String) -> Result<Vec<u8>, String> {
match uuid::Uuid::parse_str(id_str.as_str()) {
Ok(id) => Ok(id.as_bytes().to_vec()),
_ => Err("invalid group id".to_string()),
}
}

/// send file via rpc
pub fn send_file(group_id: Vec<u8>, file_name: String, description: String) {
Expand Down Expand Up @@ -142,29 +43,6 @@ impl ChatFile {
);
}

/// send file history list command via rpc
fn send_file_history_command(offset: u32, limit: u32) {
// create file history message
let proto_message = proto::ChatFile {
message: Some(proto::chat_file::Message::FileHistory(
proto::FileHistoryRequest { offset, limit },
)),
};

// encode message
let mut buf = Vec::with_capacity(proto_message.encoded_len());
proto_message
.encode(&mut buf)
.expect("Vec<u8> provides capacity as needed");

// send message
Rpc::send_message(
buf,
super::rpc::proto::Modules::Chatfile.into(),
"".to_string(),
);
}

/// Process received RPC message
///
/// Decodes received protobuf encoded binary RPC message
Expand Down
Loading

0 comments on commit f78417c

Please sign in to comment.