From cef3a17d55d0de60cbaf91145fee397fcba6a26c Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Wed, 25 Sep 2019 22:18:42 -0400 Subject: [PATCH] Move the control protocol enum to main client lib Side benefit: forcing documentation --- aeron_driver-sys/src/lib.rs | 48 ------------------------------ src/control_protocol.rs | 59 +++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 3 files changed, 60 insertions(+), 48 deletions(-) create mode 100644 src/control_protocol.rs diff --git a/aeron_driver-sys/src/lib.rs b/aeron_driver-sys/src/lib.rs index b4608e3..386ea65 100644 --- a/aeron_driver-sys/src/lib.rs +++ b/aeron_driver-sys/src/lib.rs @@ -4,46 +4,6 @@ #![allow(clippy::all)] include!(concat!(env!("OUT_DIR"), "/bindings.rs")); -/// Construct a C-compatible enum out of a set of constants. -/// Commonly used for types in Aeron that have fixed values via `#define`, -/// but aren't actually enums (e.g. AERON_COMMAND_.*, AERON_ERROR_CODE_.*). -/// Behavior is ultimately very similar to `num::FromPrimitive`. -macro_rules! define_enum { - ($(#[$outer:meta])*, $name:ident, [$(($left:ident, $right:expr)),*]) => { - #[repr(u32)] - #[derive(Debug, PartialEq)] - $(#[$outer])* - pub enum $name { - $($left = $right),* - } - - impl ::std::convert::TryFrom for $name { - type Error = (); - fn try_from(val: u32) -> Result<$name, ()> { - match val { - $(v if v == $name::$left as u32 => Ok($name::$left)),*, - _ => Err(()) - } - } - } - } -} - -define_enum!( - #[doc = "Command codes used when interacting with the Media Driver"], - AeronCommand, [ - (AddPublication, AERON_COMMAND_ADD_PUBLICATION), - (RemovePublication, AERON_COMMAND_REMOVE_PUBLICATION) - ] -); - -define_enum!( - #[doc = "Error codes used by the Media Driver control protocol"], - AeronControlErrorCode, [ - (GenericError, AERON_ERROR_CODE_GENERIC_ERROR) - ] -); - #[cfg(test)] mod tests { use crate::*; @@ -58,12 +18,4 @@ mod tests { assert_eq!(minor, 21); assert_eq!(patch, 2); } - - #[test] - fn define_enum_try() { - assert_eq!( - Ok(AeronCommand::AddPublication), - AERON_COMMAND_ADD_PUBLICATION.try_into() - ); - } } diff --git a/src/control_protocol.rs b/src/control_protocol.rs new file mode 100644 index 0000000..a8c8bea --- /dev/null +++ b/src/control_protocol.rs @@ -0,0 +1,59 @@ +//! Utilities for interacting with the control protocol of the Media Driver +use aeron_driver_sys::*; + +/// Construct a C-compatible enum out of a set of constants. +/// Commonly used for types in Aeron that have fixed values via `#define`, +/// but aren't actually enums (e.g. AERON_COMMAND_.*, AERON_ERROR_CODE_.*). +/// Behavior is ultimately very similar to `num::FromPrimitive`. +macro_rules! define_enum { + ( + $(#[$outer:meta])* + $name:ident, [$( + $(#[$inner:meta]),* + $left:ident = $right:ident + ),+] + ) => { + #[repr(u32)] + #[derive(Debug, PartialEq)] + $(#[$outer])* + pub enum $name {$( + $(#[$inner])* + $left = $right, + )*} + + impl ::std::convert::TryFrom for $name { + type Error = (); + fn try_from(val: u32) -> Result<$name, ()> { + match val { + $(v if v == $name::$left as u32 => Ok($name::$left)),*, + _ => Err(()) + } + } + } + } +} + +define_enum!( + #[doc = "Commands sent from clients to the Media Driver"] + ClientCommand, + [ + #[doc = "Client declaring a new publication"] + AddPublication = AERON_COMMAND_ADD_PUBLICATION, + #[doc = "Client removing a publication"] + RemovePublication = AERON_COMMAND_REMOVE_PUBLICATION + ] +); + +#[cfg(test)] +mod tests { + use crate::control_protocol::ClientCommand; + use std::convert::TryInto; + + #[test] + fn client_command_convert() { + assert_eq!( + Ok(ClientCommand::AddPublication), + ::aeron_driver_sys::AERON_COMMAND_ADD_PUBLICATION.try_into() + ) + } +} diff --git a/src/lib.rs b/src/lib.rs index 1895dfa..d988f30 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,4 +2,5 @@ #![deny(missing_docs)] pub mod client; +pub mod control_protocol; pub mod driver;