arcgis 0.1.3

Type-safe Rust SDK for the ArcGIS REST API with compile-time guarantees
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! Error types for the ArcGIS SDK.

/// HTTP request error wrapper.
#[derive(Debug, derive_more::Display, derive_more::Error, derive_getters::Getters)]
#[display("HTTP request failed: {}", source)]
pub struct HttpError {
    /// The underlying reqwest error.
    source: reqwest::Error,
    /// Line number where the error occurred.
    line: u32,
    /// File where the error occurred.
    file: &'static str,
}

impl HttpError {
    /// Creates a new HTTP error with caller location.
    #[track_caller]
    pub fn new(source: reqwest::Error) -> Self {
        let loc = std::panic::Location::caller();
        Self {
            source,
            line: loc.line(),
            file: loc.file(),
        }
    }
}

impl From<reqwest::Error> for HttpError {
    #[track_caller]
    fn from(source: reqwest::Error) -> Self {
        Self::new(source)
    }
}

/// JSON serialization/deserialization error wrapper.
#[derive(Debug, derive_more::Display, derive_more::Error, derive_getters::Getters)]
#[display("JSON error: {}", source)]
pub struct JsonError {
    /// The underlying serde_json error.
    source: serde_json::Error,
    /// Line number where the error occurred.
    line: u32,
    /// File where the error occurred.
    file: &'static str,
}

impl JsonError {
    /// Creates a new JSON error with caller location.
    #[track_caller]
    pub fn new(source: serde_json::Error) -> Self {
        let loc = std::panic::Location::caller();
        Self {
            source,
            line: loc.line(),
            file: loc.file(),
        }
    }
}

impl From<serde_json::Error> for JsonError {
    #[track_caller]
    fn from(source: serde_json::Error) -> Self {
        Self::new(source)
    }
}

/// URL parsing error wrapper.
#[derive(Debug, derive_more::Display, derive_more::Error, derive_getters::Getters)]
#[display("Invalid URL: {}", source)]
pub struct UrlError {
    /// The underlying url::ParseError.
    source: url::ParseError,
    /// Line number where the error occurred.
    line: u32,
    /// File where the error occurred.
    file: &'static str,
}

impl UrlError {
    /// Creates a new URL error with caller location.
    #[track_caller]
    pub fn new(source: url::ParseError) -> Self {
        let loc = std::panic::Location::caller();
        Self {
            source,
            line: loc.line(),
            file: loc.file(),
        }
    }
}

impl From<url::ParseError> for UrlError {
    #[track_caller]
    fn from(source: url::ParseError) -> Self {
        Self::new(source)
    }
}

/// File I/O error wrapper.
#[derive(Debug, derive_more::Display, derive_more::Error, derive_getters::Getters)]
#[display("I/O error: {}", source)]
pub struct IoError {
    /// The underlying std::io::Error.
    source: std::io::Error,
    /// Line number where the error occurred.
    line: u32,
    /// File where the error occurred.
    file: &'static str,
}

impl IoError {
    /// Creates a new I/O error with caller location.
    #[track_caller]
    pub fn new(source: std::io::Error) -> Self {
        let loc = std::panic::Location::caller();
        Self {
            source,
            line: loc.line(),
            file: loc.file(),
        }
    }
}

impl From<std::io::Error> for IoError {
    #[track_caller]
    fn from(source: std::io::Error) -> Self {
        Self::new(source)
    }
}

/// URL-encoded form serialization error wrapper.
#[derive(Debug, derive_more::Display, derive_more::Error, derive_getters::Getters)]
#[display("URL encoding error: {}", source)]
pub struct UrlEncodedError {
    /// The underlying serde_urlencoded error.
    source: serde_urlencoded::ser::Error,
    /// Line number where the error occurred.
    line: u32,
    /// File where the error occurred.
    file: &'static str,
}

impl UrlEncodedError {
    /// Creates a new URL encoding error with caller location.
    #[track_caller]
    pub fn new(source: serde_urlencoded::ser::Error) -> Self {
        let loc = std::panic::Location::caller();
        Self {
            source,
            line: loc.line(),
            file: loc.file(),
        }
    }
}

impl From<serde_urlencoded::ser::Error> for UrlEncodedError {
    #[track_caller]
    fn from(source: serde_urlencoded::ser::Error) -> Self {
        Self::new(source)
    }
}

/// Environment variable error wrapper.
#[derive(Debug, derive_more::Display, derive_more::Error, derive_getters::Getters)]
#[display("Environment variable error: {}", source)]
pub struct EnvError {
    /// The underlying std::env::VarError.
    source: std::env::VarError,
    /// Line number where the error occurred.
    line: u32,
    /// File where the error occurred.
    file: &'static str,
}

impl EnvError {
    /// Creates a new environment variable error with caller location.
    #[track_caller]
    pub fn new(source: std::env::VarError) -> Self {
        let loc = std::panic::Location::caller();
        Self {
            source,
            line: loc.line(),
            file: loc.file(),
        }
    }
}

impl From<std::env::VarError> for EnvError {
    #[track_caller]
    fn from(source: std::env::VarError) -> Self {
        Self::new(source)
    }
}

/// Builder error wrapper for derive_builder errors.
#[derive(Debug, derive_more::Display, derive_more::Error, derive_getters::Getters)]
#[display("Builder error: {}", message)]
pub struct BuilderError {
    /// Error message from the builder.
    message: String,
    /// Line number where the error occurred.
    line: u32,
    /// File where the error occurred.
    file: &'static str,
}

