use crate::Client;
use crate::errors::{Error, Result};
use crate::methods::accepted;
use crate::methods::messages::geo_point;
use crate::raw::functions;
#[must_use = "a request does nothing until it is awaited"]
pub struct SetBusinessLocation<'a> {
client: &'a Client,
address: String,
location: Option<(f64, f64)>,
}
impl Client {
pub fn set_business_location(&self, address: impl Into<String>) -> SetBusinessLocation<'_> {
SetBusinessLocation { client: self, address: address.into(), location: None }
}
}
impl SetBusinessLocation<'_> {
pub fn location(mut self, latitude: f64, longitude: f64) -> Self {
self.location = Some((latitude, longitude));
self
}
async fn send(self) -> Result<()> {
let address = self.address;
if address.is_empty() || address.chars().count() > 96 {
return Err(Error::InvalidArgument("a business address takes 1 to 96 characters"));
}
let on_earth = |(lat, long): (f64, f64)| (-90.0..=90.0).contains(&lat) && (-180.0..=180.0).contains(&long);
if self.location.is_some_and(|l| !on_earth(l)) {
return Err(Error::InvalidArgument("latitude must be within 90 and longitude within 180 degrees"));
}
let geo_point = self.location.map(|(lat, long)| geo_point(lat, long, None));
let request = functions::account::UpdateBusinessLocation { geo_point, address: Some(address) };
accepted(self.client.invoke(&request).await?)
}
}
into_future!(SetBusinessLocation, ());