Skip to content

Commit

Permalink
Add FromStr impl for Position (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
amousset authored Dec 21, 2024
1 parent ca2790e commit 95b3c2b
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions raugeas/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -125,6 +126,21 @@ pub enum Position {
After,
}

impl FromStr for Position {
type Err = Error;

fn from_str(s: &str) -> Result<Self> {
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<Position> for c_int {
fn from(pos: Position) -> Self {
match pos {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 95b3c2b

Please sign in to comment.