oxigdal-wasm 0.1.4

WebAssembly bindings for OxiGDAL - Browser-based geospatial processing
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
//! WASM-specific error types and handling
//!
//! This module provides comprehensive error handling for WebAssembly operations,
//! including fetch errors, canvas errors, worker errors, and tile cache errors.

use oxigdal_core::error::OxiGdalError;
use std::fmt;
use wasm_bindgen::prelude::*;

/// Result type for WASM operations
pub type WasmResult<T> = std::result::Result<T, WasmError>;

/// Comprehensive WASM error types
#[derive(Debug, Clone)]
pub enum WasmError {
    /// Fetch API errors
    Fetch(FetchError),

    /// Canvas rendering errors
    Canvas(CanvasError),

    /// Web Worker errors
    Worker(WorkerError),

    /// Tile cache errors
    TileCache(TileCacheError),

    /// JavaScript interop errors
    JsInterop(JsInteropError),

    /// OxiGDAL core errors
    OxiGdal(String),

    /// Invalid operation
    InvalidOperation {
        /// Operation description
        operation: String,
        /// Reason for invalidity
        reason: String,
    },

    /// Resource not found
    NotFound {
        /// Resource type
        resource: String,
        /// Resource identifier
        identifier: String,
    },

    /// Out of memory
    OutOfMemory {
        /// Requested size in bytes
        requested: usize,
        /// Available size in bytes
        available: Option<usize>,
    },

    /// Timeout error
    Timeout {
        /// Operation that timed out
        operation: String,
        /// Duration in milliseconds
        duration_ms: u64,
    },

    /// Format error
    Format {
        /// Expected format
        expected: String,
        /// Actual format
        actual: String,
    },
}

/// Fetch-related errors
#[derive(Debug, Clone)]
pub enum FetchError {
    /// Network request failed
    NetworkFailure {
        /// URL that failed
        url: String,
        /// Error message
        message: String,
    },

    /// HTTP error response
    HttpError {
        /// HTTP status code
        status: u16,
        /// Status text
        status_text: String,
        /// URL
        url: String,
    },

    /// CORS error
    CorsError {
        /// URL
        url: String,
        /// Details
        details: String,
    },

    /// Range request not supported
    RangeNotSupported {
        /// URL
        url: String,
    },

    /// Response parsing failed
    ParseError {
        /// Expected type
        expected: String,
        /// Error details
        message: String,
    },

    /// Request timeout
    Timeout {
        /// URL
        url: String,
        /// Timeout duration in milliseconds
        timeout_ms: u64,
    },

    /// Retry limit exceeded
    RetryLimitExceeded {
        /// URL
        url: String,
        /// Number of attempts
        attempts: u32,
    },

    /// Invalid response size
    InvalidSize {
        /// Expected size
        expected: u64,
        /// Actual size
        actual: u64,
    },
}

/// Canvas rendering errors
#[derive(Debug, Clone)]
pub enum CanvasError {
    /// Failed to create ImageData
    ImageDataCreation {
        /// Width
        width: u32,
        /// Height
        height: u32,
        /// Error message
        message: String,
    },

    /// Invalid dimensions
    InvalidDimensions {
        /// Width
        width: u32,
        /// Height
        height: u32,
        /// Reason
        reason: String,
    },

    /// Color space conversion failed
    ColorSpaceConversion {
        /// Source color space
        from: String,
        /// Target color space
        to: String,
        /// Error details
        details: String,
    },

    /// Buffer size mismatch
    BufferSizeMismatch {
        /// Expected size
        expected: usize,
        /// Actual size
        actual: usize,
    },

    /// Canvas context unavailable
    ContextUnavailable {
        /// Context type
        context_type: String,
    },

    /// Rendering operation failed
    RenderingFailed {
        /// Operation
        operation: String,
        /// Error message
        message: String,
    },

    /// Invalid parameter provided
    InvalidParameter(String),
}

/// Web Worker errors
#[derive(Debug, Clone)]
pub enum WorkerError {
    /// Worker creation failed
    CreationFailed {
        /// Error message
        message: String,
    },

    /// Worker terminated unexpectedly
    Terminated {
        /// Worker ID
        worker_id: u32,
    },

    /// Message posting failed
    PostMessageFailed {
        /// Worker ID
        worker_id: u32,
        /// Error details
        message: String,
    },

    /// Worker pool exhausted
    PoolExhausted {
        /// Pool size
        pool_size: usize,
        /// Pending jobs
        pending_jobs: usize,
    },

