Merge pull request 'Use rustfmt to format some of the Rust code' (#6) from schlink/libcwtch-rs:formatting into main

Reviewed-on: #6
This commit is contained in:
Dan Ballard 2021-09-17 00:09:40 +00:00
commit 6ccf83c2c7
3 changed files with 54 additions and 25 deletions

View File

@ -3,30 +3,35 @@
#![allow(non_snake_case)] #![allow(non_snake_case)]
#![allow(dead_code)] #![allow(dead_code)]
use std::ffi::{CString}; use std::ffi::CStr;
use std::ffi::{CStr}; use std::ffi::CString;
use super::{CwtchLib}; use super::CwtchLib;
use crate::cwtchlib_go::bindings; use crate::cwtchlib_go::bindings;
struct c_str_wrap { struct c_str_wrap {
raw: *mut i8, raw: *mut i8,
len: i32 len: i32,
} }
impl c_str_wrap { impl c_str_wrap {
pub fn new(str: &str) -> c_str_wrap { pub fn new(str: &str) -> c_str_wrap {
let cs = match CString::new(str) { let cs = match CString::new(str) {
Ok(s) => s, Ok(s) => s,
Err(_) => CString::new("").unwrap() Err(_) => CString::new("").unwrap(),
}; };
c_str_wrap { len: cs.as_bytes().len() as i32, raw: cs.into_raw() } c_str_wrap {
len: cs.as_bytes().len() as i32,
raw: cs.into_raw(),
}
} }
} }
impl Drop for c_str_wrap { impl Drop for c_str_wrap {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { CString::from_raw(self.raw); } unsafe {
CString::from_raw(self.raw);
}
} }
} }
@ -91,7 +96,9 @@ impl CwtchLib for CwtchLibGo {
c_bind!(send_message(profile, contact, msg;) c_SendMessage); c_bind!(send_message(profile, contact, msg;) c_SendMessage);
c_bind!(send_invitation(profile, contact, target;) c_SendInvitation); c_bind!(send_invitation(profile, contact, target;) c_SendInvitation);
fn reset_tor(&self) { fn reset_tor(&self) {
unsafe { bindings::c_ResetTor(); } unsafe {
bindings::c_ResetTor();
}
} }
c_bind!(create_group(profile, server, name;) c_CreateGroup); c_bind!(create_group(profile, server, name;) c_CreateGroup);
c_bind!(delete_profile(profile, pass;) c_DeleteProfile); c_bind!(delete_profile(profile, pass;) c_DeleteProfile);
@ -103,6 +110,8 @@ impl CwtchLib for CwtchLibGo {
c_bind!(set_group_attribute(profile, group, key, val;) c_SetGroupAttribute); c_bind!(set_group_attribute(profile, group, key, val;) c_SetGroupAttribute);
fn shutdown_cwtch(&self) { fn shutdown_cwtch(&self) {
unsafe { bindings::c_ShutdownCwtch(); } unsafe {
bindings::c_ShutdownCwtch();
}
} }
} }

View File

