diff --git a/Cargo.lock b/Cargo.lock index 7eb8c67..29ada57 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -37,7 +37,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "compose_spec" -version = "0.3.0-alpha.2" +version = "0.3.0-alpha.3" dependencies = [ "compose_spec_macros", "indexmap", diff --git a/Cargo.toml b/Cargo.toml index 633932a..db017af 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -226,7 +226,7 @@ sort_commits = "oldest" [package] name = "compose_spec" -version = "0.3.0-alpha.2" +version = "0.3.0-alpha.3" authors.workspace = true edition.workspace = true license.workspace = true diff --git a/src/service/limit.rs b/src/service/limit.rs index 4afb5f5..eea3ab8 100644 --- a/src/service/limit.rs +++ b/src/service/limit.rs @@ -2,8 +2,9 @@ //! [`Service`](super::Service). use std::{ - fmt::{self, Formatter}, + fmt::{self, Display, Formatter}, marker::PhantomData, + str::FromStr, }; use serde::{ @@ -44,6 +45,27 @@ impl From for Limit { } } +impl Display for Limit { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + match self { + Self::Value(value) => value.fmt(f), + Self::Unlimited => f.write_str("-1"), + } + } +} + +impl FromStr for Limit { + type Err = T::Err; + + fn from_str(s: &str) -> Result { + if s == "-1" { + Ok(Self::Unlimited) + } else { + s.parse().map(Self::Value) + } + } +} + impl Serialize for Limit { fn serialize(&self, serializer: S) -> Result { match self { @@ -88,10 +110,18 @@ impl<'de, T: Deserialize<'de>> de::Visitor<'de> for Visitor { } fn visit_str(self, v: &str) -> Result { - T::deserialize(v.into_deserializer()).map(Limit::Value) + if v == "-1" { + Ok(Limit::Unlimited) + } else { + T::deserialize(v.into_deserializer()).map(Limit::Value) + } } fn visit_string(self, v: String) -> Result { - T::deserialize(v.into_deserializer()).map(Limit::Value) + if v == "-1" { + Ok(Limit::Unlimited) + } else { + T::deserialize(v.into_deserializer()).map(Limit::Value) + } } }