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 cc5944d
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 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)),

Check warning on line 65 in src/types/quota.rs

View check run for this annotation

Codecov / codecov/patch

src/types/quota.rs#L64-L65

Added lines #L64 - L65 were not covered by tests
}
}
}

impl Display for QuotaResourceName<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down Expand Up @@ -255,11 +265,44 @@ 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_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);
}
}

0 comments on commit cc5944d

Please sign in to comment.