    /// Worker response timeout
    ResponseTimeout {
        /// Worker ID
        worker_id: u32,
        /// Job ID
        job_id: u64,
        /// Timeout duration in milliseconds
        timeout_ms: u64,
    },

    /// Invalid worker response
    InvalidResponse {
        /// Expected response type
        expected: String,
        /// Actual response
        actual: String,
    },
}

/// Tile cache errors
#[derive(Debug, Clone)]
pub enum TileCacheError {
    /// Cache miss
    Miss {
        /// Tile key
        key: String,
    },

    /// Cache full
    Full {
        /// Current size in bytes
        current_size: usize,
        /// Maximum size in bytes
        max_size: usize,
    },

    /// Invalid tile coordinates
    InvalidCoordinates {
        /// Level
        level: u32,
        /// X coordinate
        x: u32,
        /// Y coordinate
        y: u32,
        /// Reason
        reason: String,
    },

    /// Tile size mismatch
    SizeMismatch {
        /// Expected size
        expected: usize,
        /// Actual size
        actual: usize,
    },

    /// Eviction failed
    EvictionFailed {
        /// Error details
        message: String,
    },
}

/// JavaScript interop errors
#[derive(Debug, Clone)]
pub enum JsInteropError {
    /// Type conversion failed
    TypeConversion {
        /// Expected type
        expected: String,
        /// Actual type
        actual: String,
    },

    /// Property access failed
    PropertyAccess {
        /// Property name
        property: String,
        /// Error message
        message: String,
    },

    /// Function call failed
    FunctionCall {
        /// Function name
        function: String,
        /// Error message
        message: String,
    },

    /// Promise rejection
    PromiseRejection {
        /// Promise description
        promise: String,
        /// Rejection reason
        reason: String,
    },

    /// Invalid JsValue
    InvalidJsValue {
        /// Expected type
        expected: String,
        /// Error details
        details: String,
    },
}

impl fmt::Display for WasmError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Fetch(e) => write!(f, "Fetch error: {e}"),
            Self::Canvas(e) => write!(f, "Canvas error: {e}"),
            Self::Worker(e) => write!(f, "Worker error: {e}"),
            Self::TileCache(e) => write!(f, "Tile cache error: {e}"),
            Self::JsInterop(e) => write!(f, "JS interop error: {e}"),
            Self::OxiGdal(msg) => write!(f, "OxiGDAL error: {msg}"),
            Self::InvalidOperation { operation, reason } => {
                write!(f, "Invalid operation '{operation}': {reason}")
            }
            Self::NotFound {
                resource,
                identifier,
            } => {
                write!(f, "{resource} not found: {identifier}")
            }
            Self::OutOfMemory {
                requested,
                available,
            } => {
                if let Some(avail) = available {
                    write!(
                        f,
                        "Out of memory: requested {requested} bytes, {avail} available"
                    )
                } else {
                    write!(f, "Out of memory: requested {requested} bytes")
                }
            }
            Self::Timeout {
                operation,
                duration_ms,
            } => {
                write!(f, "Operation '{operation}' timed out after {duration_ms}ms")
            }
            Self::Format { expected, actual } => {
                write!(f, "Format error: expected {expected}, got {actual}")
            }
        }
    }
}

impl fmt::Display for FetchError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NetworkFailure { url, message } => {
                write!(f, "Network failure for {url}: {message}")
            }
            Self::HttpError {
                status,
                status_text,
                url,
            } => {
                write!(f, "HTTP {status} {status_text} for {url}")
            }
            Self::CorsError { url, details } => {
                write!(f, "CORS error for {url}: {details}")
            }
            Self::RangeNotSupported { url } => {
                write!(f, "Range requests not supported for {url}")
            }
            Self::ParseError { expected, message } => {
                write!(f, "Parse error: expected {expected}, {message}")
            }
            Self::Timeout { url, timeout_ms } => {
                write!(f, "Request to {url} timed out after {timeout_ms}ms")
            }
            Self::RetryLimitExceeded { url, attempts } => {
                write!(
                    f,
                    "Retry limit exceeded for {url} after {attempts} attempts"
                )
            }
            Self::InvalidSize { expected, actual } => {
                write!(
                    f,
                    "Invalid response size: expected {expected}, got {actual}"
                )
            }
        }
    }
}