impl BuilderError {
    /// Creates a new builder error with caller location.
    #[track_caller]
    pub fn new(message: impl Into<String>) -> Self {
        let loc = std::panic::Location::caller();
        Self {
            message: message.into(),
            line: loc.line(),
            file: loc.file(),
        }
    }
}

impl From<String> for BuilderError {
    #[track_caller]
    fn from(message: String) -> Self {
        Self::new(message)
    }
}

/// Specific error conditions for the ArcGIS SDK.
#[derive(Debug, derive_more::Display, derive_more::From)]
pub enum ErrorKind {
    /// HTTP request error.
    #[display("{}", _0)]
    #[from]
    Http(HttpError),

    /// Authentication error.
    #[display("Authentication failed: {}", _0)]
    Auth(String),

    /// ArcGIS API error with code and message.
    #[display("ArcGIS API error {}: {}", code, message)]
    Api {
        /// Error code from the API.
        code: i32,
        /// Error message from the API.
        message: String,
    },

    /// JSON serialization/deserialization error.
    #[display("{}", _0)]
    #[from]
    Json(JsonError),

    /// URL parsing error.
    #[display("{}", _0)]
    #[from]
    Url(UrlError),

    /// File I/O error.
    #[display("{}", _0)]
    #[from]
    Io(IoError),

    /// URL-encoded form serialization error.
    #[display("{}", _0)]
    #[from]
    UrlEncoded(UrlEncodedError),

    /// Environment variable error.
    #[display("{}", _0)]
    #[from]
    Env(EnvError),

    /// Builder error from derive_builder.
    #[display("{}", _0)]
    #[from]
    Builder(BuilderError),

    /// OAuth error.
    #[display("OAuth error: {}", _0)]
    OAuth(String),

    /// Geometry conversion error.
    #[display("Geometry conversion error: {}", _0)]
    Geometry(String),

    /// Validation error for invalid input.
    #[display("Validation error: {}", _0)]
    Validation(String),

    /// Generic error for other cases.
    #[display("{}", _0)]
    Other(String),
}

/// Macro to generate bridge From implementations for external errors.
///
/// This creates the conversion chain: ExternalError → WrapperError → ErrorKind → Error
///
/// # Example
/// ```ignore
/// bridge_error!(reqwest::Error => HttpError);
/// // Generates:
/// // impl From<reqwest::Error> for ErrorKind {
/// //     #[track_caller]
/// //     fn from(err: reqwest::Error) -> Self {
/// //         HttpError::from(err).into()
/// //     }
/// // }
/// ```
macro_rules! bridge_error {
    ($external:ty => $wrapper:ty) => {
        impl From<$external> for ErrorKind {
            #[track_caller]
            fn from(err: $external) -> Self {
                <$wrapper>::from(err).into()
            }
        }
    };
}

// Bridge From implementations to chain external errors through wrappers
bridge_error!(reqwest::Error => HttpError);
bridge_error!(serde_json::Error => JsonError);
bridge_error!(url::ParseError => UrlError);
bridge_error!(std::io::Error => IoError);
bridge_error!(serde_urlencoded::ser::Error => UrlEncodedError);
bridge_error!(std::env::VarError => EnvError);

/// The main error type for the ArcGIS SDK.
///
/// This type wraps all error conditions and provides automatic conversion
/// from underlying error types through the `?` operator.
#[derive(Debug, derive_more::Display)]
#[display("ArcGIS SDK: {}", _0)]
pub struct Error(Box<ErrorKind>);

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match &*self.0 {
            ErrorKind::Http(e) => Some(e.source()),
            ErrorKind::Json(e) => Some(e.source()),
            ErrorKind::Url(e) => Some(e.source()),
            ErrorKind::Io(e) => Some(e.source()),
            ErrorKind::UrlEncoded(e) => Some(e.source()),
            ErrorKind::Env(e) => Some(e.source()),
            _ => None,
        }
    }
}

impl Error {
    /// Returns a reference to the underlying error kind.
    pub fn kind(&self) -> &ErrorKind {
        &self.0
    }
}

/// Macro to implement From<SourceError> for Error.
///
/// This creates the full conversion chain: SourceError → ErrorKind → Error
/// with proper location tracking and error logging.
///
/// # Example
/// ```ignore
/// error_from!(reqwest::Error);
/// // Generates:
/// // impl From<reqwest::Error> for Error {
/// //     #[track_caller]
/// //     fn from(err: reqwest::Error) -> Self {
/// //         let kind = ErrorKind::from(err);
/// //         tracing::error!(error_kind = %kind, "Error created");
/// //         Self(Box::new(kind))
/// //     }
/// // }
/// ```
macro_rules! error_from {
    ($source:ty) => {
        impl From<$source> for Error {
            #[track_caller]
            fn from(err: $source) -> Self {
                let kind = ErrorKind::from(err);
                tracing::error!(error_kind = %kind, "Error created");
                Self(Box::new(kind))
            }
        }
    };
}

// Implement From<ErrorKind> for Error
impl From<ErrorKind> for Error {
    #[track_caller]
    fn from(kind: ErrorKind) -> Self {
        tracing::error!(error_kind = %kind, "Error created");
        Self(Box::new(kind))
    }
}

// Implement From for all external error types
error_from!(reqwest::Error);
error_from!(serde_json::Error);
error_from!(url::ParseError);
error_from!(std::io::Error);
error_from!(serde_urlencoded::ser::Error);
error_from!(std::env::VarError);
error_from!(BuilderError);