From 2383f151e616d2cd660df8a1547b46a627d1cad3 Mon Sep 17 00:00:00 2001 From: Jan Jansen Date: Wed, 7 Jun 2017 15:20:47 +0200 Subject: [PATCH] enum: Replacing current type conversion by enums --- examples/auth.rs | 10 ++--- examples/hello.rs | 3 +- src/connect.rs | 39 ++++++++++++------- src/domain.rs | 82 +++++++++++++++++++++++++-------------- src/error.rs | 24 +++++------- src/lib.rs | 75 +++++++++++++++++++++++++++++++++++ src/network.rs | 70 ++++++++++++++++++++++----------- src/storage_pool.rs | 24 ++++++++---- src/storage_vol.rs | 66 ++++++++++++++++++++----------- src/typedparam.rs | 26 +++++++++---- tests/domain.rs | 16 ++++---- tests/integration_qemu.rs | 38 +++++++++--------- tests/interface.rs | 3 -- 13 files changed, 322 insertions(+), 154 deletions(-) diff --git a/examples/auth.rs b/examples/auth.rs index 56b378d..c88fa1b 100644 --- a/examples/auth.rs +++ b/examples/auth.rs @@ -41,7 +41,7 @@ extern crate virt; use std::{env, io}; -use virt::connect::{Connect, ConnectCredential, ConnectAuth}; +use virt::connect::{Connect, ConnectCredential, ConnectAuth, ConnectCredentialType}; fn main() { let uri = match env::args().nth(1) { @@ -55,11 +55,11 @@ fn main() { println!("{}:", cred.prompt); match cred.typed { - ::virt::connect::VIR_CRED_AUTHNAME => { + ConnectCredentialType::Authname => { io::stdin().read_line(&mut input).expect(""); cred.result = Some(String::from(input.trim())); } - ::virt::connect::VIR_CRED_PASSPHRASE => { + ConnectCredentialType::Passphrase => { io::stdin().read_line(&mut input).expect(""); cred.result = Some(String::from(input.trim())); } @@ -69,8 +69,8 @@ fn main() { } } }; - let mut auth = ConnectAuth::new(vec![::virt::connect::VIR_CRED_AUTHNAME, - ::virt::connect::VIR_CRED_PASSPHRASE], + let mut auth = ConnectAuth::new(vec![ConnectCredentialType::Authname, + ConnectCredentialType::Passphrase], callback); println!("Attempting to connect to hypervisor: '{}'...", uri); diff --git a/examples/hello.rs b/examples/hello.rs index e5ec6e4..dc624e9 100644 --- a/examples/hello.rs +++ b/examples/hello.rs @@ -28,6 +28,7 @@ use std::env; use virt::connect::Connect; use virt::error::Error; +use virt::domain::DomainNumatuneMemMode; fn show_hypervisor_info(conn: &Connect) -> Result<(), Error> { @@ -87,7 +88,7 @@ fn show_domains(conn: &Connect) -> Result<(), Error> { println!("NUMA:"); println!(" Node Set: {}", numa.node_set.unwrap_or(String::from(""))); - println!(" Mode: {}", numa.mode.unwrap_or(0)); + println!(" Mode: {}", numa.mode.unwrap_or(DomainNumatuneMemMode::Strict)); } } } diff --git a/src/connect.rs b/src/connect.rs index 6482fe8..76ef273 100644 --- a/src/connect.rs +++ b/src/connect.rs @@ -338,16 +338,28 @@ 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 + Username -> 1, + /// Authname + Authname -> 2, + /// Language + Language -> 3, + /// Cnonce + Cnonce -> 4, + /// Passphrase + Passphrase -> 5, + /// Echoprompt + Echoprompt -> 6, + /// Noechoprompt + Noechoprompt -> 7, + /// Realm + Realm -> 8, + /// External + External -> 9, + } +} #[derive(Clone, Debug)] pub struct NodeInfo { @@ -380,7 +392,7 @@ pub type ConnectAuthCallback = fn(creds: &mut Vec); #[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. @@ -399,7 +411,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, @@ -448,8 +460,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), diff --git a/src/domain.rs b/src/domain.rs index 842cd17..b6bdd71 100644 --- a/src/domain.rs +++ b/src/domain.rs @@ -381,11 +381,18 @@ extern "C" { -> libc::c_int; } -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 + Secure -> 1, + /// Inactive + Inactive -> 2, + /// UpdateCpu + UpdateCpu -> 4, + /// Migratable + Migratable -> 8, + } +} pub type DomainCreateFlags = self::libc::c_uint; pub const VIR_DOMAIN_NONE: DomainCreateFlags = 0; @@ -445,20 +452,37 @@ 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 + Strict -> 0, + /// Preferred + Preferred -> 1, + /// Interleave + Interleave -> 2, + } +} + +virt_enum! { + DomainState { + /// Nostate + Nostate -> 0, + /// Running + Running -> 1, + /// Blocked + Blocked -> 2, + /// Paused + Paused -> 3, + /// Shutdown + Shutdown -> 4, + /// Shutoff + Shutoff -> 5, + /// Crashed + Crashed -> 6, + /// PmSuspended + PmSuspended -> 7, + } +} #[derive(Clone, Debug)] pub struct DomainInfo { @@ -478,7 +502,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, @@ -568,7 +592,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), } } @@ -704,7 +728,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)); } } @@ -1683,28 +1707,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(), }) } @@ -1836,15 +1860,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(), }) } diff --git a/src/error.rs b/src/error.rs index 82049ee..5370b05 100644 --- a/src/error.rs +++ b/src/error.rs @@ -42,20 +42,14 @@ 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 + None -> 0, + /// Warning + Warning -> 1, + /// Error + Error -> 2, } } @@ -78,7 +72,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(), } } } diff --git a/src/lib.rs b/src/lib.rs index 5412694..4927108 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -101,6 +101,81 @@ macro_rules! string_to_mut_c_chars { ($x:expr) => (::std::ffi::CString::new($x).unwrap().into_raw()) } +macro_rules! virt_enum { + ($name:ident { $(#[$meta:meta] $tag:ident -> $value:expr),*, }) => (virt_enum!($name { $(#[$meta] $tag -> $value),* });); + ($name:ident { $(#[$meta:meta] $tag:ident -> $value:expr),* }) => { + +#[derive(Clone, Debug, PartialEq)] +pub enum $name { + $(#[$meta] + $tag),*, + Custom(u32), +} + +impl ::std::fmt::Display for $name { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> { + write!(f, "{:?}", self) + } +} + +impl From 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 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 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; diff --git a/src/network.rs b/src/network.rs index 49f9042..a750c43 100644 --- a/src/network.rs +++ b/src/network.rs @@ -73,27 +73,51 @@ 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 + None -> 0, + /// Modify + Modify -> 1, + /// Delete + Delete -> 2, + /// AddLast + AddLast -> 3, + /// AddFirst + AddFirst -> 4, + } +} + +virt_enum! { + NetworkUpdateSection { + /// None + None -> 0, + /// Bridge + Bridge -> 1, + /// Domain + Domain -> 2, + /// Ip + Ip -> 3, + /// IpDhcpHost + IpDhcpHost -> 4, + /// IpDhcpRange + IpDhcpRange -> 5, + /// Forward + Forward -> 6, + /// ForwardInterface + ForwardInterface -> 7, + /// ForwardPf + ForwardPf -> 8, + /// Portgroup + Portgroup -> 9, + /// DnsHost + DnsHost -> 10, + /// DnsTxt + DnsTxt -> 11, + /// DnsSrv + DnsSrv -> 12, + } +} pub type NetworkUpdateFlags = self::libc::c_uint; pub const VIR_NETWORK_UPDATE_AFFECT_CURRENT: NetworkUpdateFlags = 0; @@ -319,8 +343,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); diff --git a/src/storage_pool.rs b/src/storage_pool.rs index 0ff238d..c3128c3 100644 --- a/src/storage_pool.rs +++ b/src/storage_pool.rs @@ -110,17 +110,25 @@ pub const STORAGE_POOL_CREATE_WITH_BUILD: StoragePoolCreateFlags = 1 << 0; pub const STORAGE_POOL_CREATE_WITH_BUILD_OVERWRITE: StoragePoolCreateFlags = 1 << 1; pub const STORAGE_POOL_CREATE_WITH_BUILD_NO_OVERWRITE: StoragePoolCreateFlags = 1 << 2; -pub type StoragePoolState = self::libc::c_uint; -pub const VIR_STORAGE_POOL_INACTIVE: StoragePoolState = 0; -pub const VIR_STORAGE_POOL_BUILDING: StoragePoolState = 1; -pub const VIR_STORAGE_POOL_RUNNING: StoragePoolState = 2; -pub const VIR_STORAGE_POOL_DEGRADED: StoragePoolState = 3; -pub const VIR_STORAGE_POOL_INACCESSIBLE: StoragePoolState = 4; +virt_enum! { + StoragePoolState { + /// Inactive + Inactive -> 0, + /// Building + Building -> 1, + /// Running + Running -> 2, + /// Degraded + Degraded -> 3, + /// Inaccessible + Inaccessible -> 4, + } +} #[derive(Clone, Debug)] pub struct StoragePoolInfo { /// A `StoragePoolState` flags - pub state: u32, + pub state: StoragePoolState, /// Logical size bytes. pub capacity: u64, /// Current allocation bytes. @@ -133,7 +141,7 @@ impl StoragePoolInfo { pub fn from_ptr(ptr: sys::virStoragePoolInfoPtr) -> StoragePoolInfo { unsafe { StoragePoolInfo { - state: (*ptr).state as StoragePoolState, + state: (*ptr).state.into(), capacity: (*ptr).capacity as u64, allocation: (*ptr).allocation as u64, available: (*ptr).available as u64, diff --git a/src/storage_vol.rs b/src/storage_vol.rs index b32d232..04aa6c8 100644 --- a/src/storage_vol.rs +++ b/src/storage_vol.rs @@ -125,30 +125,52 @@ pub const VIR_STORAGE_VOL_RESIZE_ALLOCATE: StorageVolResizeFlags = 1 << 0; pub const VIR_STORAGE_VOL_RESIZE_DELTA: StorageVolResizeFlags = 1 << 1; pub const VIR_STORAGE_VOL_RESIZE_SHRINK: StorageVolResizeFlags = 1 << 2; -pub type StorageVolWipeAlgorithm = self::libc::c_uint; -pub const VIR_STORAGE_VOL_WIPE_ALG_ZERO: StorageVolWipeAlgorithm = 0; -pub const VIR_STORAGE_VOL_WIPE_ALG_NNSA: StorageVolWipeAlgorithm = 1; -pub const VIR_STORAGE_VOL_WIPE_ALG_DOD: StorageVolWipeAlgorithm = 2; -pub const VIR_STORAGE_VOL_WIPE_ALG_BSI: StorageVolWipeAlgorithm = 3; -pub const VIR_STORAGE_VOL_WIPE_ALG_GUTMANN: StorageVolWipeAlgorithm = 4; -pub const VIR_STORAGE_VOL_WIPE_ALG_SCHNEIER: StorageVolWipeAlgorithm = 5; -pub const VIR_STORAGE_VOL_WIPE_ALG_PFITZNER7: StorageVolWipeAlgorithm = 6; -pub const VIR_STORAGE_VOL_WIPE_ALG_PFITZNER33: StorageVolWipeAlgorithm = 7; -pub const VIR_STORAGE_VOL_WIPE_ALG_RANDOM: StorageVolWipeAlgorithm = 8; -pub const VIR_STORAGE_VOL_WIPE_ALG_TRIM: StorageVolWipeAlgorithm = 9; - -pub type StorageVolType = self::libc::c_uint; -pub const VIR_STORAGE_VOL_FILE: StorageVolType = 0; -pub const VIR_STORAGE_VOL_BLOCK: StorageVolType = 1; -pub const VIR_STORAGE_VOL_DIR: StorageVolType = 2; -pub const VIR_STORAGE_VOL_NETWORK: StorageVolType = 3; -pub const VIR_STORAGE_VOL_NETDIR: StorageVolType = 4; -pub const VIR_STORAGE_VOL_PLOOP: StorageVolType = 5; +virt_enum! { + StorageVolWipeAlgorithm { + /// Zero + Zero -> 0, + /// Nnsa + Nnsa -> 1, + /// Dod + Dod -> 2, + /// Bsi + Bsi -> 3, + /// Gutmann + Gutmann -> 4, + /// Schneier + Schneier -> 5, + /// Pfitzner7 + Pfitzner7 -> 6, + /// Pfitzner33 + Pfitzner33 -> 7, + /// Random + Random -> 8, + /// Trim + Trim -> 9, + } +} + +virt_enum! { + StorageVolType { + /// File + File -> 0, + /// Block + Block -> 1, + /// Dir + Dir -> 2, + /// Network + Network -> 3, + /// Netdir + Netdir -> 4, + /// Ploop + Ploop -> 5, + } +} #[derive(Clone, Debug)] pub struct StorageVolInfo { /// See: `virStorageVolType` flags - pub kind: u32, + pub kind: StorageVolType, /// Logical size bytes. pub capacity: u64, /// Current allocation bytes @@ -159,7 +181,7 @@ impl StorageVolInfo { pub fn from_ptr(ptr: sys::virStorageVolInfoPtr) -> StorageVolInfo { unsafe { StorageVolInfo { - kind: (*ptr).kind as StorageVolType, + kind: (*ptr).kind.into(), capacity: (*ptr).capacity as u64, allocation: (*ptr).allocation as u64, } @@ -329,7 +351,7 @@ impl StorageVol { pub fn wipe_pattern(&self, algo: StorageVolWipeAlgorithm, flags: u32) -> Result<(), Error> { unsafe { if virStorageVolWipePattern(self.as_ptr(), - algo as libc::c_uint, + algo.into(), flags as libc::c_uint) == -1 { return Err(Error::new()); } diff --git a/src/typedparam.rs b/src/typedparam.rs index 1a9d651..4a304b8 100644 --- a/src/typedparam.rs +++ b/src/typedparam.rs @@ -51,11 +51,21 @@ pub mod sys { pub type virTypedParameterPtr = *mut virTypedParameter; } -pub type TypedParameterType = self::libc::c_int; -pub const VIR_TYPED_PARAM_INT: TypedParameterType = 1; -pub const VIR_TYPED_PARAM_UINT: TypedParameterType = 2; -pub const VIR_TYPED_PARAM_LLONG: TypedParameterType = 3; -pub const VIR_TYPED_PARAM_ULLONG: TypedParameterType = 4; -pub const VIR_TYPED_PARAM_DOUBLE: TypedParameterType = 5; -pub const VIR_TYPED_PARAM_BOOLEAN: TypedParameterType = 6; -pub const VIR_TYPED_PARAM_STRING: TypedParameterType = 7; +virt_enum! { + TypedParameterType { + /// Int + Int -> 1, + /// Uint + Uint -> 2, + /// Llong + Llong -> 3, + /// Ullong + Ullong -> 4, + /// Double + Double -> 5, + /// Boolean + Boolean -> 6, + /// String + String -> 7, + } +} diff --git a/tests/domain.rs b/tests/domain.rs index 5a64a75..cf849ac 100644 --- a/tests/domain.rs +++ b/tests/domain.rs @@ -20,7 +20,7 @@ extern crate virt; mod common; -use virt::domain::Domain; +use virt::domain::{Domain, DomainState}; fn tdom(exec_test: fn(dom: Domain)) { @@ -73,7 +73,7 @@ fn test_get_xml_desc() { fn test_get_info() { fn t(dom: Domain) { match dom.get_info() { - Ok(info) => assert_eq!(1, info.state), + Ok(info) => assert_eq!(DomainState::Running, info.state), Err(_) => panic!("should have a node info"), } } @@ -117,7 +117,7 @@ fn test_create_with_flags() { let c = common::conn(); let d = common::build_test_domain(&c, "create", false); assert_eq!(Ok(0), d.create_with_flags(0)); - assert_eq!(Ok((::virt::domain::VIR_DOMAIN_RUNNING, 1)), d.get_state()); + assert_eq!(Ok((DomainState::Running, 1)), d.get_state()); assert_eq!(Ok(String::from("libvirt-rs-test-create")), d.get_name()); common::clean(d); common::close(c); @@ -128,9 +128,9 @@ fn test_shutdown() { let c = common::conn(); let d = common::build_test_domain(&c, "shutdown", false); assert_eq!(Ok(0), d.create_with_flags(0)); - assert_eq!(Ok((::virt::domain::VIR_DOMAIN_RUNNING, 1)), d.get_state()); + assert_eq!(Ok((DomainState::Running, 1)), d.get_state()); assert_eq!(Ok(0), d.shutdown()); - assert_eq!(Ok((::virt::domain::VIR_DOMAIN_SHUTOFF, 1)), d.get_state()); + assert_eq!(Ok((DomainState::Shutoff, 1)), d.get_state()); common::clean(d); common::close(c); } @@ -140,11 +140,11 @@ fn test_pause_resume() { let c = common::conn(); let d = common::build_test_domain(&c, "pause_resume", false); assert_eq!(Ok(0), d.create_with_flags(0)); - assert_eq!(Ok((::virt::domain::VIR_DOMAIN_RUNNING, 1)), d.get_state()); + assert_eq!(Ok((DomainState::Running, 1)), d.get_state()); assert_eq!(Ok(0), d.suspend()); - assert_eq!(Ok((::virt::domain::VIR_DOMAIN_PAUSED, 1)), d.get_state()); + assert_eq!(Ok((DomainState::Paused, 1)), d.get_state()); assert_eq!(Ok(0), d.resume()); - assert_eq!(Ok((::virt::domain::VIR_DOMAIN_RUNNING, 5)), d.get_state()); + assert_eq!(Ok((DomainState::Running, 5)), d.get_state()); common::clean(d); common::close(c); } diff --git a/tests/integration_qemu.rs b/tests/integration_qemu.rs index 49e07c4..b00eeee 100644 --- a/tests/integration_qemu.rs +++ b/tests/integration_qemu.rs @@ -23,7 +23,10 @@ extern crate virt; mod common; -use virt::connect::{Connect, ConnectAuth, ConnectCredential}; +use virt::connect::{Connect, ConnectAuth, ConnectCredential, ConnectCredentialType}; +use virt::domain::DomainState; +use virt::storage_vol::StorageVolType; +use virt::storage_pool::StoragePoolState; #[test] #[ignore] @@ -31,8 +34,7 @@ fn test_create_domain_with_flags() { let c = common::qemu_conn(); let d = common::build_qemu_domain(&c, "create", false); assert_eq!(Ok(0), d.create_with_flags(0)); - assert_eq!(Ok((::virt::domain::VIR_DOMAIN_START_PAUSED, 1)), - d.get_state()); + assert_eq!(Ok((DomainState::Running, 1)), d.get_state()); assert_eq!(Ok(String::from("libvirt-rs-test-create")), d.get_name()); common::clean(d); common::close(c); @@ -47,12 +49,10 @@ fn test_create_storage_pool_and_vols() { assert_eq!(Ok(String::from("libvirt-rs-test-create")), p.get_name()); let v = common::build_storage_vol(&p, "vol1", 8); assert_eq!(Ok(String::from("vol1")), v.get_name()); - assert_eq!(Ok(String::from("/var/lib/libvirt/images/vol1")), - v.get_path()); - assert_eq!(Ok(String::from("/var/lib/libvirt/images/vol1")), - v.get_key()); + assert_eq!(Ok(String::from("/var/lib/libvirt/images/vol1")), v.get_path()); + assert_eq!(Ok(String::from("/var/lib/libvirt/images/vol1")), v.get_key()); if let Ok(info) = v.get_info() { - assert_eq!(0, info.kind); + assert_eq!(StorageVolType::File, info.kind); assert_eq!(8192, info.allocation); assert_eq!(8192, info.capacity); } else { @@ -63,7 +63,7 @@ fn test_create_storage_pool_and_vols() { } assert_eq!(Ok(0), v.resize(10240, 0)); if let Ok(info) = v.get_info() { - assert_eq!(0, info.kind); + assert_eq!(StorageVolType::File, info.kind); assert_eq!(8192, info.allocation); assert_eq!(10240, info.capacity); } else { @@ -73,7 +73,7 @@ fn test_create_storage_pool_and_vols() { panic!("should not be here") } if let Ok(info) = p.get_info() { - assert_eq!(2, info.state); + assert_eq!(StoragePoolState::Running, info.state); assert_eq!(0, info.capacity - (info.allocation + info.available)); } else { common::clean_vol(v); @@ -92,10 +92,10 @@ fn test_connection_with_auth() { fn callback(creds: &mut Vec) { for cred in creds { match cred.typed { - ::virt::connect::VIR_CRED_AUTHNAME => { + ConnectCredentialType::Authname => { cred.result = Some(String::from("user")); } - ::virt::connect::VIR_CRED_PASSPHRASE => { + ConnectCredentialType::Passphrase => { cred.result = Some(String::from("pass")); } _ => { @@ -105,8 +105,8 @@ fn test_connection_with_auth() { } }; - let mut auth = ConnectAuth::new(vec![::virt::connect::VIR_CRED_AUTHNAME, - ::virt::connect::VIR_CRED_PASSPHRASE], + let mut auth = ConnectAuth::new(vec![ConnectCredentialType::Authname, + ConnectCredentialType::Passphrase], callback); match Connect::open_auth("test+tcp://127.0.0.1/default", &mut auth, 0) { Ok(c) => common::close(c), @@ -125,10 +125,10 @@ fn test_connection_with_auth_wrong() { fn callback(creds: &mut Vec) { for cred in creds { match cred.typed { - ::virt::connect::VIR_CRED_AUTHNAME => { + ConnectCredentialType::Authname => { cred.result = Some(String::from("user")); } - ::virt::connect::VIR_CRED_PASSPHRASE => { + ConnectCredentialType::Passphrase => { cred.result = Some(String::from("passwrong")); } _ => { @@ -138,8 +138,8 @@ fn test_connection_with_auth_wrong() { } }; - let mut auth = ConnectAuth::new(vec![::virt::connect::VIR_CRED_AUTHNAME, - ::virt::connect::VIR_CRED_PASSPHRASE], + let mut auth = ConnectAuth::new(vec![ConnectCredentialType::Authname, + ConnectCredentialType::Passphrase], callback); if Connect::open_auth("test+tcp://127.0.0.1/default", &mut auth, 0).is_ok() { panic!("open_auth did not work: code {}, message:"); @@ -152,7 +152,7 @@ fn test_reset() { let c = common::qemu_conn(); let d = common::build_qemu_domain(&c, "reset", false); assert_eq!(Ok(0), d.create_with_flags(0)); - assert_eq!(Ok((::virt::domain::VIR_DOMAIN_RUNNING, 1)), d.get_state()); + assert_eq!(Ok((DomainState::Running, 1)), d.get_state()); assert_eq!(Ok(0), d.reset()); // TODO assert something showing reset has the intended side effect common::clean(d); diff --git a/tests/interface.rs b/tests/interface.rs index 917a27e..e86ed73 100644 --- a/tests/interface.rs +++ b/tests/interface.rs @@ -18,9 +18,6 @@ extern crate virt; -use virt::connect::Connect; -use virt::interface::Interface; - mod common; #[test]