More cleanup

This commit is contained in:
Austen Adler 2023-02-28 09:20:06 -05:00
parent b620ceec9b
commit d1e6f536fb
4 changed files with 13 additions and 18 deletions

View File

@ -47,7 +47,7 @@ pub enum Error {
WrongComponentCount,
/// No number component was found
///
/// See [`Algorithm::from_str`] for the possible position of the number component
/// See [`Address::from_str`] for the possible position of the number component
#[error("No number component")]
NoNumberComponent,
/// The empty string was given to decode
@ -156,11 +156,6 @@ impl Address<'_> {
v0::UnpackedCellID::from(CellID(cellid_u64)).into()
}
// /// Decodes an address to latitude and longitude
// pub fn to_lat_lon(&self) -> (f64, f64) {
// let cellid = self.as_cell_id();
// }
// Parses an address v0 given the number and word components
fn parse_v0(number: Number, word_components: &[&str]) -> Result<Self, Error> {
if !(V0_MIN_NUMBER..=V0_MAX_NUMBER).contains(&number) {
@ -198,16 +193,17 @@ impl Address<'_> {
/// Get the address as a [`CellID`] value
#[must_use]
pub fn as_cell_id(&self) -> CellID {
pub fn as_cellid(&self) -> CellID {
match self.version {
Version::V0 => UnpackedCellID::from(self).into(),
}
}
/// Get the address as a latitude and longitude `f64` value
// TODO: Test this
#[must_use]
pub fn to_lat_lon(&self) -> (f64, f64) {
let lat_lng = LatLng::from(self.as_cell_id());
pub fn as_lat_lon(&self) -> (f64, f64) {
let lat_lng = LatLng::from(self.as_cellid());
(lat_lng.lat.deg(), lat_lng.lng.deg())
}
}

View File

@ -23,7 +23,7 @@ fn test_invalid_lat_lon() {
fn test_cellid_translation() {
for (idx, entry) in test_events().iter().enumerate() {
let addr = Address::from_lat_lon(entry.lat, entry.lon).unwrap();
let addr_cellid = addr.as_cell_id();
let addr_cellid = addr.as_cellid();
eprintln!("Testing word {idx}");

View File

@ -2,12 +2,11 @@ use std::str::FromStr;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
// fn alert(s: &str);
#[wasm_bindgen(js_namespace = console, js_name = log)]
fn log_str(s: &str);
}
// #[wasm_bindgen]
// extern "C" {
// #[wasm_bindgen(js_namespace = console, js_name = log)]
// fn log_str(s: &str);
// }
#[wasm_bindgen]
pub fn address_from_lat_lon(lat: f64, lon: f64) -> Result<String, String> {
@ -21,6 +20,6 @@ pub fn address_to_lat_lon(addr_str: &str) -> Result<Vec<f64>, String> {
this_algorithm::Address::from_str(addr_str)
.as_ref()
.map_err(|e| e.to_string())
.map(this_algorithm::Address::to_lat_lon)
.map(this_algorithm::Address::as_lat_lon)
.map(|(lat, lon)| vec![lat, lon])
}

View File

@ -39,7 +39,7 @@ async fn get_coords(address: Option<String>) -> Result<Json<Coords>, ApiError> {
Address::from_str(&address)
.as_ref()
.map_err(Into::into)
.map(Address::to_lat_lon)
.map(Address::as_lat_lon)
.map(Coords::from)
.map(Json)
}