Skip to content

Commit

Permalink
enum: Replacing current type conversion by enums
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Jansen committed Jun 7, 2017
1 parent 8b01b7d commit 72fd37e
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 126 deletions.
30 changes: 17 additions & 13 deletions src/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,16 +338,19 @@ pub type BaselineCPUFlags = self::libc::c_int;
pub const VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES: BaselineCPUFlags = (1 << 0);
pub const VIR_CONNECT_BASELINE_CPU_MIGRATABLE: BaselineCPUFlags = (1 << 1);

pub type ConnectCredentialType = self::libc::c_int;
pub const VIR_CRED_USERNAME: ConnectCredentialType = 1;
pub const VIR_CRED_AUTHNAME: ConnectCredentialType = 2;
pub const VIR_CRED_LANGUAGE: ConnectCredentialType = 3;
pub const VIR_CRED_CNONCE: ConnectCredentialType = 4;
pub const VIR_CRED_PASSPHRASE: ConnectCredentialType = 5;
pub const VIR_CRED_ECHOPROMPT: ConnectCredentialType = 6;
pub const VIR_CRED_NOECHOPROMPT: ConnectCredentialType = 7;
pub const VIR_CRED_REALM: ConnectCredentialType = 8;
pub const VIR_CRED_EXTERNAL: ConnectCredentialType = 9;
virt_enum! {
ConnectCredentialType {
Username -> 1,
Authname -> 2,
Language -> 3,
Cnonce -> 4,
Passphrase -> 5,
Echoprompt -> 6,
Noechoprompt -> 7,
Realm -> 8,
External -> 9,
}
}

