windjammer 0.48.0

A simple language inspired by Go, Ruby, and Elixir that transpiles to Rust - 80% of Rust's power with 20% of the complexity
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
/// Error Catalog: Generate comprehensive error documentation
///
/// This module generates a searchable error catalog with examples,
/// explanations, and solutions for all Windjammer errors.
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::Path;

/// Error catalog entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorEntry {
    /// Error code (e.g., "E0425", "WJ0001")
    pub code: String,
    /// Short error title
    pub title: String,
    /// Detailed explanation
    pub explanation: String,
    /// Common causes
    pub causes: Vec<String>,
    /// Solutions
    pub solutions: Vec<String>,
    /// Code examples that trigger this error
    pub examples: Vec<ErrorExample>,
    /// Related errors
    pub related: Vec<String>,
    /// Severity (error, warning, note)
    pub severity: String,
    /// Category (type, ownership, syntax, etc.)
    pub category: String,
}

/// Code example for an error
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorExample {
    /// Example title
    pub title: String,
    /// Code that triggers the error
    pub bad_code: String,
    /// Fixed code
    pub good_code: String,
    /// Explanation of the fix
    pub explanation: String,
}

/// Error catalog
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorCatalog {
    /// All error entries indexed by code
    pub errors: HashMap<String, ErrorEntry>,
    /// Categories
    pub categories: Vec<String>,
    /// Version
    pub version: String,
}

impl ErrorCatalog {
    /// Create a new error catalog with common errors
    pub fn new() -> Self {
        let mut catalog = Self {
            errors: HashMap::new(),
            categories: vec![
                "Type Errors".to_string(),
                "Ownership Errors".to_string(),
                "Syntax Errors".to_string(),
                "Module Errors".to_string(),
                "Trait Errors".to_string(),
            ],
            version: "0.1.0".to_string(),
        };

        // Add common Rust errors with Windjammer translations
        catalog.add_common_errors();
        catalog
    }

    /// Add common errors to the catalog
    fn add_common_errors(&mut self) {
        // E0425: Variable not found
        self.add_error(ErrorEntry {
            code: "E0425".to_string(),
            title: "Variable not found".to_string(),
            explanation: "The compiler cannot find a variable, function, or constant with this name in the current scope.".to_string(),
            causes: vec![
                "Typo in the variable name".to_string(),
                "Variable not declared before use".to_string(),
                "Variable is out of scope".to_string(),
                "Module not imported".to_string(),
            ],
            solutions: vec![
                "Check the spelling of the variable name".to_string(),
                "Declare the variable before using it: let x = 42".to_string(),
                "Import the module: use std::collections::HashMap".to_string(),
                "Check variable scope (declared inside a block?)".to_string(),
            ],
            examples: vec![
                ErrorExample {
                    title: "Typo in variable name".to_string(),
                    bad_code: r#"let count = 10
println!("{}", cont)  // Typo: 'cont' instead of 'count'"#.to_string(),
                    good_code: r#"let count = 10
println!("{}", count)  // Fixed!"#.to_string(),
                    explanation: "Fixed the typo in the variable name".to_string(),
                },
                ErrorExample {
                    title: "Variable not declared".to_string(),
                    bad_code: r#"println!("{}", total)  // 'total' not declared"#.to_string(),
                    good_code: r#"let total = 100
println!("{}", total)  // Declared first!"#.to_string(),
                    explanation: "Declared the variable before using it".to_string(),
                },
            ],
            related: vec!["E0412".to_string(), "E0433".to_string()],
            severity: "error".to_string(),
            category: "Type Errors".to_string(),
        });

        // E0308: Type mismatch
        self.add_error(ErrorEntry {
            code: "E0308".to_string(),
            title: "Type mismatch".to_string(),
            explanation: "The compiler expected one type but found another. Types must match exactly in Windjammer.".to_string(),
            causes: vec![
                "Passing wrong type to function".to_string(),
                "Assigning wrong type to variable".to_string(),
                "Returning wrong type from function".to_string(),
                "String vs integer confusion".to_string(),
            ],
            solutions: vec![
                "Use .parse() to convert string to number: \"42\".parse()".to_string(),
                "Use .to_string() to convert number to string: 42.to_string()".to_string(),
                "Check function signature for expected types".to_string(),
                "Use type annotations if needed: let x: int = 42".to_string(),
            ],
            examples: vec![
                ErrorExample {
                    title: "String to int conversion".to_string(),
                    bad_code: r#"let x: int = "42"  // String, not int"#.to_string(),
                    good_code: r#"let x: int = "42".parse()  // Convert to int"#.to_string(),
                    explanation: "Used .parse() to convert string to integer".to_string(),
                },
                ErrorExample {
                    title: "Int to string conversion".to_string(),
                    bad_code: r#"let s: string = 42  // Int, not string"#.to_string(),
                    good_code: r#"let s: string = 42.to_string()  // Convert to string"#.to_string(),
                    explanation: "Used .to_string() to convert integer to string".to_string(),
                },
            ],
            related: vec!["E0277".to_string()],
            severity: "error".to_string(),
            category: "Type Errors".to_string(),
        });

        // E0384: Immutability error
        self.add_error(ErrorEntry {
            code: "E0384".to_string(),
            title: "Cannot modify immutable variable".to_string(),
            explanation: "Variables in Windjammer are immutable by default. To modify a variable, declare it as mutable with 'let mut'.".to_string(),
            causes: vec![
                "Trying to modify immutable variable".to_string(),
                "Forgot 'mut' keyword".to_string(),
            ],
            solutions: vec![
                "Declare variable as mutable: let mut x = 42".to_string(),
                "Create a new variable instead of modifying".to_string(),
            ],
            examples: vec![
                ErrorExample {
                    title: "Modifying immutable variable".to_string(),
                    bad_code: r#"let x = 10
x = 20  // Error: x is immutable"#.to_string(),
                    good_code: r#"let mut x = 10
x = 20  // Works! x is mutable"#.to_string(),
                    explanation: "Added 'mut' keyword to make variable mutable".to_string(),
                },
            ],
            related: vec!["E0596".to_string()],
            severity: "error".to_string(),
            category: "Ownership Errors".to_string(),
        });
    }

