Skip to content

Commit 3e31b73

Browse files
committed
implement Display for Coords
1 parent de7478f commit 3e31b73

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

src/common/geo.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
//! Functionality for working with geographical data,
33
//! including country, city, region, and coordinates. It is used to enrich
44
//! server information with location-based details.
5-
use anyhow::Error;
65
use phf::phf_map;
76
use quake_serverinfo::Settings;
87
use std::hash::{Hash, Hasher};
@@ -122,6 +121,12 @@ impl Coords {
122121
}
123122
}
124123

124+
impl core::fmt::Display for Coords {
125+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
126+
write!(f, "{:.6},{:.6}", self.lat, self.lng)
127+
}
128+
}
129+
125130
impl Hash for Coords {
126131
fn hash<H: Hasher>(&self, state: &mut H) {
127132
state.write_u64(self.lat.to_bits());
@@ -132,17 +137,17 @@ impl Hash for Coords {
132137
impl Eq for Coords {}
133138

134139
impl TryFrom<&str> for Coords {
135-
type Error = Error;
140+
type Error = anyhow::Error;
136141

137142
fn try_from(value: &str) -> Result<Self, Self::Error> {
138143
let parts = value.split_once(',');
139144

140145
if let Some((lat_str, lng_str)) = parts {
141146
let lat = lat_str.trim().parse::<f64>()?;
142-
let long = lng_str.trim().parse::<f64>()?;
143-
Ok(Self { lat, lng: long })
147+
let lng = lng_str.trim().parse::<f64>()?;
148+
Ok(Self { lat, lng })
144149
} else {
145-
Err(Error::msg("Invalid coordinate format"))
150+
Err(anyhow::anyhow!("Invalid coordinate format"))
146151
}
147152
}
148153
}
@@ -454,6 +459,8 @@ pub mod tests {
454459
assert_eq!(coords.lng(), -74.0060);
455460
assert_eq!(Coords::new(40.7128, -74.0060), coords);
456461

462+
assert_eq!(coords.to_string().as_str(), "40.712800,-74.006000");
463+
457464
Ok(())
458465
}
459466

0 commit comments

Comments
 (0)