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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
//! TypeScript bindings and documentation generation
//!
//! This module provides utilities for generating TypeScript type definitions,
//! API documentation, and example code for the WASM bindings.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// TypeScript type representation
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum TsType {
    /// String primitive type
    String,
    /// Number primitive type
    Number,
    /// Boolean primitive type
    Boolean,
    /// Void type (function returns nothing)
    Void,
    /// Null type
    Null,
    /// Undefined type
    Undefined,
    /// Any type (accepts anything)
    Any,

    /// Array type
    Array(Box<TsType>),

    /// Object type with fields
    Object(Vec<(String, TsType)>),

    /// Union type
    Union(Vec<TsType>),

    /// Optional type
    Optional(Box<TsType>),

    /// Promise type
    Promise(Box<TsType>),

    /// Tuple type
    Tuple(Vec<TsType>),

    /// Custom type reference
    Reference(String),
}

impl TsType {
    /// Converts to TypeScript string representation
    pub fn to_ts_string(&self) -> String {
        match self {
            Self::String => "string".to_string(),
            Self::Number => "number".to_string(),
            Self::Boolean => "boolean".to_string(),
            Self::Void => "void".to_string(),
            Self::Null => "null".to_string(),
            Self::Undefined => "undefined".to_string(),
            Self::Any => "any".to_string(),
            Self::Array(inner) => format!("{}[]", inner.to_ts_string()),
            Self::Object(fields) => {
                let fields_str = fields
                    .iter()
                    .map(|(name, ty)| format!("{}: {}", name, ty.to_ts_string()))
                    .collect::<Vec<_>>()
                    .join(", ");
                format!("{{ {} }}", fields_str)
            }
            Self::Union(types) => types
                .iter()
                .map(|t| t.to_ts_string())
                .collect::<Vec<_>>()
                .join(" | "),
            Self::Optional(inner) => format!("{} | null", inner.to_ts_string()),
            Self::Promise(inner) => format!("Promise<{}>", inner.to_ts_string()),
            Self::Tuple(types) => {
                let types_str = types
                    .iter()
                    .map(|t| t.to_ts_string())
                    .collect::<Vec<_>>()
                    .join(", ");
                format!("[{}]", types_str)
            }
            Self::Reference(name) => name.clone(),
        }
    }
}

/// Function parameter
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TsParameter {
    /// Parameter name
    pub name: String,
    /// Parameter type
    pub ty: TsType,
    /// Is optional
    pub optional: bool,
    /// Default value (as string)
    pub default: Option<String>,
    /// Description
    pub description: Option<String>,
}

impl TsParameter {
    /// Creates a new parameter
    pub fn new(name: impl Into<String>, ty: TsType) -> Self {
        Self {
            name: name.into(),
            ty,
            optional: false,
            default: None,
            description: None,
        }
    }

    /// Makes the parameter optional
    pub fn optional(mut self) -> Self {
        self.optional = true;
        self
    }

    /// Sets a default value
    pub fn with_default(mut self, default: impl Into<String>) -> Self {
        self.default = Some(default.into());
        self.optional = true;
        self
    }

    /// Sets a description
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Converts to TypeScript string
    pub fn to_ts_string(&self) -> String {
        let optional_marker = if self.optional { "?" } else { "" };
        let default_str = if let Some(ref default) = self.default {
            format!(" = {}", default)
        } else {
            String::new()
        };

        format!(
            "{}{}: {}{}",
            self.name,
            optional_marker,
            self.ty.to_ts_string(),
            default_str
        )
    }
}

/// Function signature
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TsFunction {
    /// Function name
    pub name: String,
    /// Parameters
    pub parameters: Vec<TsParameter>,
    /// Return type
    pub return_type: TsType,
    /// Is async
    pub is_async: bool,
    /// Description
    pub description: Option<String>,
    /// Example code
    pub examples: Vec<String>,
}

impl TsFunction {
    /// Creates a new function
    pub fn new(name: impl Into<String>, return_type: TsType) -> Self {
        Self {
            name: name.into(),
            parameters: Vec::new(),
            return_type,
            is_async: false,
            description: None,
            examples: Vec::new(),
        }
    }

    /// Adds a parameter
    pub fn parameter(mut self, param: TsParameter) -> Self {
        self.parameters.push(param);
        self
    }

    /// Marks as async
    pub fn async_fn(mut self) -> Self {
        self.is_async = true;
        self
    }

    /// Sets a description
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Adds an example
    pub fn with_example(mut self, example: impl Into<String>) -> Self {
        self.examples.push(example.into());
        self
    }