    /// Add an error to the catalog
    pub fn add_error(&mut self, error: ErrorEntry) {
        self.errors.insert(error.code.clone(), error);
    }

    /// Get an error by code
    pub fn get_error(&self, code: &str) -> Option<&ErrorEntry> {
        self.errors.get(code)
    }

    /// Search errors by keyword
    pub fn search(&self, keyword: &str) -> Vec<&ErrorEntry> {
        let keyword_lower = keyword.to_lowercase();
        self.errors
            .values()
            .filter(|error| {
                error.title.to_lowercase().contains(&keyword_lower)
                    || error.explanation.to_lowercase().contains(&keyword_lower)
                    || error.code.to_lowercase().contains(&keyword_lower)
            })
            .collect()
    }

    /// Get errors by category
    pub fn get_by_category(&self, category: &str) -> Vec<&ErrorEntry> {
        self.errors
            .values()
            .filter(|error| error.category == category)
            .collect()
    }

    /// Generate HTML documentation
    pub fn generate_html(&self) -> String {
        let mut html = String::new();

        // HTML header
        html.push_str("<!DOCTYPE html>\n");
        html.push_str("<html lang=\"en\">\n");
        html.push_str("<head>\n");
        html.push_str("  <meta charset=\"UTF-8\">\n");
        html.push_str(
            "  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n",
        );
        html.push_str("  <title>Windjammer Error Catalog</title>\n");
        html.push_str("  <style>\n");
        html.push_str(Self::get_default_css());
        html.push_str("  </style>\n");
        html.push_str("</head>\n");
        html.push_str("<body>\n");

        // Header
        html.push_str("  <header>\n");
        html.push_str("    <h1>Windjammer Error Catalog</h1>\n");
        html.push_str(&format!("    <p>Version {}</p>\n", self.version));
        html.push_str("  </header>\n");

        // Navigation
        html.push_str("  <nav>\n");
        html.push_str("    <h2>Categories</h2>\n");
        html.push_str("    <ul>\n");
        for category in &self.categories {
            let anchor = category.replace(" ", "-").to_lowercase();
            html.push_str(&format!(
                "      <li><a href=\"#{}\">{}</a></li>\n",
                anchor, category
            ));
        }
        html.push_str("    </ul>\n");
        html.push_str("  </nav>\n");

        // Main content
        html.push_str("  <main>\n");

        for category in &self.categories {
            let anchor = category.replace(" ", "-").to_lowercase();
            html.push_str(&format!("    <section id=\"{}\">\n", anchor));
            html.push_str(&format!("      <h2>{}</h2>\n", category));

            let errors = self.get_by_category(category);
            for error in errors {
                html.push_str(&self.generate_error_html(error));
            }

            html.push_str("    </section>\n");
        }

        html.push_str("  </main>\n");
        html.push_str("</body>\n");
        html.push_str("</html>\n");

        html
    }