@ -3,8 +3,8 @@
#![doc(html_root_url = "https://git.openprivacy.ca/cwtch.im/libcwtch-rs")] #![doc(html_root_url = "https://git.openprivacy.ca/cwtch.im/libcwtch-rs")]
#![deny(missing_docs)] #![deny(missing_docs)]
mod cwtchlib_go;
mod bindings_go; mod bindings_go;
mod cwtchlib_go;
/// Basic structs using data from Cwtch and for deserializing JSON and serializing to JSON to communicate with Cwtch /// Basic structs using data from Cwtch and for deserializing JSON and serializing to JSON to communicate with Cwtch
pub mod structs; pub mod structs;
@ -18,7 +18,7 @@ pub trait CwtchLib {
fn send_app_event(&self, event_json: &str); fn send_app_event(&self, event_json: &str);
/// Pull json of a structs::CwtchEvent off the appbus for responding to /// Pull json of a structs::CwtchEvent off the appbus for responding to
fn get_appbus_event(&self, ) -> String; fn get_appbus_event(&self) -> String;
/// Create a new profile encrypted with pass /// Create a new profile encrypted with pass
fn create_profile(&self, nick: &str, pass: &str); fn create_profile(&self, nick: &str, pass: &str);
@ -36,7 +36,13 @@ pub trait CwtchLib {
fn block_contact(&self, profile: &str, contact: &str); fn block_contact(&self, profile: &str, contact: &str);
/// Cause profile to update contact's message to have it's flags updated /// Cause profile to update contact's message to have it's flags updated
fn update_message_flags(&self, profile: &str, contact: &str, message_id: i32, message_flags: u64); fn update_message_flags(
&self,
profile: &str,
contact: &str,
message_id: i32,
message_flags: u64,
);
/// Get a specific message for contact of profile by index /// Get a specific message for contact of profile by index
fn get_message(&self, profile: &str, contact: &str, message_index: i32) -> String; fn get_message(&self, profile: &str, contact: &str, message_index: i32) -> String;
@ -51,7 +57,7 @@ pub trait CwtchLib {
fn send_invitation(&self, profile: &str, contact: &str, target: &str); fn send_invitation(&self, profile: &str, contact: &str, target: &str);
/// Ask the ACN inside the Cwtch app to restart the tor connection /// Ask the ACN inside the Cwtch app to restart the tor connection
fn reset_tor(&self, ); fn reset_tor(&self);
/// Cause profile to create a group on server with name /// Cause profile to create a group on server with name
fn create_group(&self, profile: &str, server: &str, name: &str); fn create_group(&self, profile: &str, server: &str, name: &str);
@ -78,10 +84,10 @@ pub trait CwtchLib {
fn set_group_attribute(&self, profile: &str, group: &str, key: &str, val: &str); fn set_group_attribute(&self, profile: &str, group: &str, key: &str, val: &str);
/// Shutdown the cwtch app and associated ACN /// Shutdown the cwtch app and associated ACN
fn shutdown_cwtch(&self, ); fn shutdown_cwtch(&self);
} }
/// Create a new CwtchLib that is backed by bindings to libcwtch-go /// Create a new CwtchLib that is backed by bindings to libcwtch-go
pub fn new_cwtchlib_go() -> impl CwtchLib { pub fn new_cwtchlib_go() -> impl CwtchLib {
bindings_go::CwtchLibGo {} bindings_go::CwtchLibGo {}
} }

View File

@ -1,7 +1,7 @@
use crate::structs::ConnectionState::Disconnected;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DefaultOnError}; use serde_with::{serde_as, DefaultOnError};
use std::collections::HashMap; use std::collections::HashMap;
use crate::structs::ConnectionState::Disconnected;
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
/// Defines the states a Cwtch connection can be in /// Defines the states a Cwtch connection can be in
@ -19,7 +19,7 @@ pub enum ConnectionState {
/// The connection attempt failed /// The connection attempt failed
Failed, Failed,
/// The connection has been killed /// The connection has been killed
Killed Killed,
} }
impl Default for ConnectionState { impl Default for ConnectionState {
@ -37,7 +37,7 @@ pub enum ContactAuthorization {
/// The contact is approved by the user (manual action) /// The contact is approved by the user (manual action)
Approved, Approved,
/// The contact is blocked by the user, should be ignored /// The contact is blocked by the user, should be ignored
Blocked Blocked,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
@ -62,7 +62,8 @@ pub struct Contact {
pub onion: String, pub onion: String,
/// display name of the contact, as determined in libcwtch-go from name specified by contact /// display name of the contact, as determined in libcwtch-go from name specified by contact
pub name: String, pub name: String,
#[serde_as(deserialize_as = "DefaultOnError")] // cwtch loads profile/contacts from storage and leaves status blank, it's filled in "soon" by events... #[serde_as(deserialize_as = "DefaultOnError")]
// cwtch loads profile/contacts from storage and leaves status blank, it's filled in "soon" by events...
/// contact connection status /// contact connection status
pub status: ConnectionState, pub status: ConnectionState,
/// contact authorization state as set by profile /// contact authorization state as set by profile
@ -91,7 +92,7 @@ pub struct Profile {
/// path to a profile image, controled by "picture" attribute /// path to a profile image, controled by "picture" attribute
pub image_path: String, pub image_path: String,
/// all profile attributes /// all profile attributes
pub attr: HashMap<String,String>, pub attr: HashMap<String, String>,
/// map of contacts [ onion => contact ] /// map of contacts [ onion => contact ]
pub contacts: HashMap<String, Contact>, pub contacts: HashMap<String, Contact>,
/// map of servers [ onion => server ] /// map of servers [ onion => server ]
@ -105,17 +106,30 @@ pub struct Message {
/// [ OverlayChat = 1, OverlayInviteContact = 100, OverlayInviteGroup = 101, OverlayFileSharing = 200 ] /// [ OverlayChat = 1, OverlayInviteContact = 100, OverlayInviteGroup = 101, OverlayFileSharing = 200 ]
pub o: i64, pub o: i64,
/// data of the message /// data of the message
pub d: String pub d: String,
} }
impl Profile { impl Profile {
/// Create a new profile populated from supplied data /// Create a new profile populated from supplied data
/// contacts_json as supplied by libcwtch-go, a map of contacts /// contacts_json as supplied by libcwtch-go, a map of contacts
/// server_list as supplied by libcwtch-go, a map of servers /// server_list as supplied by libcwtch-go, a map of servers
pub fn new(identity: &str, name: &str, picture: &str, contacts_json: &str, server_list: &str) -> Profile { pub fn new(
identity: &str,
name: &str,
picture: &str,
contacts_json: &str,
server_list: &str,
) -> Profile {
let contacts = Profile::process_contacts(contacts_json); let contacts = Profile::process_contacts(contacts_json);
let servers = Profile::process_servers(server_list); let servers = Profile::process_servers(server_list);
Profile{ onion: identity.to_string(), nick: name.to_string(), image_path: picture.to_string(), attr: Default::default(), contacts: contacts, servers: servers } Profile {
onion: identity.to_string(),
nick: name.to_string(),
image_path: picture.to_string(),
attr: Default::default(),
contacts: contacts,
servers: servers,
}
} }
fn process_contacts(constacts_json: &str) -> HashMap<String, Contact> { fn process_contacts(constacts_json: &str) -> HashMap<String, Contact> {
@ -142,4 +156,4 @@ impl Profile {
} }
servers servers
} }
} }