Merge pull request #272 from urkle/fix-quota-resource-name-from

Implement From<String> for QuotaResourceName
This commit is contained in:
Jon Gjengset 2023-10-07 16:54:18 +02:00 committed by GitHub
commit 2bd38aa6a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

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