impl fmt::Display for CanvasError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::ImageDataCreation {
                width,
                height,
                message,
            } => {
                write!(f, "Failed to create ImageData {width}x{height}: {message}")
            }
            Self::InvalidDimensions {
                width,
                height,
                reason,
            } => {
                write!(f, "Invalid dimensions {width}x{height}: {reason}")
            }
            Self::ColorSpaceConversion { from, to, details } => {
                write!(f, "Color space conversion {from} -> {to} failed: {details}")
            }
            Self::BufferSizeMismatch { expected, actual } => {
                write!(f, "Buffer size mismatch: expected {expected}, got {actual}")
            }
            Self::ContextUnavailable { context_type } => {
                write!(f, "Canvas context '{context_type}' unavailable")
            }
            Self::RenderingFailed { operation, message } => {
                write!(f, "Rendering operation '{operation}' failed: {message}")
            }
            Self::InvalidParameter(msg) => {
                write!(f, "Invalid parameter: {msg}")
            }
        }
    }
}

impl fmt::Display for WorkerError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::CreationFailed { message } => {
                write!(f, "Worker creation failed: {message}")
            }
            Self::Terminated { worker_id } => {
                write!(f, "Worker {worker_id} terminated unexpectedly")
            }
            Self::PostMessageFailed { worker_id, message } => {
                write!(f, "Failed to post message to worker {worker_id}: {message}")
            }
            Self::PoolExhausted {
                pool_size,
                pending_jobs,
            } => {
                write!(
                    f,
                    "Worker pool exhausted: {pool_size} workers, {pending_jobs} pending jobs"
                )
            }
            Self::ResponseTimeout {
                worker_id,
                job_id,
                timeout_ms,
            } => {
                write!(
                    f,
                    "Worker {worker_id} job {job_id} timed out after {timeout_ms}ms"
                )
            }
            Self::InvalidResponse { expected, actual } => {
                write!(
                    f,
                    "Invalid worker response: expected {expected}, got {actual}"
                )
            }
        }
    }
}

impl fmt::Display for TileCacheError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Miss { key } => {
                write!(f, "Cache miss for tile {key}")
            }
            Self::Full {
                current_size,
                max_size,
            } => {
                write!(f, "Cache full: {current_size}/{max_size} bytes")
            }
            Self::InvalidCoordinates {
                level,
                x,
                y,
                reason,
            } => {
                write!(f, "Invalid tile coordinates ({level}, {x}, {y}): {reason}")
            }
            Self::SizeMismatch { expected, actual } => {
                write!(f, "Tile size mismatch: expected {expected}, got {actual}")
            }
            Self::EvictionFailed { message } => {
                write!(f, "Cache eviction failed: {message}")
            }
        }
    }
}

impl fmt::Display for JsInteropError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::TypeConversion { expected, actual } => {
                write!(
                    f,
                    "Type conversion failed: expected {expected}, got {actual}"
                )
            }
            Self::PropertyAccess { property, message } => {
                write!(f, "Property access failed for '{property}': {message}")
            }
            Self::FunctionCall { function, message } => {
                write!(f, "Function call failed for '{function}': {message}")
            }
            Self::PromiseRejection { promise, reason } => {
                write!(f, "Promise rejected for '{promise}': {reason}")
            }
            Self::InvalidJsValue { expected, details } => {
                write!(f, "Invalid JsValue: expected {expected}, {details}")
            }
        }
    }
}

impl std::error::Error for WasmError {}
impl std::error::Error for FetchError {}
impl std::error::Error for CanvasError {}
impl std::error::Error for WorkerError {}
impl std::error::Error for TileCacheError {}
impl std::error::Error for JsInteropError {}

/// Convert `WasmError` to `JsValue` for WASM bindings
impl From<WasmError> for JsValue {
    fn from(err: WasmError) -> Self {
        JsValue::from_str(&err.to_string())
    }
}

/// Convert `OxiGdalError` to `WasmError`
impl From<OxiGdalError> for WasmError {
    fn from(err: OxiGdalError) -> Self {
        Self::OxiGdal(err.to_string())
    }
}

/// Convert `FetchError` to `WasmError`
impl From<FetchError> for WasmError {
    fn from(err: FetchError) -> Self {
        Self::Fetch(err)
    }
}

/// Convert `CanvasError` to `WasmError`
impl From<CanvasError> for WasmError {
    fn from(err: CanvasError) -> Self {
        Self::Canvas(err)
    }
}

