From 49b12fd2965c89143af12a443b79fde1cdba6ae4 Mon Sep 17 00:00:00 2001 From: mii443 Date: Thu, 17 Oct 2024 07:56:13 +0000 Subject: [PATCH] impl FromStr for CGroupLimitValue --- src/cgroup/limit_value.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/cgroup/limit_value.rs b/src/cgroup/limit_value.rs index 6ed402f..d114247 100644 --- a/src/cgroup/limit_value.rs +++ b/src/cgroup/limit_value.rs @@ -1,5 +1,32 @@ +use std::str::FromStr; + #[derive(Debug)] -pub enum CGroupLimitValue { +pub enum CGroupLimitValue +where + T: FromStr, +{ Max, Value(T), } + +#[derive(Debug, PartialEq, Eq)] +pub struct ParseCGroupLimitValueError; + +impl FromStr for CGroupLimitValue +where + T: FromStr, +{ + type Err = ParseCGroupLimitValueError; + + fn from_str(s: &str) -> Result { + if s.trim() == "max" { + Ok(Self::Max) + } else { + if let Ok(value) = T::from_str(s) { + Ok(Self::Value(value)) + } else { + Err(ParseCGroupLimitValueError) + } + } + } +}