Skip to content

Commit

Permalink
Remove unnecessary JNI macro and organize rust code (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
fundthmcalculus authored Feb 15, 2022
1 parent 6e9d304 commit 7ee00f9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 48 deletions.
25 changes: 0 additions & 25 deletions native/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,6 @@ macro_rules! c_impl {
}};
}

#[allow(unused_macros)]
macro_rules! jni_impl {
($message:ty,$struct:ident,$func:ident,$env:expr,$req:expr) => {{
let request = $env.convert_byte_array($req).unwrap();

let gen_key_req = match <$message>::from_vec(&request) {
Ok(request) => request,
Err(err) => {
$env.throw_new("java/lang/Exception", format!("{:?}", err)).unwrap_or_default();
return $env.byte_array_from_slice(&vec![].as_slice()).unwrap();
}
};

let response = match crate::$struct::$func(&gen_key_req) {
Ok(response) => response,
Err(err) => {
$env.throw_new("java/lang/Exception", format!("{:?}", err)).unwrap_or_default();
return $env.byte_array_from_slice(&vec![].as_slice()).unwrap();
}
};

return $env.byte_array_from_slice(&response.to_vec().as_slice()).unwrap();
}};
}

macro_rules! map_or_return {
($name:expr,$err:expr) => {
match $name {
Expand Down
40 changes: 20 additions & 20 deletions native/src/proto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,6 @@ impl<'de> Visitor<'de> for Value {
write!(formatter, "unrecognized value")
}

fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error>
where
A: de::MapAccess<'de>,
{
let mut fields: HashMap<String, Value> = HashMap::new();
let mut map = map;

while let Some((k, v)) = map.next_entry().unwrap() {
fields.insert(k, v);
}

Ok(Value {
kind: Some(Kind::StructValue(Struct { fields })),
})
}

fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
where
E: Error,
Expand Down Expand Up @@ -134,21 +118,21 @@ impl<'de> Visitor<'de> for Value {
})
}

fn visit_unit<E>(self) -> Result<Self::Value, E>
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: Error,
{
Ok(Value {
kind: Some(Kind::NullValue(0)),
kind: Some(Kind::StringValue(v.to_string())),
})
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
fn visit_unit<E>(self) -> Result<Self::Value, E>
where
E: Error,
{
Ok(Value {
kind: Some(Kind::StringValue(v.to_string())),
kind: Some(Kind::NullValue(0)),
})
}

Expand All @@ -167,6 +151,22 @@ impl<'de> Visitor<'de> for Value {
kind: Some(Kind::ListValue(ListValue { values })),
})
}

fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error>
where
A: de::MapAccess<'de>,
{
let mut fields: HashMap<String, Value> = HashMap::new();
let mut map = map;

while let Some((k, v)) = map.next_entry().unwrap() {
fields.insert(k, v);
}

Ok(Value {
kind: Some(Kind::StructValue(Struct { fields })),
})
}
}

pub mod tests {
Expand Down
2 changes: 2 additions & 0 deletions native/src/proto/pbmse.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod pbmse;

/// JWS
/// Protocol buffer message signing and encryption
#[derive(::serde::Serialize, ::serde::Deserialize)]
Expand Down
6 changes: 3 additions & 3 deletions native/src/tests/oberon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ fn test_create_proof() {
};

let rep = crate::Oberon::proof(&req).unwrap();
assert!(rep.proof.len() == 96)
assert_eq!(rep.proof.len(), 96)
}

#[test]
Expand Down Expand Up @@ -163,7 +163,7 @@ fn test_validate_proof() {
};

let rep = crate::Oberon::verify(&req).unwrap();
assert!(rep.valid == true);
assert_eq!(rep.valid, true);

let req = VerifyOberonProofRequest {
proof: vec![
Expand All @@ -177,7 +177,7 @@ fn test_validate_proof() {
};

let rep = crate::Oberon::verify(&req).unwrap();
assert!(rep.valid == false);
assert_eq!(rep.valid, false);
}

#[test]
Expand Down

0 comments on commit 7ee00f9

Please sign in to comment.