1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use std::ffi::*;
use std::{string, ptr};
use virt;
use error::VirError;

#[derive(Clone)]
struct VirInterface {
    ptr: virt::virInterfacePtr
}

impl VirInterface {
    pub fn create(self, flags: u32) -> Result<(), VirError> {
        unsafe {
            match virt::virInterfaceCreate(self.ptr, flags) == -1{
                true => Err(VirError::new()),
                false => Ok(())
            }
        }
    }

    pub fn destroy(self, flags: u32) -> Result<(), VirError> {
        unsafe {
            match virt::virInterfaceDestroy(self.ptr, flags) == -1{
                true => Err(VirError::new()),
                false => Ok(())
            }
        }
    }

    pub fn is_active(self) -> Result<(), VirError> {
        unsafe {
            match virt::virInterfaceIsActive(self.ptr) == 1 {
                true => Ok(()),
                false => Err(VirError::new())
            }
        }
    }

    pub fn mac(self) -> Result<String, VirError> {
        unsafe {
            let result = virt::virInterfaceGetMACString(self.ptr);
            match result.is_null() {
                true => Err(VirError::new()),
                false => Ok(String::from_utf8_lossy(CStr::from_ptr(result).to_bytes()).into_owned())
            }
        }
    }

    pub fn name(self) -> Result<String, VirError> {
        unsafe {
            let result = virt::virInterfaceGetName(self.ptr);
            match result.is_null() {
                true => Err(VirError::new()),
                false => Ok(String::from_utf8_lossy(CStr::from_ptr(result).to_bytes()).into_owned())
            }
        }
    }

    pub fn xml_desc(self, flags: u32) -> Result<String, VirError>{
        unsafe {
            let results = virt::virInterfaceGetXMLDesc(self.ptr, flags);
            match results.is_null() {
                true => Err(VirError::new()),
                false => Ok(String::from_utf8_lossy(CStr::from_ptr(results).to_bytes()).into_owned())
            }
        }
    }

    pub fn undefine(self) -> Result<(), VirError>{
        unsafe {
            match virt::virInterfaceUndefine(self.ptr) != -1 {
                false => Err(VirError::new()),
                true => Ok(())
            }
        }
    }

    pub fn free(self) -> Result<(), VirError> {
        unsafe {
            match virt::virInterfaceFree(self.ptr) != -1 {
                false => Err(VirError::new()),
                true => Ok(())
            }
        }
    }

}