From 95b3c2b29d56c95049ff04868ae631677d4f00e2 Mon Sep 17 00:00:00 2001 From: Alexis Mousset Date: Sat, 21 Dec 2024 02:01:32 +0100 Subject: [PATCH] Add FromStr impl for Position (#19) --- raugeas/src/lib.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/raugeas/src/lib.rs b/raugeas/src/lib.rs index 882988e..fdce0a3 100644 --- a/raugeas/src/lib.rs +++ b/raugeas/src/lib.rs @@ -56,6 +56,7 @@ use std::mem::transmute; use std::ops::Range; use std::os::raw::{c_char, c_int}; use std::os::unix::prelude::{OsStrExt, OsStringExt}; +use std::str::FromStr; use std::{fmt, ptr}; pub mod error; @@ -125,6 +126,21 @@ pub enum Position { After, } +impl FromStr for Position { + type Err = Error; + + fn from_str(s: &str) -> Result { + match s.to_lowercase().as_str() { + "before" => Ok(Position::Before), + "after" => Ok(Position::After), + _ => Err(Error::Augeas(AugeasError::new_unknown(format!( + "Invalid position: {}", + s + )))), + } + } +} + impl From for c_int { fn from(pos: Position) -> Self { match pos { @@ -1340,6 +1356,15 @@ impl Drop for Augeas { mod tests { use super::*; + #[test] + fn position_test() { + let p = Position::After; + assert_eq!(p, "after".parse().unwrap()); + + let p = Position::Before; + assert_eq!(p, "BEFORE".parse().unwrap()); + } + #[test] fn version_test() { let aug = Augeas::init("", "toto", Flags::NONE).unwrap();