Skip to main content

france_api_adresse/
types.rs

1use std::fmt;
2
3use serde::Deserialize;
4
5#[derive(Debug, thiserror::Error)]
6pub enum Error {
7    #[error("HTTP error: unable to access the API")]
8    HttpError(#[from] reqwest::Error),
9    #[error("Error extracting response: {0}")]
10    GetTextError(String),
11    #[error("Error unmarshalling JSON response: {0}")]
12    UnmarshalJsonError(String),
13    #[error("API error: {code} - {message}")]
14    ApiError {
15        code: u16,
16        message: String,
17        detail: Option<Vec<String>>,
18    },
19}
20
21/// A list of features (addresses) returned by the API
22#[derive(Deserialize, Debug)]
23pub struct AddressResult {
24    pub r#type: String,
25    pub features: Vec<FeatureResult>,
26}
27
28/// A feature is basically a single address + its coordinates
29#[derive(Deserialize, Debug)]
30pub struct FeatureResult {
31    pub r#type: String,
32    pub geometry: GeometryResult,
33    pub properties: PropertiesResult,
34}
35
36/// Basically the point of the address
37#[derive(Deserialize, Debug)]
38pub struct GeometryResult {
39    pub r#type: String,
40    pub coordinates: Coordinates,
41}
42
43/// Latitude and Longitude, WGS 84 format
44#[derive(Deserialize, Debug)]
45pub struct Coordinates {
46    #[serde(rename = "0")]
47    pub lon: f64,
48
49    #[serde(rename = "1")]
50    pub lat: f64,
51}
52
53/// An Address returned by the API
54#[derive(Deserialize, Debug)]
55pub struct PropertiesResult {
56    pub id: String,
57
58    pub score: f64,
59    /// Full address label
60    pub label: String,
61
62    /// X coord in the Lambert-93 projection
63    pub x: f64,
64    /// Y coord in the Lambert-93 projection
65    pub y: f64,
66
67    pub importance: f64,
68
69    /// Type of the address, e.g. "housenumber", "street", …
70    pub r#type: String,
71
72    /// name of the address, I guess housenumber + street
73    pub name: String,
74
75    pub housenumber: Option<String>,
76    pub street: Option<String>,
77    pub postcode: String,
78    pub citycode: String,
79
80    pub context: String,
81}
82
83/// Filter types for geocoding and reverse geocoding queries
84#[derive(Debug, Clone)]
85pub enum FilterType {
86    HouseNumber,
87    Street,
88    Locality,
89    Municipality,
90}
91impl fmt::Display for FilterType {
92    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93        match self {
94            FilterType::HouseNumber => write!(f, "housenumber"),
95            FilterType::Street => write!(f, "street"),
96            FilterType::Locality => write!(f, "locality"),
97            FilterType::Municipality => write!(f, "municipality"),
98        }
99    }
100}
101#[derive(Debug, serde::Deserialize)]
102pub(crate) struct ApiErrorResponse {
103    pub code: u16,
104    pub message: String,
105    pub detail: Option<Vec<String>>,
106}