diff --git a/examples/echobot.rs b/examples/echobot.rs index 92770b3..185816b 100644 --- a/examples/echobot.rs +++ b/examples/echobot.rs @@ -41,9 +41,7 @@ fn main() { } Event::NewMessageFromPeer { profile_id, conversation_id, contact_id: contact, nick, timestamp_received, message, notification, picture } => { let response = Message { o: message.o.into(), d: message.d }; - let response_json = - serde_json::to_string(&response).expect("Error parsing json response"); - cwtch.send_message( &profile_id, conversation_id, &response_json); + cwtch.send_message( &profile_id, conversation_id, &response); } _ => eprintln!("unhandled event!"), }; diff --git a/src/bindings_go.rs b/src/bindings_go.rs index 10e5141..3efba74 100644 --- a/src/bindings_go.rs +++ b/src/bindings_go.rs @@ -149,9 +149,15 @@ impl CwtchLib for CwtchLibGo { fn get_messages(&self, profile: &ProfileIdentity, conversation_id: ConversationID, message_index: i32, count: i32) -> String { self._get_messages(String::from(profile).as_str(), conversation_id.into(), message_index, count) } - fn send_message(&self, profile: &ProfileIdentity, conversation_id: ConversationID, msg: &str) -> String { + fn send_message_raw(&self, profile: &ProfileIdentity, conversation_id: ConversationID, msg: &str) -> String { self._send_message(String::from(profile).as_str(), conversation_id.into(), msg) } + fn send_message(&self, profile: &ProfileIdentity, conversation_id: ConversationID, message: &Message) -> Result { + match serde_json::to_string(&message) { + Ok(message_json) => Ok(self._send_message(&String::from(profile), conversation_id.into(), &message_json)), + Err(e) => Err(format!("Error parsing json response: {}", e.to_string())) + } + } fn send_invitation(&self, profile: &ProfileIdentity, conversation_id: ConversationID, target_id: i32) -> String { self._send_invitation(String::from(profile).as_str(), conversation_id.into(), target_id) } diff --git a/src/lib.rs b/src/lib.rs index ee92cc5..458804c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ #![deny(missing_docs)] use crate::event::{ConversationID, Event, FileKey, ProfileIdentity, ServerIdentity}; +use crate::structs::Message; mod bindings_go; mod cwtchlib_go; @@ -66,9 +67,12 @@ pub trait CwtchLib { fn get_messages(&self, profile: &ProfileIdentity, conversation_id: ConversationID, message_index: i32, count: i32) -> String; /// Send json of a structs::Message from profile to contact. Returns computed sent message (including index and hash values) - fn send_message(&self, profile: &ProfileIdentity, conversation_id: ConversationID, msg: &str) -> String; + fn send_message_raw(&self, profile: &ProfileIdentity, conversation_id: ConversationID, msg: &str) -> String; - /// Send profile's contact an invite for/to target. Returns computed sent message (including index and hash values) + /// Send structs::Message from profile to contact. Returns computed sent message (including index and hash values) or Error + fn send_message(&self, profile: &ProfileIdentity, conversation_id: ConversationID, message: &Message) -> Result; + + /// Send profile's contact an invite for/to target. Returns computed sent message (including index and hash values) fn send_invitation(&self, profile: &ProfileIdentity, conversation_id: ConversationID, target_id: i32) -> String; /// share a file file_path with a conersation. Returns computed sent message (including index and hash values)