    /// Generates TypeScript declaration
    pub fn to_ts_declaration(&self) -> String {
        let params_str = self
            .parameters
            .iter()
            .map(|p| p.to_ts_string())
            .collect::<Vec<_>>()
            .join(", ");

        let return_type = if self.is_async {
            TsType::Promise(Box::new(self.return_type.clone()))
        } else {
            self.return_type.clone()
        };

        format!(
            "{}({}): {}",
            self.name,
            params_str,
            return_type.to_ts_string()
        )
    }

    /// Generates JSDoc comment
    pub fn to_jsdoc(&self) -> String {
        let mut lines = vec!["/**".to_string()];

        if let Some(ref desc) = self.description {
            lines.push(format!(" * {}", desc));
            if !self.parameters.is_empty() || !self.examples.is_empty() {
                lines.push(" *".to_string());
            }
        }

        for param in &self.parameters {
            let param_desc = param
                .description
                .as_ref()
                .map(|d| format!(" - {}", d))
                .unwrap_or_default();
            lines.push(format!(
                " * @param {{{}}} {}{}",
                param.ty.to_ts_string(),
                param.name,
                param_desc
            ));
        }

        if self.is_async {
            lines.push(format!(
                " * @returns {{Promise<{}>}}",
                self.return_type.to_ts_string()
            ));
        } else {
            lines.push(format!(
                " * @returns {{{}}}",
                self.return_type.to_ts_string()
            ));
        }

        if !self.examples.is_empty() {
            lines.push(" *".to_string());
            for example in &self.examples {
                lines.push(" * @example".to_string());
                for line in example.lines() {
                    lines.push(format!(" * {}", line));
                }
            }
        }

        lines.push(" */".to_string());
        lines.join("\n")
    }
}

/// Class member
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TsMember {
    /// Constructor
    Constructor(TsFunction),
    /// Method
    Method(TsFunction),
    /// Property
    Property {
        name: String,
        ty: TsType,
        readonly: bool,
        description: Option<String>,
    },
}

/// Class definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TsClass {
    /// Class name
    pub name: String,
    /// Description
    pub description: Option<String>,
    /// Members
    pub members: Vec<TsMember>,
    /// Examples
    pub examples: Vec<String>,
}

impl TsClass {
    /// Creates a new class
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            description: None,
            members: Vec::new(),
            examples: Vec::new(),
        }
    }

    /// Sets a description
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Adds a member
    pub fn member(mut self, member: TsMember) -> Self {
        self.members.push(member);
        self
    }

    /// Adds an example
    pub fn with_example(mut self, example: impl Into<String>) -> Self {
        self.examples.push(example.into());
        self
    }

    /// Generates TypeScript declaration
    pub fn to_ts_declaration(&self) -> String {
        let mut lines = Vec::new();

        // Class JSDoc
        lines.push("/**".to_string());
        if let Some(ref desc) = self.description {
            lines.push(format!(" * {}", desc));
        }
        if !self.examples.is_empty() {
            lines.push(" *".to_string());
            for example in &self.examples {
                lines.push(" * @example".to_string());
                for line in example.lines() {
                    lines.push(format!(" * {}", line));
                }
            }
        }
        lines.push(" */".to_string());

        lines.push(format!("export class {} {{", self.name));

        for member in &self.members {
            match member {
                TsMember::Constructor(func) => {
                    lines.push("".to_string());
                    lines.push(format!("  {}", func.to_jsdoc().replace('\n', "\n  ")));
                    lines.push(format!(
                        "  constructor({});",
                        func.parameters
                            .iter()
                            .map(|p| p.to_ts_string())
                            .collect::<Vec<_>>()
                            .join(", ")
                    ));
                }
                TsMember::Method(func) => {
                    lines.push("".to_string());
                    lines.push(format!("  {}", func.to_jsdoc().replace('\n', "\n  ")));
                    lines.push(format!("  {};", func.to_ts_declaration()));
                }
                TsMember::Property {
                    name,
                    ty,
                    readonly,
                    description,
                } => {
                    lines.push("".to_string());
                    if let Some(desc) = description {
                        lines.push(format!("  /** {} */", desc));
                    }
                    let readonly_str = if *readonly { "readonly " } else { "" };
                    lines.push(format!(
                        "  {}{}: {};",
                        readonly_str,
                        name,
                        ty.to_ts_string()
                    ));
                }
            }
        }

        lines.push("}".to_string());
        lines.join("\n")
    }
}

/// Interface definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TsInterface {
    /// Interface name
    pub name: String,
    /// Description
    pub description: Option<String>,
    /// Fields
    pub fields: Vec<(String, TsType, Option<String>)>, // (name, type, description)
}

