Skip to main content

overpass_lib/query/
overpassql.rs

1use std::fmt::{Display, Formatter, Write, Result as FResult, Error as FmtError};
2use crate::Namer;
3
4/// An error returned when a [Query](crate::Query) cannot produce a valid OverpassQL query string.
5#[derive(Debug, Clone)]
6pub enum OverpassQLError {
7    /// Failed to write to the string.
8    Format,
9    /// This query contains sets with mutual dependencies, making evaluation impossible.
10    CircularReference,
11}
12
13impl Display for OverpassQLError {
14    fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
15        match self {
16            Self::Format => write!(f, "Format"),
17            Self::CircularReference => write!(f, "Circular reference"),
18        }
19    }
20}
21
22impl std::error::Error for OverpassQLError {}
23
24impl From<FmtError> for OverpassQLError {
25    fn from(_: FmtError) -> Self {
26        Self::Format
27    }
28}
29impl Into<FmtError> for OverpassQLError {
30    fn into(self) -> FmtError {
31        FmtError
32    }
33}
34
35/// Implementers can be represented as a full or partial OverpassQL query.
36pub trait OverpassQL {
37    /// Write the OverpassQL representation to the given [Write] object.
38    fn fmt_oql(&self, f: &mut impl Write) -> Result<(), OverpassQLError>;
39
40    /// Write the OverpassQL representation into a String, panicking if the conversion fails.
41    fn to_oql(&self) -> String {
42        let mut buf = String::new();
43
44        // Bypass format_args!() to avoid write_str with zero-length strs
45        OverpassQL::fmt_oql(self, &mut buf)
46            .expect("an OverpassQL implementation returned an error unexpectedly");
47        buf
48    }
49}
50
51
52pub(crate) trait OverpassQLNamed<'a> {
53    #[allow(unused_variables)]
54    fn fmt_oql_named<'b, 'c>(&'b self, f: &mut impl Write, namer: &mut Namer<'a, 'c>)
55    -> Result<(), OverpassQLError>
56    where 'b: 'c;
57}