#[derive(Clone, Debug)]
pub struct NodeInfo {
Expand Down Expand Up @@ -380,7 +383,7 @@ pub type ConnectAuthCallback = fn(creds: &mut Vec<ConnectCredential>);
#[derive(Clone, Debug)]
pub struct ConnectCredential {
/// One of `ConnectCredentialType` constants
pub typed: i32,
pub typed: ConnectCredentialType,
/// Prompt to show to user.
pub prompt: String,
/// Additional challenge to show.
Expand All @@ -399,7 +402,7 @@ impl ConnectCredential {
default = c_chars_to_string!((*cred).defresult, nofree);
}
ConnectCredential {
typed: (*cred).typed,
typed: (*cred).typed.into(),
prompt: c_chars_to_string!((*cred).prompt, nofree),
challenge: c_chars_to_string!((*cred).challenge, nofree),
def_result: default,
Expand Down Expand Up @@ -448,8 +451,9 @@ impl ConnectAuth {
}

unsafe {
let mut cred = self.creds[0].clone().into();
sys::virConnectAuth {
credtype: &mut self.creds[0],
credtype: &mut cred,
ncredtype: self.creds.len() as u32,
cb: wrapper,
cbdata: mem::transmute(self.callback),
Expand Down
67 changes: 38 additions & 29 deletions src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,11 +375,14 @@ extern "C" {
-> sys::virDomainPtr;
}

pub type DomainXMLFlags = self::libc::c_uint;
pub const VIR_DOMAIN_XML_SECURE: DomainXMLFlags = 1 << 0;
pub const VIR_DOMAIN_XML_INACTIVE: DomainXMLFlags = 1 << 1;
pub const VIR_DOMAIN_XML_UPDATE_CPU: DomainXMLFlags = 1 << 2;
pub const VIR_DOMAIN_XML_MIGRATABLE: DomainXMLFlags = 1 << 3;
virt_enum! {
DomainXMLFlags {
Secure -> 1,
Inactive -> 2,
UpdateCpu -> 4,
Migratable -> 8,
}
}

pub type DomainCreateFlags = self::libc::c_uint;
pub const VIR_DOMAIN_NONE: DomainCreateFlags = 0;
Expand Down Expand Up @@ -439,20 +442,26 @@ pub const VIR_DOMAIN_SAVE_BYPASS_CACHE: DomainSaveRestoreFlags = 1 << 0;
pub const VIR_DOMAIN_SAVE_RUNNING: DomainSaveRestoreFlags = 1 << 1;
pub const VIR_DOMAIN_SAVE_PAUSED: DomainSaveRestoreFlags = 1 << 2;

pub type DomainNumatuneMemMode = self::libc::c_int;
pub const VIR_DOMAIN_NUMATUNE_MEM_STRICT: DomainNumatuneMemMode = 0;
pub const VIR_DOMAIN_NUMATUNE_MEM_PREFERRED: DomainNumatuneMemMode = 1;
pub const VIR_DOMAIN_NUMATUNE_MEM_INTERLEAVE: DomainNumatuneMemMode = 2;

pub type DomainState = self::libc::c_uint;
pub const VIR_DOMAIN_NOSTATE: DomainState = 0;
pub const VIR_DOMAIN_RUNNING: DomainState = 1;
pub const VIR_DOMAIN_BLOCKED: DomainState = 2;
pub const VIR_DOMAIN_PAUSED: DomainState = 3;
pub const VIR_DOMAIN_SHUTDOWN: DomainState = 4;
pub const VIR_DOMAIN_SHUTOFF: DomainState = 5;
pub const VIR_DOMAIN_CRASHED: DomainState = 6;
pub const VIR_DOMAIN_PMSUSPENDED: DomainState = 7;
virt_enum! {
DomainNumatuneMemMode {
Strict -> 0,
Preferred -> 1,
Interleave -> 2,
}
}

virt_enum! {
DomainState {
Nostate -> 0,
Running -> 1,
Blocked -> 2,
Paused -> 3,
Shutdown -> 4,
Shutoff -> 5,
Crashed -> 6,
PmSuspended -> 7,
}
}

#[derive(Clone, Debug)]
pub struct DomainInfo {
Expand All @@ -472,7 +481,7 @@ impl DomainInfo {
pub fn from_ptr(ptr: sys::virDomainInfoPtr) -> DomainInfo {
unsafe {
DomainInfo {
state: (*ptr).state as DomainState,
state: ((*ptr).state as u32).into(),
max_mem: (*ptr).maxMem as u64,
memory: (*ptr).memory as u64,
nr_virt_cpu: (*ptr).nrVirtCpu as u32,
Expand Down Expand Up @@ -562,7 +571,7 @@ impl NUMAParameters {
"numa_nodeset" => {
ret.node_set = Some(c_chars_to_string!(param.value as *mut libc::c_char))
}
"numa_mode" => ret.mode = Some(param.value as libc::c_int),
"numa_mode" => ret.mode = Some(DomainNumatuneMemMode::from(param.value as libc::c_int)),
unknow => panic!("Field not implemented for NUMAParameters, {:?}", unknow),
}
}
Expand Down Expand Up @@ -698,7 +707,7 @@ impl Domain {
if ret == -1 {
return Err(Error::new());
}
return Ok((state as DomainState, reason as i32));
return Ok(((state as u32).into(), reason as i32));
}
}

Expand Down Expand Up @@ -1677,28 +1686,28 @@ impl Domain {
if params.hard_limit.is_some() {
cparams.push(virTypedParameter {
field: to_arr("hard_limit\0"),
typed: ::typedparam::VIR_TYPED_PARAM_ULLONG,
typed: ::typedparam::TypedParameterType::Ullong.into(),
value: params.hard_limit.unwrap(),
})
}
if params.soft_limit.is_some() {
cparams.push(virTypedParameter {
field: to_arr("soft_limit\0"),
typed: ::typedparam::VIR_TYPED_PARAM_ULLONG,
typed: ::typedparam::TypedParameterType::Ullong.into(),
value: params.soft_limit.unwrap(),
})
}
if params.min_guarantee.is_some() {
cparams.push(virTypedParameter {
field: to_arr("min_guarantee\0"),
typed: ::typedparam::VIR_TYPED_PARAM_ULLONG,
typed: ::typedparam::TypedParameterType::Ullong.into(),
value: params.min_guarantee.unwrap(),
})
}
if params.swap_hard_limit.is_some() {
cparams.push(virTypedParameter {
field: to_arr("swap_hard_limit\0"),
typed: ::typedparam::VIR_TYPED_PARAM_ULLONG,
typed: ::typedparam::TypedParameterType::Ullong.into(),
value: params.swap_hard_limit.unwrap(),
})
}
Expand Down Expand Up @@ -1830,15 +1839,15 @@ impl Domain {
if params.node_set.is_some() {
cparams.push(virTypedParameter {
field: to_arr("numa_nodeset\0"),
typed: ::typedparam::VIR_TYPED_PARAM_STRING,
typed: ::typedparam::TypedParameterType::String.into(),
value: string_to_mut_c_chars!(params.node_set.unwrap()) as u64,
})
}
if params.mode.is_some() {
cparams.push(virTypedParameter {
field: to_arr("numa_mode\0"),
typed: ::typedparam::VIR_TYPED_PARAM_INT,
value: params.mode.unwrap() as u64,
typed: ::typedparam::TypedParameterType::Int.into(),
value: params.mode.unwrap().into(),
})
}

Expand Down
21 changes: 6 additions & 15 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,11 @@ extern "C" {
fn virGetLastError() -> sys::virErrorPtr;
}

#[derive(Debug, PartialEq)]
pub enum ErrorLevel {
None,
Warning,
Error,
}

impl ErrorLevel {
pub fn new(level: u32) -> ErrorLevel {
match level {
0 => ErrorLevel::None,
1 => ErrorLevel::Warning,
_ => ErrorLevel::Error,
}
virt_enum! {
ErrorLevel {
None -> 0,
Warning -> 1,
Error -> 2,
}
}

Expand All @@ -78,7 +69,7 @@ impl Error {
code: (*ptr).code,
domain: (*ptr).domain,
message: c_chars_to_string!((*ptr).message, nofree),
level: ErrorLevel::new((*ptr).level as u32),
level: ((*ptr).level as u32).into(),
}
}
}
Expand Down
68 changes: 68 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,74 @@ macro_rules! string_to_mut_c_chars {
($x:expr) => (::std::ffi::CString::new($x).unwrap().into_raw())
}

macro_rules! virt_enum {
($name:ident { $($tag:ident -> $value:expr),*, }) => (virt_enum!($name { $($tag -> $value),* }););
($name:ident { $($tag:ident -> $value:expr),* }) => {

#[derive(Clone, Debug, PartialEq)]
pub enum $name {
$($tag),*,
Custom(u32),
}

impl From<u32> for $name {
fn from(flag: u32) -> Self {
match flag {
$($value => $name::$tag),*,
s => $name::Custom(s),
}
}
}

impl From<$name> for u32 {
fn from(flag: $name) -> Self {
match flag {
$($name::$tag => $value),*,
$name::Custom(s) => s,
}
}
}

impl From<i32> for $name {
fn from(flag: i32) -> Self {
match flag as u32 {
$($value => $name::$tag),*,
s => $name::Custom(s),
}
}
}

impl From<$name> for i32 {
fn from(flag: $name) -> Self {
let flag = match flag {
$($name::$tag => $value),*,
$name::Custom(s) => s,
};
flag as i32
}
}

impl From<u64> for $name {
fn from(flag: u64) -> Self {
match flag as u32 {
$($value => $name::$tag),*,
s => $name::Custom(s),
}
}
}

impl From<$name> for u64 {
fn from(flag: $name) -> Self {
let flag = match flag {
$($name::$tag => $value),*,
$name::Custom(s) => s,
};
flag as u64
}
}
};
}

pub mod typedparam;
pub mod connect;
pub mod domain;
Expand Down
52 changes: 29 additions & 23 deletions src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,33 @@ extern "C" {
pub type NetworkXMLFlags = self::libc::c_uint;
pub const VIR_NETWORK_XML_INACTIVE: NetworkXMLFlags = 1 << 0;

pub type NetworkUpdateCommand = self::libc::c_uint;
pub const VIR_NETWORK_UPDATE_COMMAND_NONE: NetworkUpdateCommand = 0;
pub const VIR_NETWORK_UPDATE_COMMAND_MODIFY: NetworkUpdateCommand = 1;
pub const VIR_NETWORK_UPDATE_COMMAND_DELETE: NetworkUpdateCommand = 2;
pub const VIR_NETWORK_UPDATE_COMMAND_ADD_LAST: NetworkUpdateCommand = 3;
pub const VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST: NetworkUpdateCommand = 4;

pub type NetworkUpdateSection = self::libc::c_uint;
pub const VIR_NETWORK_SECTION_NONE: NetworkUpdateSection = 0;
pub const VIR_NETWORK_SECTION_BRIDGE: NetworkUpdateSection = 1;
pub const VIR_NETWORK_SECTION_DOMAIN: NetworkUpdateSection = 2;
pub const VIR_NETWORK_SECTION_IP: NetworkUpdateSection = 3;
pub const VIR_NETWORK_SECTION_IP_DHCP_HOST: NetworkUpdateSection = 4;
pub const VIR_NETWORK_SECTION_IP_DHCP_RANGE: NetworkUpdateSection = 5;
pub const VIR_NETWORK_SECTION_FORWARD: NetworkUpdateSection = 6;
pub const VIR_NETWORK_SECTION_FORWARD_INTERFACE: NetworkUpdateSection = 7;
pub const VIR_NETWORK_SECTION_FORWARD_PF: NetworkUpdateSection = 8;
pub const VIR_NETWORK_SECTION_PORTGROUP: NetworkUpdateSection = 9;
pub const VIR_NETWORK_SECTION_DNS_HOST: NetworkUpdateSection = 10;
pub const VIR_NETWORK_SECTION_DNS_TXT: NetworkUpdateSection = 11;
pub const VIR_NETWORK_SECTION_DNS_SRV: NetworkUpdateSection = 12;
virt_enum! {
NetworkUpdateCommand {
None -> 0,
Modify -> 1,
Delete -> 2,
AddLast -> 3,
AddFirst -> 4,
}
}

virt_enum! {
NetworkUpdateSection {
None -> 0,
Bridge -> 1,
Domain -> 2,
Ip -> 3,
IpDhcpHost -> 4,
IpDhcpRange -> 5,
Forward -> 6,
ForwardInterface -> 7,
ForwardPf -> 8,
Portgroup -> 9,
DnsHost -> 10,
DnsTxt -> 11,
DnsSrv -> 12,
}
}

pub type NetworkUpdateFlags = self::libc::c_uint;
pub const VIR_NETWORK_UPDATE_AFFECT_CURRENT: NetworkUpdateFlags = 0;
Expand Down Expand Up @@ -319,8 +325,8 @@ impl Network {
-> Result<(), Error> {
unsafe {
let ret = virNetworkUpdate(self.as_ptr(),
cmd,
section,
cmd.into(),
section.into(),
index as libc::c_uint,
string_to_c_chars!(xml),
flags);
Expand Down
Loading

0 comments on commit 72fd37e

Please sign in to comment.