From 245a6ca010512a64520721ddee81ee4da9d8c352 Mon Sep 17 00:00:00 2001 From: Edward Rudd Date: Wed, 27 Sep 2023 17:55:12 -0400 Subject: [PATCH] Change lifetime specifier for From<&str> for QuotaResourceName - otherwise in common use-cases one cannot convert a String easily into a QuotaResourceName --- src/types/quota.rs | 73 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/src/types/quota.rs b/src/types/quota.rs index cff88c4..72d1506 100644 --- a/src/types/quota.rs +++ b/src/types/quota.rs @@ -57,6 +57,16 @@ impl<'a> From<&'a str> for QuotaResourceName<'a> { } } +impl From for QuotaResourceName<'_> { + fn from(input: String) -> Self { + match input.as_str() { + "STORAGE" => QuotaResourceName::Storage, + "MESSAGE" => QuotaResourceName::Message, + _ => QuotaResourceName::Atom(Cow::from(input)), + } + } +} + impl Display for QuotaResourceName<'_> { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { @@ -255,6 +265,24 @@ mod tests { assert!(matches!(new_owned, QuotaResourceName::Atom(Cow::Owned(_)))); } + #[test] + fn test_quota_resource_name_from_str() { + let name = "STORAGE"; + + let name: QuotaResourceName<'_> = name.into(); + + assert!(matches!(name, QuotaResourceName::Storage)); + } + + #[test] + fn test_quota_resource_name_from_string() { + let name = "STORAGE".to_string(); + + let name: QuotaResourceName<'_> = name.into(); + + assert!(matches!(name, QuotaResourceName::Storage)); + } + #[test] fn test_quota_resource_limit_new() { let limit = QuotaResourceLimit::new("STORAGE", 1000); @@ -262,4 +290,49 @@ mod tests { assert_eq!(limit.name, QuotaResourceName::Storage); assert_eq!(limit.amount, 1000); } + + #[test] + fn test_quota_resource_limit_new_custom() { + let name = "X-NUM-FOLDERS"; + + let limit = QuotaResourceLimit::new(name, 50); + + assert!(matches!( + limit.name, + QuotaResourceName::Atom(x) if x == Cow::from("X-NUM-FOLDERS") + )); + assert_eq!(limit.amount, 50); + } + #[test] + fn test_quota_resource_limit_new_from_string() { + let name = "STORAGE".to_string(); + + // use a function to for use of a dropped string + fn make_limit(name: String) -> QuotaResourceLimit<'static> { + QuotaResourceLimit::new(name, 1000) + } + + let limit = make_limit(name); + + assert_eq!(limit.name, QuotaResourceName::Storage); + assert_eq!(limit.amount, 1000); + } + + #[test] + fn test_quota_resource_limit_new_custom_from_string() { + let name = "X-NUM-FOLDERS".to_string(); + + // use a function to for use of a dropped string + fn make_limit(name: String) -> QuotaResourceLimit<'static> { + QuotaResourceLimit::new(name, 50) + } + + let limit = make_limit(name); + + assert!(matches!( + limit.name, + QuotaResourceName::Atom(x) if x == Cow::from("X-NUM-FOLDERS") + )); + assert_eq!(limit.amount, 50); + } }