Skip to content

Commit

Permalink
Change lifetime specifier for From<&str> for QuotaResourceName
Browse files Browse the repository at this point in the history
- otherwise in common use-cases one cannot convert a String easily into a QuotaResourceName
  • Loading branch information
urkle committed Oct 5, 2023
1 parent ce87f7d commit 245a6ca
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/types/quota.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ impl<'a> From<&'a str> for QuotaResourceName<'a> {
}
}

impl From<String> 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 {
Expand Down Expand Up @@ -255,11 +265,74 @@ 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);

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);
}
}

0 comments on commit 245a6ca

Please sign in to comment.