/// Convert `WorkerError` to `WasmError`
impl From<WorkerError> for WasmError {
    fn from(err: WorkerError) -> Self {
        Self::Worker(err)
    }
}

/// Convert `TileCacheError` to `WasmError`
impl From<TileCacheError> for WasmError {
    fn from(err: TileCacheError) -> Self {
        Self::TileCache(err)
    }
}

/// Convert `JsInteropError` to `WasmError`
impl From<JsInteropError> for WasmError {
    fn from(err: JsInteropError) -> Self {
        Self::JsInterop(err)
    }
}

/// Helper to convert `JsValue` to `WasmError`
#[allow(dead_code)]
pub fn js_to_wasm_error(js_val: JsValue, context: &str) -> WasmError {
    let message = if let Some(s) = js_val.as_string() {
        s
    } else {
        format!("{js_val:?}")
    };

    WasmError::JsInterop(JsInteropError::FunctionCall {
        function: context.to_string(),
        message,
    })
}

/// Helper to convert errors to `JsValue`
#[allow(dead_code)]
pub fn to_js_value<E: std::fmt::Display>(err: E) -> JsValue {
    JsValue::from_str(&err.to_string())
}

/// Error builder for common patterns
#[allow(dead_code)]
pub struct WasmErrorBuilder;

#[allow(dead_code)]
impl WasmErrorBuilder {
    /// Create a fetch network failure error
    pub fn fetch_network(url: impl Into<String>, message: impl Into<String>) -> WasmError {
        WasmError::Fetch(FetchError::NetworkFailure {
            url: url.into(),
            message: message.into(),
        })
    }

    /// Create a fetch HTTP error
    pub fn fetch_http(
        status: u16,
        status_text: impl Into<String>,
        url: impl Into<String>,
    ) -> WasmError {
        WasmError::Fetch(FetchError::HttpError {
            status,
            status_text: status_text.into(),
            url: url.into(),
        })
    }

    /// Create a canvas ImageData creation error
    pub fn canvas_image_data(width: u32, height: u32, message: impl Into<String>) -> WasmError {
        WasmError::Canvas(CanvasError::ImageDataCreation {
            width,
            height,
            message: message.into(),
        })
    }

    /// Create a worker creation error
    pub fn worker_creation(message: impl Into<String>) -> WasmError {
        WasmError::Worker(WorkerError::CreationFailed {
            message: message.into(),
        })
    }

    /// Create a tile cache miss error
    pub fn cache_miss(key: impl Into<String>) -> WasmError {
        WasmError::TileCache(TileCacheError::Miss { key: key.into() })
    }

    /// Create an invalid operation error
    pub fn invalid_op(operation: impl Into<String>, reason: impl Into<String>) -> WasmError {
        WasmError::InvalidOperation {
            operation: operation.into(),
            reason: reason.into(),
        }
    }

    /// Create a not found error
    pub fn not_found(resource: impl Into<String>, identifier: impl Into<String>) -> WasmError {
        WasmError::NotFound {
            resource: resource.into(),
            identifier: identifier.into(),
        }
    }

    /// Create an out of memory error
    pub fn out_of_memory(requested: usize, available: Option<usize>) -> WasmError {
        WasmError::OutOfMemory {
            requested,
            available,
        }
    }

    /// Create a timeout error
    pub fn timeout(operation: impl Into<String>, duration_ms: u64) -> WasmError {
        WasmError::Timeout {
            operation: operation.into(),
            duration_ms,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_error_display() {
        let err = WasmErrorBuilder::fetch_network("https://example.com", "Connection refused");
        assert!(err.to_string().contains("Network failure"));
        assert!(err.to_string().contains("example.com"));
    }

    #[test]
    fn test_error_conversion() {
        let fetch_err = FetchError::HttpError {
            status: 404,
            status_text: "Not Found".to_string(),
            url: "https://example.com".to_string(),
        };
        let wasm_err: WasmError = fetch_err.into();
        assert!(matches!(wasm_err, WasmError::Fetch(_)));
    }

    #[test]
    #[cfg(target_arch = "wasm32")]
    fn test_js_value_conversion() {
        let err = WasmErrorBuilder::invalid_op("test", "invalid");
        let js_val: JsValue = err.into();
        assert!(js_val.is_string());
    }

    #[test]
    fn test_error_builder() {
        let err = WasmErrorBuilder::out_of_memory(1024, Some(512));
        assert!(matches!(err, WasmError::OutOfMemory { .. }));
        assert!(err.to_string().contains("1024"));
    }
}