Skip to content

Commit

Permalink
Add Region::all()
Browse files Browse the repository at this point in the history
  • Loading branch information
silverlyra committed May 29, 2024
1 parent 0ddd859 commit b0b7c54
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ tokio = { version = "1.34.0", features = ["full"] }
name = "api"
required-features = ["api", "environment"]

[[example]]
name = "regions"
required-features = ["regions"]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
7 changes: 7 additions & 0 deletions examples/regions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use flytrap::Region;

fn main() {
for (code, details) in Region::all() {
println!("{}\t{}", code, details.name);
}
}
15 changes: 13 additions & 2 deletions src/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ impl Region {
DETAILS[*self]
}

/// Iterate over all known [regions][Region].
pub fn all() -> impl Iterator<Item = (Region, RegionDetails<'static>)> {
DETAILS.iter().map(|(r, d)| (r, *d))
}

fn key(&self) -> RegionKey<'_> {
(self.city.geo.x(), self.city.geo.y(), &self.code)
}
Expand Down Expand Up @@ -330,7 +335,7 @@ impl FromStr for Region {
/// assert_eq!(city.name, "Atlanta");
/// assert_eq!(name, "Atlanta, Georgia (US)");
/// ```
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct RegionDetails<'l> {
pub code: &'l str,
Expand Down Expand Up @@ -359,7 +364,7 @@ impl RegionDetails<'static> {
}

/// Describes a city where a Fly.io [region][Region] is hosted.
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct City<'l> {
pub name: &'l str,
Expand Down Expand Up @@ -597,4 +602,10 @@ mod test {
regions
);
}

#[test]
fn all() {
assert!(Region::all().count() >= 30);
assert!(Region::all().all(|(r, d)| r.details() == d));
}
}

0 comments on commit b0b7c54

Please sign in to comment.