summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorJustus Winter <justus@pep-project.org>2018-01-09 12:19:23 +0100
committerJustus Winter <justus@pep-project.org>2018-01-09 12:19:23 +0100
commit598cdc97094f067c92f222d2038cf7b4b69cc7af (patch)
tree36ffd176434c65ed8de0712f62570b760371b76e /core
parentbcc668602092449fa53fe80341d73c34818821b7 (diff)
core,ffi: Convert 'core::NetworkPolicy' to and from u8.
- At some points, we need to convert the policy to a primitive type. It is better to just provide it. - Use it in the ffi glue.
Diffstat (limited to 'core')
-rw-r--r--core/src/lib.rs25
1 files changed, 25 insertions, 0 deletions
diff --git a/core/src/lib.rs b/core/src/lib.rs
index 1451bcc3..78242206 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -258,6 +258,31 @@ impl NetworkPolicy {
}
}
+impl<'a> From<&'a NetworkPolicy> for u8 {
+ fn from(policy: &NetworkPolicy) -> Self {
+ match policy {
+ &NetworkPolicy::Offline => 0,
+ &NetworkPolicy::Anonymized => 1,
+ &NetworkPolicy::Encrypted => 2,
+ &NetworkPolicy::Insecure => 3,
+ }
+ }
+}
+
+
+// XXX: TryFrom would be nice.
+impl From<u8> for NetworkPolicy {
+ fn from(policy: u8) -> Self {
+ match policy {
+ 0 => NetworkPolicy::Offline,
+ 1 => NetworkPolicy::Anonymized,
+ 2 => NetworkPolicy::Encrypted,
+ 3 => NetworkPolicy::Insecure,
+ n => panic!("Bad policy: {}", n),
+ }
+ }
+}
+
#[macro_export]
macro_rules! assert_match {
( $error: pat = $expr:expr ) => {