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
//! RPM macros are configuration parameters that have largely replaced the
//! previous rpmrc system.

use librpm_sys;
use std::ffi::CString;

use error::Error;

/// Scopes in which macros are defined
pub struct MacroContext(librpm_sys::rpmMacroContext);

/// Obtain the default global context
impl Default for MacroContext {
    fn default() -> MacroContext {
        unsafe { MacroContext(librpm_sys::rpmGlobalMacroContext) }
    }
}

impl MacroContext {
    /// Define a macro in this context. Macros take the form:
    ///
    /// `<name>[(opts)] <body>`
    ///
    /// Level defines the macro recursion level (0 is the entry API)
    pub fn define(&self, macro_string: &str, level: isize) -> Result<(), Error> {
        let cstr = CString::new(macro_string).map_err(|e| err!(ConfigError, "{}", e))?;

        unsafe {
            librpm_sys::rpmDefineMacro(self.0, cstr.as_ptr(), level as i32);
        }

        Ok(())
    }

    /// Delete a macro from this context.
    pub fn delete(&self, name: &str) -> Result<(), Error> {
        let cstr = CString::new(name).unwrap();

        unsafe {
            librpm_sys::delMacro(self.0, cstr.as_ptr());
        }

        Ok(())
    }
}