diff --git a/rust/src/consts.rs b/rust/src/consts.rs index 465a25b9..fe7e7169 100644 --- a/rust/src/consts.rs +++ b/rust/src/consts.rs @@ -10,25 +10,7 @@ pub const PADDING_CHAR_STR: &str = "0"; // The character set used to encode the values. pub const CODE_ALPHABET: [char; 20] = [ - '2', - '3', - '4', - '5', - '6', - '7', - '8', - '9', - 'C', - 'F', - 'G', - 'H', - 'J', - 'M', - 'P', - 'Q', - 'R', - 'V', - 'W', + '2', '3', '4', '5', '6', '7', '8', '9', 'C', 'F', 'G', 'H', 'J', 'M', 'P', 'Q', 'R', 'V', 'W', 'X', ]; @@ -71,12 +53,22 @@ pub const MIN_TRIMMABLE_CODE_LEN: usize = 6; // What to multiply latitude degrees by to get an integer value. There are three pairs representing // decimal digits, and five digits in the grid. -pub const LAT_INTEGER_MULTIPLIER: i64 = - (ENCODING_BASE * ENCODING_BASE * ENCODING_BASE * GRID_ROWS * GRID_ROWS * GRID_ROWS * - GRID_ROWS * GRID_ROWS) as i64; +pub const LAT_INTEGER_MULTIPLIER: i64 = (ENCODING_BASE + * ENCODING_BASE + * ENCODING_BASE + * GRID_ROWS + * GRID_ROWS + * GRID_ROWS + * GRID_ROWS + * GRID_ROWS) as i64; // What to multiply longitude degrees by to get an integer value. There are three pairs representing // decimal digits, and five digits in the grid. -pub const LNG_INTEGER_MULTIPLIER: i64 = - (ENCODING_BASE * ENCODING_BASE * ENCODING_BASE * GRID_COLUMNS * GRID_COLUMNS * - GRID_COLUMNS * GRID_COLUMNS * GRID_COLUMNS) as i64; +pub const LNG_INTEGER_MULTIPLIER: i64 = (ENCODING_BASE + * ENCODING_BASE + * ENCODING_BASE + * GRID_COLUMNS + * GRID_COLUMNS + * GRID_COLUMNS + * GRID_COLUMNS + * GRID_COLUMNS) as i64; diff --git a/rust/src/interface.rs b/rust/src/interface.rs index 9b1ab933..3e543f98 100644 --- a/rust/src/interface.rs +++ b/rust/src/interface.rs @@ -3,13 +3,16 @@ use std::cmp; use codearea::CodeArea; -use consts::{CODE_ALPHABET, ENCODING_BASE, GRID_CODE_LENGTH, GRID_COLUMNS, GRID_ROWS, - LATITUDE_MAX, LAT_INTEGER_MULTIPLIER, LNG_INTEGER_MULTIPLIER, LONGITUDE_MAX, - MIN_CODE_LENGTH, MAX_CODE_LENGTH, MIN_TRIMMABLE_CODE_LEN, PADDING_CHAR, - PADDING_CHAR_STR, PAIR_CODE_LENGTH, PAIR_RESOLUTIONS, SEPARATOR, SEPARATOR_POSITION}; +use consts::{ + CODE_ALPHABET, ENCODING_BASE, GRID_CODE_LENGTH, GRID_COLUMNS, GRID_ROWS, LATITUDE_MAX, + LAT_INTEGER_MULTIPLIER, LNG_INTEGER_MULTIPLIER, LONGITUDE_MAX, MAX_CODE_LENGTH, + MIN_CODE_LENGTH, MIN_TRIMMABLE_CODE_LEN, PADDING_CHAR, PADDING_CHAR_STR, PAIR_CODE_LENGTH, + PAIR_RESOLUTIONS, SEPARATOR, SEPARATOR_POSITION, +}; -use private::{clip_latitude, code_value, compute_latitude_precision, normalize_longitude, - prefix_by_reference}; +use private::{ + clip_latitude, code_value, compute_latitude_precision, normalize_longitude, prefix_by_reference, +}; /// Determines if a code is a valid Open Location Code. pub fn is_valid(code: &str) -> bool { @@ -67,9 +70,9 @@ pub fn is_valid(code: &str) -> bool { } // Validate all characters are permissible - code.chars().map(|c| c.to_ascii_uppercase()).all(|c| { - c == SEPARATOR || CODE_ALPHABET.contains(&c) - }) + code.chars() + .map(|c| c.to_ascii_uppercase()) + .all(|c| c == SEPARATOR || CODE_ALPHABET.contains(&c)) } /// Determines if a code is a valid short code. @@ -112,10 +115,10 @@ pub fn encode(pt: Point, code_length: usize) -> String { } // Convert to integers. - let mut lat_val = (((lat + LATITUDE_MAX) * LAT_INTEGER_MULTIPLIER as f64 * 1e6).round() / - 1e6f64) as i64; - let mut lng_val = (((lng + LONGITUDE_MAX) * LNG_INTEGER_MULTIPLIER as f64 * 1e6).round() / - 1e6f64) as i64; + let mut lat_val = + (((lat + LATITUDE_MAX) * LAT_INTEGER_MULTIPLIER as f64 * 1e6).round() / 1e6f64) as i64; + let mut lng_val = + (((lng + LONGITUDE_MAX) * LNG_INTEGER_MULTIPLIER as f64 * 1e6).round() / 1e6f64) as i64; // Compute the code digits. This largely ignores the requested length - it // generates either a 10 digit code, or a 15 digit code, and then truncates @@ -175,7 +178,8 @@ pub fn decode(code: &str) -> Result { if !is_full(code) { return Err(format!("Code must be a valid full code: {}", code)); } - let mut code = code.to_string() + let mut code = code + .to_string() .replace(SEPARATOR, "") .replace(PADDING_CHAR_STR, "") .to_uppercase(); @@ -208,10 +212,10 @@ pub fn decode(code: &str) -> Result { // Convert to floating point values. let lat_lo: f64 = lat as f64 / LAT_INTEGER_MULTIPLIER as f64; let lng_lo: f64 = lng as f64 / LNG_INTEGER_MULTIPLIER as f64; - let lat_hi: f64 = (lat + lat_place_val) as f64 / - (ENCODING_BASE.pow(3) * GRID_ROWS.pow(5)) as f64; - let lng_hi: f64 = (lng + lng_place_val) as f64 / - (ENCODING_BASE.pow(3) * GRID_COLUMNS.pow(5)) as f64; + let lat_hi: f64 = + (lat + lat_place_val) as f64 / (ENCODING_BASE.pow(3) * GRID_ROWS.pow(5)) as f64; + let lng_hi: f64 = + (lng + lng_place_val) as f64 / (ENCODING_BASE.pow(3) * GRID_COLUMNS.pow(5)) as f64; Ok(CodeArea::new(lat_lo, lng_lo, lat_hi, lng_hi, code.len())) } @@ -250,9 +254,7 @@ pub fn shorten(code: &str, ref_pt: Point) -> Result { // How close are the latitude and longitude to the code center. let range = (codearea.center.lat() - clip_latitude(ref_pt.lat())) .abs() - .max( - (codearea.center.lng() - normalize_longitude(ref_pt.lng())).abs(), - ); + .max((codearea.center.lng() - normalize_longitude(ref_pt.lng())).abs()); for i in 0..PAIR_RESOLUTIONS.len() - 2 { // Check if we're close enough to shorten. The range must be less than 1/2 diff --git a/rust/tests/all_test.rs b/rust/tests/all_test.rs index 879e66d5..c5fd1ef4 100644 --- a/rust/tests/all_test.rs +++ b/rust/tests/all_test.rs @@ -22,7 +22,6 @@ use csv_reader::CSVReader; /// /// We could probably take it a little further, and assert that tested was >= # tests in the file /// (allowing tests to be added, but assuming # tests will never be reduced). - #[test] fn is_valid_test() { let mut tested = 0;