Skip to main content

overpass_lib/
overpass.rs

1use std::{
2    collections::HashMap,
3    fmt::{Display, Formatter, Result as FResult},
4};
5use serde::Deserialize;
6use crate::{Element, OverpassQLError, Query};
7
8mod server;
9pub use server::*;
10
11/// An error returned when a request to evaluate a [Query] fails.
12#[derive(Debug)]
13pub enum OverpassError {
14    /// There was an error serializing the query.
15    Query(OverpassQLError),
16    /// There was an error communicating with the Overpass server.
17    Request(reqwest::Error),
18    /// There was an error parsing the response from the Overpass server.
19    Parse(serde_json::Error),
20    /// An unknown error occurred.
21    Other(String),
22}
23impl Display for OverpassError {
24    fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
25        match self {
26            Self::Query(e) => write!(f, "{e}"),
27            Self::Request(e) => write!(f, "{e}"),
28            Self::Parse(e) => write!(f, "Deserialization error: {e}"),
29            Self::Other(e) => write!(f, "Error from API provider: {e}"),
30        }
31    }
32}
33impl std::error::Error for OverpassError {}
34
35/// The data returned from an [Overpass] [Query] evaluation.
36#[derive(Debug, Clone, PartialEq, Deserialize)]
37pub struct OverpassResult {
38    /// The [Element]s in the query set.
39    pub elements: Vec<Element>,
40
41    /// Miscellaneous information provided by the API server, such as the API version and a timestamp.
42    #[serde(flatten)]
43    pub other_fields: HashMap<String, serde_json::Value>,
44}
45
46/// Can retrieve [Element] data from OpenStreetMap that matches the provided [Query] set.
47pub trait Overpass {
48    /// An async method that evaluates a [Query] against the map database and returns the
49    /// resulting [Element]s.
50    fn evaluate(&self, query: &Query<'_>) -> impl std::future::Future<Output = Result<OverpassResult, OverpassError>> + Send;
51}