Skip to content

Commit

Permalink
Add a helper enum and method for save modes (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
amousset authored Dec 15, 2024
1 parent 6420ee0 commit 62f4a58
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion raugeas/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ extern crate bitflags;
use raugeas_sys::*;
use std::convert::From;
use std::ffi::CString;
use std::fmt::Display;
use std::mem::transmute;
use std::ops::Range;
use std::os::raw::{c_char, c_int};
use std::ptr;
use std::{fmt, ptr};

pub mod error;
use error::AugeasError;
Expand All @@ -79,6 +80,30 @@ pub struct Augeas {
ptr: *mut augeas,
}

/// Parameters for the save modes.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SaveMode {
/// Do not save changes in files.
Noop,
/// Save changes into a file and overwrite the original file.
Overwrite,
/// Save changes into a file with extension `.augnew`, and do not overwrite the original file.
NewFile,
/// Keep the original file with a `.augsave` extension.
Backup,
}

impl Display for SaveMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SaveMode::Noop => write!(f, "noop"),
SaveMode::Overwrite => write!(f, "overwrite"),
SaveMode::NewFile => write!(f, "newfile"),
SaveMode::Backup => write!(f, "backup"),
}
}
}

/// The insert position.
///
/// Use this enum with [`insert`](#method.insert) to indicate whether the
Expand Down Expand Up @@ -880,6 +905,11 @@ impl Augeas {
})
}

/// Set the behavior of the save operation.
pub fn set_save_mode(&mut self, mode: SaveMode) -> Result<()> {
self.set("/augeas/save", &mode.to_string())
}

fn check_tree_error(&self, path: &str) -> Result<()> {
match self.tree_error(path)? {
Some(e) => Err(e.into()),
Expand Down Expand Up @@ -1317,6 +1347,23 @@ mod tests {
assert_eq!(num, CommandsNumber::Quit);
}

#[test]
fn save_mode_test() {
let mut aug = Augeas::init("tests/test_root", "", Flags::NONE).unwrap();
aug.set_save_mode(SaveMode::Backup).unwrap();
let mode = aug.get("/augeas/save").unwrap().unwrap();
assert_eq!("backup", mode);
aug.set_save_mode(SaveMode::Noop).unwrap();
let mode = aug.get("/augeas/save").unwrap().unwrap();
assert_eq!("noop", mode);
aug.set_save_mode(SaveMode::NewFile).unwrap();
let mode = aug.get("/augeas/save").unwrap().unwrap();
assert_eq!("newfile", mode);
aug.set_save_mode(SaveMode::Overwrite).unwrap();
let mode = aug.get("/augeas/save").unwrap().unwrap();
assert_eq!("overwrite", mode);
}

#[test]
fn error_test() {
let aug = Augeas::init("tests/test_root", "", Flags::NONE).unwrap();
Expand Down

0 comments on commit 62f4a58

Please sign in to comment.