nomi4m 0.1.0-rc.1

A rust client for nominatim.
Documentation
use async_trait::async_trait;
use serde::Deserialize;

use crate::{Nominatim, Result};

/// Different formats to get the response of [`Nominatim::status`] back in.
#[async_trait]
pub trait StatusFormat: Sized {
    /// Get the actual status of the server, in this format.
    async fn status(client: &Nominatim, email: &str) -> Result<Self>;
}

#[async_trait]
impl StatusFormat for String {
    async fn status(client: &Nominatim, email: &str) -> Result<Self> {
        Ok(client
            .client
            .get(client.status_url()?)
            .query(&[("format", "text"), ("email", email)])
            .send()
            .await?
            .error_for_status()?
            .text()
            .await?)
    }
}

/// Get the [`Nominatim::status`] back in [json format](https://nominatim.org/release-docs/latest/api/Status/#json-format).
#[derive(Debug, Clone, Deserialize)]
pub struct StatusJson {
    status: u32,
    message: String,
    #[serde(default)]
    data_updated: Option<String>,
    #[serde(default)]
    software_version: Option<String>,
    #[serde(default)]
    database_version: Option<String>,
}

#[allow(missing_docs, reason = "self-explanatory getters")]
impl StatusJson {
    pub fn status(&self) -> u32 {
        self.status
    }

    pub fn message(&self) -> &str {
        &self.message
    }

    pub fn data_updated(&self) -> Option<&str> {
        self.data_updated.as_deref()
    }

    pub fn software_version(&self) -> Option<&str> {
        self.software_version.as_deref()
    }

    pub fn database_version(&self) -> Option<&str> {
        self.database_version.as_deref()
    }
}

#[async_trait]
impl StatusFormat for StatusJson {
    async fn status(client: &Nominatim, email: &str) -> Result<Self> {
        Ok(client
            .client
            .get(client.status_url()?)
            .query(&[("format", "json"), ("email", email)])
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?)
    }
}