impl TsInterface {
    /// Creates a new interface
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            description: None,
            fields: Vec::new(),
        }
    }

    /// Sets a description
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Adds a field
    pub fn field(mut self, name: impl Into<String>, ty: TsType) -> Self {
        self.fields.push((name.into(), ty, None));
        self
    }

    /// Adds a field with description
    pub fn field_with_description(
        mut self,
        name: impl Into<String>,
        ty: TsType,
        description: impl Into<String>,
    ) -> Self {
        self.fields
            .push((name.into(), ty, Some(description.into())));
        self
    }

    /// Generates TypeScript declaration
    pub fn to_ts_declaration(&self) -> String {
        let mut lines = Vec::new();

        lines.push("/**".to_string());
        if let Some(ref desc) = self.description {
            lines.push(format!(" * {}", desc));
        }
        lines.push(" */".to_string());

        lines.push(format!("export interface {} {{", self.name));

        for (name, ty, desc) in &self.fields {
            if let Some(description) = desc {
                lines.push(format!("  /** {} */", description));
            }
            lines.push(format!("  {}: {};", name, ty.to_ts_string()));
        }

        lines.push("}".to_string());
        lines.join("\n")
    }
}

/// Type alias definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TsTypeAlias {
    /// Alias name
    pub name: String,
    /// Target type
    pub ty: TsType,
    /// Description
    pub description: Option<String>,
}

impl TsTypeAlias {
    /// Creates a new type alias
    pub fn new(name: impl Into<String>, ty: TsType) -> Self {
        Self {
            name: name.into(),
            ty,
            description: None,
        }
    }

    /// Sets a description
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Generates TypeScript declaration
    pub fn to_ts_declaration(&self) -> String {
        let mut lines = Vec::new();

        if let Some(ref desc) = self.description {
            lines.push("/**".to_string());
            lines.push(format!(" * {}", desc));
            lines.push(" */".to_string());
        }

        lines.push(format!(
            "export type {} = {};",
            self.name,
            self.ty.to_ts_string()
        ));
        lines.join("\n")
    }
}

/// Module definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TsModule {
    /// Module name
    pub name: String,
    /// Description
    pub description: Option<String>,
    /// Classes
    pub classes: Vec<TsClass>,
    /// Interfaces
    pub interfaces: Vec<TsInterface>,
    /// Type aliases
    pub type_aliases: Vec<TsTypeAlias>,
    /// Functions
    pub functions: Vec<TsFunction>,
}

impl TsModule {
    /// Creates a new module
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            description: None,
            classes: Vec::new(),
            interfaces: Vec::new(),
            type_aliases: Vec::new(),
            functions: Vec::new(),
        }
    }

    /// Sets a description
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Adds a class
    pub fn class(mut self, class: TsClass) -> Self {
        self.classes.push(class);
        self
    }

    /// Adds an interface
    pub fn interface(mut self, interface: TsInterface) -> Self {
        self.interfaces.push(interface);
        self
    }

    /// Adds a type alias
    pub fn type_alias(mut self, alias: TsTypeAlias) -> Self {
        self.type_aliases.push(alias);
        self
    }

    /// Adds a function
    pub fn function(mut self, function: TsFunction) -> Self {
        self.functions.push(function);
        self
    }

    /// Generates TypeScript declarations
    pub fn to_ts_declarations(&self) -> String {
        let mut lines = Vec::new();

        // Module header
        lines.push("/**".to_string());
        lines.push(format!(" * @module {}", self.name));
        if let Some(ref desc) = self.description {
            lines.push(" *".to_string());
            lines.push(format!(" * {}", desc));
        }
        lines.push(" */".to_string());
        lines.push("".to_string());

        // Type aliases
        for alias in &self.type_aliases {
            lines.push(alias.to_ts_declaration());
            lines.push("".to_string());
        }

        // Interfaces
        for interface in &self.interfaces {
            lines.push(interface.to_ts_declaration());
            lines.push("".to_string());
        }

        // Classes
        for class in &self.classes {
            lines.push(class.to_ts_declaration());
            lines.push("".to_string());
        }

        // Functions
        for function in &self.functions {
            lines.push(function.to_jsdoc());
            lines.push(format!(
                "export function {}();",
                function.to_ts_declaration()
            ));
            lines.push("".to_string());
        }

        lines.join("\n")
    }
}

/// Documentation generator
pub struct DocGenerator {
    /// Modules
    modules: HashMap<String, TsModule>,
}

impl DocGenerator {
    /// Creates a new documentation generator
    pub fn new() -> Self {
        Self {
            modules: HashMap::new(),
        }
    }

    /// Adds a module
    pub fn add_module(&mut self, module: TsModule) {
        self.modules.insert(module.name.clone(), module);
    }

    /// Generates all declarations
    pub fn generate_all(&self) -> HashMap<String, String> {
        self.modules
            .iter()
            .map(|(name, module)| (name.clone(), module.to_ts_declarations()))
            .collect()
    }

