what3words/
polygon.rs

1//! A Polygon is a figure defined by multiple coordinates and can be used in certain what3words API
2//! calls.
3
4use crate::coordinate::Coordinate;
5
6/// A polygon defined by at least 3 coordinates. The what3words API only supports up to 25
7/// coordinates at the moment.
8#[derive(Debug)]
9pub struct Polygon<'a> {
10    /// Vector of the coordinates of the polygon
11    pub coordinates: Vec<&'a Coordinate>,
12}
13
14impl Polygon<'_> {
15    /// Returns a string of all the coordinates of the polygon separated with a comma. As last
16    /// element, the first coordinate is added again as per the what3words API documentation.
17    pub fn to_string(&self) -> String {
18        let mut url: String = String::new();
19        for item in self.coordinates.iter() {
20            url.push_str(&format!("{},", &item.to_string()));
21        }
22        url.push_str(&self.coordinates[0].to_string());
23        url
24    }
25}