use serde::{Deserialize, Serialize};
use super::Location;
#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)]
pub struct Response {
loc: String,
postal: String,
}
impl From<Response> for Location {
fn from(response: Response) -> Self {
let (lat, lon) = response.loc.split_once(',').unwrap();
Self {
loc: response.loc.clone(),
latitude: lat.to_string(),
longitude: lon.to_string(),
postal_code: response.postal,
}
}
}
pub struct Client;
impl Client {
pub fn new() -> Self {
Self {}
}
}
impl crate::api::Fetchable<Response, Location> for Client {
fn url(&self) -> &'static str {
"https://ipinfo.io/json"
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_location_from() {
let lat = "35.12345";
let long = "-80.54321";
let postal_code = "10001";
let loc = format!("{lat},{long}");
let response = Response {
loc: loc.to_string(),
postal: postal_code.to_string(),
};
let location = Location::from(response);
assert_eq!(location.loc, loc);
assert_eq!(location.latitude, lat);
assert_eq!(location.longitude, long);
assert_eq!(location.postal_code, postal_code);
}
}