    /// Generates a combined .d.ts file
    pub fn generate_combined(&self) -> String {
        let mut output = Vec::new();

        output.push("// Auto-generated TypeScript declarations for oxigdal-wasm".to_string());
        output.push("// Do not edit manually".to_string());
        output.push("".to_string());

        for module in self.modules.values() {
            output.push(module.to_ts_declarations());
            output.push("".to_string());
        }

        output.join("\n")
    }
}

impl Default for DocGenerator {
    fn default() -> Self {
        Self::new()
    }
}

/// Creates the standard OxiGDAL WASM bindings documentation
pub fn create_oxigdal_wasm_docs() -> DocGenerator {
    let mut generator = DocGenerator::new();

    // Create main module
    let mut main_module = TsModule::new("oxigdal-wasm")
        .with_description("WebAssembly bindings for OxiGDAL - Browser-based geospatial processing");

    // Add WasmCogViewer class
    let cog_viewer = TsClass::new("WasmCogViewer")
        .with_description("Cloud Optimized GeoTIFF viewer for the browser")
        .member(TsMember::Constructor(
            TsFunction::new("constructor", TsType::Void)
                .with_description("Creates a new COG viewer instance"),
        ))
        .member(TsMember::Method(
            TsFunction::new("open", TsType::Promise(Box::new(TsType::Void)))
                .async_fn()
                .parameter(TsParameter::new("url", TsType::String).with_description("URL to the COG file"))
                .with_description("Opens a COG file from a URL")
                .with_example("const viewer = new WasmCogViewer();\nawait viewer.open('https://example.com/image.tif');"),
        ))
        .member(TsMember::Method(
            TsFunction::new("width", TsType::Number)
                .with_description("Returns the image width in pixels"),
        ))
        .member(TsMember::Method(
            TsFunction::new("height", TsType::Number)
                .with_description("Returns the image height in pixels"),
        ))
        .with_example("const viewer = new WasmCogViewer();\nawait viewer.open('image.tif');\nconsole.log(`Size: ${viewer.width()}x${viewer.height()}`);");

    main_module = main_module.class(cog_viewer);

    // Add TileCoord interface
    let tile_coord = TsInterface::new("TileCoord")
        .with_description("Tile coordinate in a pyramid")
        .field_with_description("level", TsType::Number, "Zoom level")
        .field_with_description("x", TsType::Number, "Column index")
        .field_with_description("y", TsType::Number, "Row index");

    main_module = main_module.interface(tile_coord);

    generator.add_module(main_module);
    generator
}

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

    #[test]
    fn test_ts_type_string() {
        assert_eq!(TsType::String.to_ts_string(), "string");
        assert_eq!(TsType::Number.to_ts_string(), "number");
        assert_eq!(
            TsType::Array(Box::new(TsType::String)).to_ts_string(),
            "string[]"
        );
        assert_eq!(
            TsType::Promise(Box::new(TsType::Number)).to_ts_string(),
            "Promise<number>"
        );
    }

    #[test]
    fn test_ts_parameter() {
        let param = TsParameter::new("name", TsType::String);
        assert_eq!(param.to_ts_string(), "name: string");

        let optional_param = TsParameter::new("value", TsType::Number).optional();
        assert_eq!(optional_param.to_ts_string(), "value?: number");
    }

    #[test]
    fn test_ts_function() {
        let func = TsFunction::new("greet", TsType::String)
            .parameter(TsParameter::new("name", TsType::String))
            .with_description("Greets a person");

        let declaration = func.to_ts_declaration();
        assert!(declaration.contains("greet"));
        assert!(declaration.contains("name: string"));
        assert!(declaration.contains("string"));
    }

    #[test]
    fn test_ts_class() {
        let class = TsClass::new("MyClass")
            .with_description("A test class")
            .member(TsMember::Method(
                TsFunction::new("method", TsType::Void).with_description("A test method"),
            ));

        let declaration = class.to_ts_declaration();
        assert!(declaration.contains("export class MyClass"));
        assert!(declaration.contains("method"));
    }

    #[test]
    fn test_ts_interface() {
        let interface = TsInterface::new("MyInterface")
            .with_description("A test interface")
            .field("name", TsType::String)
            .field("age", TsType::Number);

        let declaration = interface.to_ts_declaration();
        assert!(declaration.contains("export interface MyInterface"));
        assert!(declaration.contains("name: string"));
        assert!(declaration.contains("age: number"));
    }

    #[test]
    fn test_doc_generator() {
        let generator = create_oxigdal_wasm_docs();
        let combined = generator.generate_combined();

        assert!(combined.contains("WasmCogViewer"));
        assert!(combined.contains("TileCoord"));
    }
}