From f2d288fd5d1054075374f3f7229f8a9332adf61c Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Wed, 24 Jul 2024 19:39:46 -0400 Subject: Be more specific about zero vs. two or more ASGs matching the name. This also lets me remove the clever-but-silly Single trait. --- src/autoscaling.rs | 23 +++++++++-------------- src/lib.rs | 1 - src/single.rs | 24 ------------------------ 3 files changed, 9 insertions(+), 39 deletions(-) delete mode 100644 src/single.rs diff --git a/src/autoscaling.rs b/src/autoscaling.rs index 327a4f7..ea76dc0 100644 --- a/src/autoscaling.rs +++ b/src/autoscaling.rs @@ -1,9 +1,7 @@ -use anyhow::{anyhow, Result}; +use anyhow::{bail, Result}; use aws_sdk_autoscaling as autoscaling; use aws_sdk_autoscaling::types::AutoScalingGroup; -use crate::single::Single; - pub trait AutoScaling { fn autoscaling(&self) -> &autoscaling::Client; } @@ -12,22 +10,19 @@ pub async fn asg_by_name(aws_context: &C, name: &str) -> Result group, + (None, _) => bail!("No autoscaling group found with name: {name}"), + (Some(_), Some(_)) => bail!("Multiple autoscaling groups found with name: {name}"), + }; - Ok(auto_scaling_group) + Ok(group.to_owned()) } diff --git a/src/lib.rs b/src/lib.rs index 6eddc65..178033d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,4 +7,3 @@ mod dns; mod ec2; mod hashable; mod route53; -mod single; diff --git a/src/single.rs b/src/single.rs deleted file mode 100644 index b3f0b18..0000000 --- a/src/single.rs +++ /dev/null @@ -1,24 +0,0 @@ -pub trait Single { - type Item; - - fn single(self) -> Option; -} - -impl Single for I -where - I: IntoIterator, -{ - type Item = T; - - fn single(self) -> Option { - let mut iter = self.into_iter(); - - // There are three cases of interest: - // - // 1. `self` has zero items -> return None. - // 2. `self` has two or more items -> return None. - // 3. `self` has exactly one item -> return `iter.next()` unchanged, as - // it holds that item. - iter.next().filter(|_| iter.next().is_none()) - } -} -- cgit v1.2.3