    /// Generate HTML for a single error
    fn generate_error_html(&self, error: &ErrorEntry) -> String {
        let mut html = String::new();

        html.push_str(&format!(
            "      <article class=\"error\" id=\"{}\">\n",
            error.code
        ));
        html.push_str(&format!(
            "        <h3>{} - {}</h3>\n",
            error.code, error.title
        ));
        html.push_str(&format!(
            "        <p class=\"explanation\">{}</p>\n",
            error.explanation
        ));

        // Causes
        if !error.causes.is_empty() {
            html.push_str("        <h4>Common Causes:</h4>\n");
            html.push_str("        <ul>\n");
            for cause in &error.causes {
                html.push_str(&format!("          <li>{}</li>\n", cause));
            }
            html.push_str("        </ul>\n");
        }

        // Solutions
        if !error.solutions.is_empty() {
            html.push_str("        <h4>Solutions:</h4>\n");
            html.push_str("        <ul>\n");
            for solution in &error.solutions {
                html.push_str(&format!("          <li>{}</li>\n", solution));
            }
            html.push_str("        </ul>\n");
        }

        // Examples
        if !error.examples.is_empty() {
            html.push_str("        <h4>Examples:</h4>\n");
            for example in &error.examples {
                html.push_str("        <div class=\"example\">\n");
                html.push_str(&format!("          <h5>{}</h5>\n", example.title));
                html.push_str("          <div class=\"code-comparison\">\n");
                html.push_str("            <div class=\"bad-code\">\n");
                html.push_str("              <h6>❌ Wrong:</h6>\n");
                html.push_str(&format!(
                    "              <pre><code>{}</code></pre>\n",
                    example.bad_code
                ));
                html.push_str("            </div>\n");
                html.push_str("            <div class=\"good-code\">\n");
                html.push_str("              <h6>✅ Correct:</h6>\n");
                html.push_str(&format!(
                    "              <pre><code>{}</code></pre>\n",
                    example.good_code
                ));
                html.push_str("            </div>\n");
                html.push_str("          </div>\n");
                html.push_str(&format!(
                    "          <p class=\"example-explanation\">{}</p>\n",
                    example.explanation
                ));
                html.push_str("        </div>\n");
            }
        }

        html.push_str("      </article>\n");
        html
    }

    /// Generate Markdown documentation
    pub fn generate_markdown(&self) -> String {
        let mut md = String::new();

        md.push_str("# Windjammer Error Catalog\n\n");
        md.push_str(&format!("Version: {}\n\n", self.version));

        for category in &self.categories {
            md.push_str(&format!("## {}\n\n", category));

            let errors = self.get_by_category(category);
            for error in errors {
                md.push_str(&self.generate_error_markdown(error));
            }
        }

        md
    }

    /// Generate Markdown for a single error
    fn generate_error_markdown(&self, error: &ErrorEntry) -> String {
        let mut md = String::new();

        md.push_str(&format!("### {} - {}\n\n", error.code, error.title));
        md.push_str(&format!("{}\n\n", error.explanation));

        if !error.causes.is_empty() {
            md.push_str("**Common Causes:**\n\n");
            for cause in &error.causes {
                md.push_str(&format!("- {}\n", cause));
            }
            md.push('\n');
        }

        if !error.solutions.is_empty() {
            md.push_str("**Solutions:**\n\n");
            for solution in &error.solutions {
                md.push_str(&format!("- {}\n", solution));
            }
            md.push('\n');
        }

        if !error.examples.is_empty() {
            md.push_str("**Examples:**\n\n");
            for example in &error.examples {
                md.push_str(&format!("#### {}\n\n", example.title));
                md.push_str("❌ Wrong:\n\n");
                md.push_str(&format!("```windjammer\n{}\n```\n\n", example.bad_code));
                md.push_str("✅ Correct:\n\n");
                md.push_str(&format!("```windjammer\n{}\n```\n\n", example.good_code));
                md.push_str(&format!("{}\n\n", example.explanation));
            }
        }

        md.push_str("---\n\n");
        md
    }

    /// Save catalog to JSON file
    pub fn save_json(&self, path: &Path) -> anyhow::Result<()> {
        let json = serde_json::to_string_pretty(self)?;
        fs::write(path, json)?;
        Ok(())
    }

    /// Load catalog from JSON file
    pub fn load_json(path: &Path) -> anyhow::Result<Self> {
        let json = fs::read_to_string(path)?;
        let catalog: ErrorCatalog = serde_json::from_str(&json)?;
        Ok(catalog)
    }

    /// Get default CSS for HTML generation
    fn get_default_css() -> &'static str {
        include_str!("../docs/error_catalog_style.css")
    }
}

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

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

    #[test]
    fn test_catalog_creation() {
        let catalog = ErrorCatalog::new();
        assert!(!catalog.errors.is_empty());
        assert!(catalog.errors.contains_key("E0425"));
    }

    #[test]
    fn test_search() {
        let catalog = ErrorCatalog::new();
        let results = catalog.search("variable");
        assert!(!results.is_empty());
    }

    #[test]
    fn test_get_by_category() {
        let catalog = ErrorCatalog::new();
        let errors = catalog.get_by_category("Type Errors");
        assert!(!errors.is_empty());
    }

    #[test]
    fn test_generate_markdown() {
        let catalog = ErrorCatalog::new();
        let md = catalog.generate_markdown();
        assert!(md.contains("# Windjammer Error Catalog"));
        assert!(md.contains("E0425"));
    }

    #[test]
    fn test_generate_html() {
        let catalog = ErrorCatalog::new();
        let html = catalog.generate_html();
        assert!(html.contains("<!DOCTYPE html>"));
        assert!(html.contains("Windjammer Error Catalog"));
    }
}