Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

feat: update cargo deps, and rustfmt all the rust #1021

Merged
merged 1 commit into from
Sep 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 40 additions & 40 deletions autopush_rs/Cargo.lock

Large diffs are not rendered by default.

179 changes: 92 additions & 87 deletions autopush_rs/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,27 @@ pub struct PythonCall {
}

#[no_mangle]
pub extern "C" fn autopush_python_call_input_ptr(call: *mut AutopushPythonCall,
err: &mut AutopushError)
-> *const u8
{
unsafe {
(*call).inner.catch(err, |call| {
call.input.as_ptr()
})
}
pub extern "C" fn autopush_python_call_input_ptr(
call: *mut AutopushPythonCall,
err: &mut AutopushError,
) -> *const u8 {
unsafe { (*call).inner.catch(err, |call| call.input.as_ptr()) }
}

#[no_mangle]
pub extern "C" fn autopush_python_call_input_len(call: *mut AutopushPythonCall,
err: &mut AutopushError)
-> usize
{
unsafe {
(*call).inner.catch(err, |call| {
call.input.len()
})
}
pub extern "C" fn autopush_python_call_input_len(
call: *mut AutopushPythonCall,
err: &mut AutopushError,
) -> usize {
unsafe { (*call).inner.catch(err, |call| call.input.len()) }
}

#[no_mangle]
pub extern "C" fn autopush_python_call_complete(call: *mut AutopushPythonCall,
input: *const c_char,
err: &mut AutopushError)
-> i32
{
pub extern "C" fn autopush_python_call_complete(
call: *mut AutopushPythonCall,
input: *const c_char,
err: &mut AutopushError,
) -> i32 {
unsafe {
(*call).inner.catch(err, |call| {
let input = CStr::from_ptr(input).to_str().unwrap();
Expand All @@ -100,7 +92,8 @@ impl AutopushPythonCall {
}

fn _new<F>(input: String, f: F) -> AutopushPythonCall
where F: FnOnce(&str) + Send + 'static,
where
F: FnOnce(&str) + Send + 'static,
{
AutopushPythonCall {
inner: UnwindGuard::new(Inner {
Expand Down Expand Up @@ -162,19 +155,14 @@ enum Call {
timestamp: i64,
},

DropUser {
uaid: String,
},
DropUser { uaid: String },

MigrateUser {
uaid: String,
message_month: String,
},
MigrateUser { uaid: String, message_month: String },

StoreMessages {
message_month: String,
messages: Vec<protocol::Notification>,
}
},
}

#[derive(Deserialize)]
Expand All @@ -195,29 +183,25 @@ pub struct HelloResponse {
#[derive(Deserialize)]
#[serde(untagged)]
pub enum RegisterResponse {
Success {
endpoint: String,
},
Success { endpoint: String },

Error {
error_msg: String,
error: bool,
status: u32,
}
},
}

#[derive(Deserialize)]
#[serde(untagged)]
pub enum UnRegisterResponse {
Success {
success: bool,
},
Success { success: bool },

Error {
error_msg: String,
error: bool,
status: u32,
}
},
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -254,100 +238,124 @@ pub struct StoreMessagesResponse {


impl Server {
pub fn hello(&self, connected_at: &u64, uaid: Option<&Uuid>)
-> MyFuture<HelloResponse>
{
pub fn hello(&self, connected_at: &u64, uaid: Option<&Uuid>) -> MyFuture<HelloResponse> {
let ms = *connected_at as i64;
let (call, fut) = PythonCall::new(&Call::Hello {
connected_at: ms,
uaid: if let Some(uuid) = uaid { Some(uuid.simple().to_string()) } else { None },
uaid: if let Some(uuid) = uaid {
Some(uuid.simple().to_string())
} else {
None
},
});
self.send_to_python(call);
return fut
return fut;
}

pub fn register(&self, uaid: String, message_month: String, channel_id: String, key: Option<String>)
-> MyFuture<RegisterResponse>
{
pub fn register(
&self,
uaid: String,
message_month: String,
channel_id: String,
key: Option<String>,
) -> MyFuture<RegisterResponse> {
let (call, fut) = PythonCall::new(&Call::Register {
uaid: uaid,
message_month: message_month,
channel_id: channel_id,
key: key,
});
self.send_to_python(call);
return fut
return fut;
}

pub fn unregister(&self, uaid: String, message_month: String, channel_id: String, code: i32)
-> MyFuture<UnRegisterResponse>
{
pub fn unregister(
&self,
uaid: String,
message_month: String,
channel_id: String,
code: i32,
) -> MyFuture<UnRegisterResponse> {
let (call, fut) = PythonCall::new(&Call::Unregister {
uaid: uaid,
message_month: message_month,
channel_id: channel_id,
code: code,
});
self.send_to_python(call);
return fut
return fut;
}

pub fn check_storage(&self, uaid: String, message_month: String, include_topic: bool, timestamp: Option<i64>)
-> MyFuture<CheckStorageResponse>
{
pub fn check_storage(
&self,
uaid: String,
message_month: String,
include_topic: bool,
timestamp: Option<i64>,
) -> MyFuture<CheckStorageResponse> {
let (call, fut) = PythonCall::new(&Call::CheckStorage {
uaid: uaid,
message_month: message_month,
include_topic: include_topic,
timestamp: timestamp,
});
self.send_to_python(call);
return fut
return fut;
}

pub fn increment_storage(&self, uaid: String, message_month: String, timestamp: i64)
-> MyFuture<IncStorageResponse>
{
pub fn increment_storage(
&self,
uaid: String,
message_month: String,
timestamp: i64,
) -> MyFuture<IncStorageResponse> {
let (call, fut) = PythonCall::new(&Call::IncStoragePosition {
uaid: uaid,
message_month: message_month,
timestamp: timestamp,
});
self.send_to_python(call);
return fut
return fut;
}

pub fn delete_message(&self, message_month: String, notif: protocol::Notification)
-> MyFuture<DeleteMessageResponse>
{
pub fn delete_message(
&self,
message_month: String,
notif: protocol::Notification,
) -> MyFuture<DeleteMessageResponse> {
let (call, fut) = PythonCall::new(&Call::DeleteMessage {
message: notif,
message_month: message_month,
});
self.send_to_python(call);
return fut
return fut;
}

pub fn drop_user(&self, uaid: String) -> MyFuture<DropUserResponse> {
let (call, fut) = PythonCall::new(&Call::DropUser {
uaid,
});
let (call, fut) = PythonCall::new(&Call::DropUser { uaid });
self.send_to_python(call);
return fut
return fut;
}

pub fn migrate_user(&self, uaid: String, message_month: String) -> MyFuture<MigrateUserResponse> {
pub fn migrate_user(
&self,
uaid: String,
message_month: String,
) -> MyFuture<MigrateUserResponse> {
let (call, fut) = PythonCall::new(&Call::MigrateUser {
uaid,
message_month,
});
self.send_to_python(call);
return fut
return fut;
}

pub fn store_messages(&self, uaid: String, message_month: String, mut messages: Vec<protocol::Notification>)
-> MyFuture<StoreMessagesResponse>
{
pub fn store_messages(
&self,
uaid: String,
message_month: String,
mut messages: Vec<protocol::Notification>,
) -> MyFuture<StoreMessagesResponse> {
for message in messages.iter_mut() {
message.uaid = Some(uaid.clone());
}
Expand All @@ -356,7 +364,7 @@ impl Server {
messages,
});
self.send_to_python(call);
return fut
return fut;
}

fn send_to_python(&self, call: PythonCall) {
Expand All @@ -366,22 +374,19 @@ impl Server {

impl PythonCall {
fn new<T, U>(input: &T) -> (PythonCall, MyFuture<U>)
where T: ser::Serialize,
U: for<'de> de::Deserialize<'de> + 'static,
where
T: ser::Serialize,
U: for<'de> de::Deserialize<'de> + 'static,
{
let (tx, rx) = oneshot::channel();
let call = PythonCall {
input: serde_json::to_string(input).unwrap(),
output: Box::new(|json: &str| {
drop(tx.send(json_or_error(json)));
}),
output: Box::new(|json: &str| { drop(tx.send(json_or_error(json))); }),
};
let rx = Box::new(rx.then(|res| {
match res {
Ok(Ok(s)) => Ok(serde_json::from_str(&s)?),
Ok(Err(e)) => Err(e),
Err(_) => Err("call canceled from python".into()),
}
let rx = Box::new(rx.then(|res| match res {
Ok(Ok(s)) => Ok(serde_json::from_str(&s)?),
Ok(Err(e)) => Err(e),
Err(_) => Err("call canceled from python".into()),
}));
(call, rx)
}
Expand All @@ -390,7 +395,7 @@ impl PythonCall {
fn json_or_error(json: &str) -> Result<String> {
if let Ok(err) = serde_json::from_str::<PythonError>(json) {
if err.error {
return Err(format!("python exception: {}", err.error_msg).into())
return Err(format!("python exception: {}", err.error_msg).into());
}
}
Ok(json.to_string())
Expand Down
15 changes: 9 additions & 6 deletions autopush_rs/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,20 @@ pub type MyFuture<T> = Box<Future<Item = T, Error = Error>>;

pub trait FutureChainErr<T> {
fn chain_err<F, E>(self, callback: F) -> MyFuture<T>
where F: FnOnce() -> E + 'static,
E: Into<ErrorKind>;
where
F: FnOnce() -> E + 'static,
E: Into<ErrorKind>;
}

impl<F> FutureChainErr<F::Item> for F
where F: Future + 'static,
F::Error: error::Error + Send + 'static,
where
F: Future + 'static,
F::Error: error::Error + Send + 'static,
{
fn chain_err<C, E>(self, callback: C) -> MyFuture<F::Item>
where C: FnOnce() -> E + 'static,
E: Into<ErrorKind>,
where
C: FnOnce() -> E + 'static,
E: Into<ErrorKind>,
{
Box::new(self.then(|r| r.chain_err(callback)))
}
Expand Down
Loading