sigil-stitch 0.3.2

Type-safe, import-aware, width-aware code generation for multiple languages
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
//! Java language implementation.

use crate::import::ImportGroup;
use crate::lang::CodeLang;
use crate::spec::modifiers::{DeclarationContext, TypeKind, Visibility};

/// Java language implementation.
///
/// Java-specific behaviors:
/// - Type-before-name declarations (`int count`, not `count: int`)
/// - Return type as prefix (`int add(int a, int b)`)
/// - `import pkg.Class;` with java/javax/third-party grouping
/// - Semicolons after statements
/// - No function keyword (no `fn`/`func`/`def`)
/// - `class`, `interface`, and `enum` keywords
/// - `extends` for class inheritance, `implements` for interfaces
/// - Generic bounds via `extends` and `&` (`<T extends Comparable & Serializable>`)
/// - `/** ... */` Javadoc comments
/// - `final` instead of `const` for readonly fields
/// - Annotations (`@Override`, `@Nullable`) via `annotation()`
///
/// # Import conventions
///
/// Use [`crate::type_name::TypeName::importable`] with the package as module and class name as name:
/// ```text
/// TypeName::importable("java.util", "List")            // import java.util.List;
/// TypeName::importable("java.util", "Map")             // import java.util.Map;
/// TypeName::importable("com.example.model", "User")    // import com.example.model.User;
/// ```
///
/// # Annotations
///
/// Use `annotation()` on builders for Java annotations:
/// ```text
/// fb.annotation(CodeBlock::of("@Override", ()).unwrap());
/// fb.annotation(CodeBlock::of("@Nullable", ()).unwrap());
/// ```
///
/// # Constructors
///
/// Omit `.returns()` — with `return_type_is_prefix()`, no return type means
/// the signature starts with modifiers then name: `public ClassName(...)`.
#[derive(Debug, Clone)]
pub struct JavaLang {
    /// Indent with this string (default: "    " — 4 spaces).
    pub indent: String,
    /// File extension (default: "java").
    pub extension: String,
}

impl Default for JavaLang {
    fn default() -> Self {
        Self {
            indent: "    ".to_string(),
            extension: "java".to_string(),
        }
    }
}

impl JavaLang {
    /// Create a new Java language instance.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the indent string (e.g., `"    "` for 4-space, `"\t"` for tabs).
    pub fn with_indent(mut self, s: &str) -> Self {
        self.indent = s.to_string();
        self
    }

    /// Set the file extension (default: `"java"`).
    pub fn with_extension(mut self, s: &str) -> Self {
        self.extension = s.to_string();
        self
    }
}

#[rustfmt::skip]
const JAVA_RESERVED: &[&str] = &[
    "abstract", "assert", "boolean", "break", "byte", "case", "catch",
    "char", "class", "const", "continue", "default", "do", "double",
    "else", "enum", "extends", "final", "finally", "float", "for",
    "goto", "if", "implements", "import", "instanceof", "int",
    "interface", "long", "native", "new", "package", "private",
    "protected", "public", "return", "short", "static", "strictfp",
    "super", "switch", "synchronized", "this", "throw", "throws",
    "transient", "try", "var", "void", "volatile", "while", "yield",
];

/// Classify an import module into a group for ordering.
/// 0 = java.*, 1 = javax.*, 2 = everything else.
fn import_group_order(module: &str) -> u8 {
    if module.starts_with("java.") {
        0
    } else if module.starts_with("javax.") {
        1
    } else {
        2
    }
}

impl CodeLang for JavaLang {
    fn file_extension(&self) -> &str {
        &self.extension
    }

    fn reserved_words(&self) -> &[&str] {
        JAVA_RESERVED
    }

    fn render_imports(&self, imports: &ImportGroup) -> String {
        if imports.entries.is_empty() {
            return String::new();
        }

        // Collect unique fully-qualified imports, grouped by category.
        let mut java_imports: Vec<String> = Vec::new();
        let mut javax_imports: Vec<String> = Vec::new();
        let mut other_imports: Vec<String> = Vec::new();

        let mut seen = std::collections::BTreeSet::new();
        for entry in &imports.entries {
            let line = if entry.is_wildcard {
                let fqn = format!("{}.*", entry.module);
                if !seen.insert(fqn.clone()) {
                    continue;
                }
                format!("import {};", fqn)
            } else if entry.is_side_effect {
                // Java has no side-effect imports; skip.
                continue;
            } else {
                let fqn = format!("{}.{}", entry.module, entry.name);
                if !seen.insert(fqn.clone()) {
                    continue;
                }
                format!("import {};", fqn)
            };

            match import_group_order(&entry.module) {
                0 => java_imports.push(line),
                1 => javax_imports.push(line),
                _ => other_imports.push(line),
            }
        }

        java_imports.sort();
        javax_imports.sort();
        other_imports.sort();

        let groups: Vec<&Vec<String>> = [&java_imports, &javax_imports, &other_imports]
            .into_iter()
            .filter(|g| !g.is_empty())
            .collect();

        let mut lines = Vec::new();
        for (i, group) in groups.iter().enumerate() {
            if i > 0 {
                lines.push(String::new());
            }
            lines.extend(group.iter().cloned());
        }

        lines.join("\n")
    }

    fn render_string_literal(&self, s: &str) -> String {
        format!(
            "\"{}\"",
            s.replace('\\', "\\\\")
                .replace('"', "\\\"")
                .replace('\n', "\\n")
                .replace('\t', "\\t")
                .replace('\r', "\\r")
                .replace('\0', "\\0")
        )
    }

    fn render_doc_comment(&self, lines: &[&str]) -> String {
        // Javadoc /** ... */ style.
        let mut result = String::from("/**");
        for line in lines {
            result.push('\n');
            if line.is_empty() {
                result.push_str(" *");
            } else {
                result.push_str(" * ");
                result.push_str(line);
            }
        }
        result.push('\n');
        result.push_str(" */");
        result
    }

    fn line_comment_prefix(&self) -> &str {
        "//"
    }

    fn render_visibility(&self, vis: Visibility, ctx: DeclarationContext) -> &str {
        match ctx {
            DeclarationContext::TopLevel => match vis {
                Visibility::Public => "public ",
                _ => "", // package-private
            },
            DeclarationContext::Member => match vis {
                Visibility::Public => "public ",
                Visibility::Private => "private ",
                Visibility::Protected => "protected ",
                _ => "", // package-private
            },
        }
    }

    fn function_keyword(&self, _ctx: DeclarationContext) -> &str {
        // Java has no function keyword.
        ""
    }

    fn type_keyword(&self, kind: TypeKind) -> &str {
        match kind {
            TypeKind::Class | TypeKind::Struct => "class",
            TypeKind::Interface | TypeKind::Trait => "interface",
            TypeKind::Enum => "enum",
            TypeKind::TypeAlias | TypeKind::Newtype => "class",
        }
    }

    fn methods_inside_type_body(&self, _kind: TypeKind) -> bool {
        true
    }

    fn optional_field_style(&self) -> crate::lang::config::OptionalFieldStyle {
        // Note: callers must ensure `java.util.Optional` is imported. The
        // wrapping text itself is emitted literally and does not trigger
        // automatic import tracking.
        crate::lang::config::OptionalFieldStyle::TypeWrap {
            open: "Optional<",
            close: ">",
        }
    }

    fn type_presentation(&self) -> crate::lang::config::TypePresentationConfig<'_> {
        crate::lang::config::TypePresentationConfig {
            array: crate::type_name::TypePresentation::GenericWrap { name: "List" },
            readonly_array: Some(crate::type_name::TypePresentation::GenericWrap { name: "List" }),
            optional: crate::type_name::TypePresentation::GenericWrap { name: "Optional" },
            associated_type: crate::type_name::AssociatedTypeStyle::DotAccess,
            ..Default::default()
        }
    }

    fn generic_syntax(&self) -> crate::lang::config::GenericSyntaxConfig<'_> {
        crate::lang::config::GenericSyntaxConfig {
            constraint_keyword: " extends ",
            constraint_separator: " & ",
            context_bound_keyword: " extends ",
            ..Default::default()
        }
    }

    fn module_separator(&self) -> Option<&str> {
        Some(".")
    }

    fn block_syntax(&self) -> crate::lang::config::BlockSyntaxConfig<'_> {
        crate::lang::config::BlockSyntaxConfig {
            indent_unit: &self.indent,
            field_terminator: ";",
            ..Default::default()
        }
    }

    fn function_syntax(&self) -> crate::lang::config::FunctionSyntaxConfig<'_> {
        crate::lang::config::FunctionSyntaxConfig {
            return_type_separator: " ",
            async_keyword: "",
            ..Default::default()
        }
    }

    fn type_decl_syntax(&self) -> crate::lang::config::TypeDeclSyntaxConfig<'_> {
        crate::lang::config::TypeDeclSyntaxConfig {
            type_before_name: true,
            return_type_is_prefix: true,
            super_type_keyword: " extends ",
            implements_keyword: " implements ",
            ..Default::default()
        }
    }

    fn enum_and_annotation(&self) -> crate::lang::config::EnumAndAnnotationConfig<'_> {
        crate::lang::config::EnumAndAnnotationConfig {
            readonly_keyword: "final ",
            variant_value_format: crate::lang::config::VariantValueFormat::ConstructorArg,
            variants_before_fields: true,
            variant_section_terminator: ";",
            ..Default::default()
        }
    }
}

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

    #[test]
    fn test_file_extension() {
        let java = JavaLang::new();
        assert_eq!(java.file_extension(), "java");
    }

    #[test]
    fn test_escape_reserved() {
        let java = JavaLang::new();
        assert_eq!(java.escape_reserved("class"), "class_");
        assert_eq!(java.escape_reserved("import"), "import_");
        assert_eq!(java.escape_reserved("synchronized"), "synchronized_");
        assert_eq!(java.escape_reserved("name"), "name");
    }

    #[test]
    fn test_render_imports_single() {
        let java = JavaLang::new();
        let imports = ImportGroup {
            entries: vec![ImportEntry {
                module: "java.util".into(),
                name: "List".into(),
                alias: None,
                is_type_only: false,
                is_side_effect: false,
                is_wildcard: false,
            }],
        };
        assert_eq!(java.render_imports(&imports), "import java.util.List;");
    }

    #[test]
    fn test_render_imports_grouped() {
        let java = JavaLang::new();
        let imports = ImportGroup {
            entries: vec![
                ImportEntry {
                    module: "com.example.model".into(),
                    name: "User".into(),
                    alias: None,
                    is_type_only: false,
                    is_side_effect: false,
                    is_wildcard: false,
                },
                ImportEntry {
                    module: "java.util".into(),
                    name: "List".into(),
                    alias: None,
                    is_type_only: false,
                    is_side_effect: false,
                    is_wildcard: false,
                },
                ImportEntry {
                    module: "javax.persistence".into(),
                    name: "Entity".into(),
                    alias: None,
                    is_type_only: false,
                    is_side_effect: false,
                    is_wildcard: false,
                },
            ],
        };
        let output = java.render_imports(&imports);
        let lines: Vec<&str> = output.lines().collect();
        assert_eq!(lines[0], "import java.util.List;");
        assert_eq!(lines[1], "");
        assert_eq!(lines[2], "import javax.persistence.Entity;");
        assert_eq!(lines[3], "");
        assert_eq!(lines[4], "import com.example.model.User;");
    }

    #[test]
    fn test_render_imports_sorted_within_group() {
        let java = JavaLang::new();
        let imports = ImportGroup {
            entries: vec![
                ImportEntry {
                    module: "java.util".into(),
                    name: "Map".into(),
                    alias: None,
                    is_type_only: false,
                    is_side_effect: false,
                    is_wildcard: false,
                },
                ImportEntry {
                    module: "java.io".into(),
                    name: "File".into(),
                    alias: None,
                    is_type_only: false,
                    is_side_effect: false,
                    is_wildcard: false,
                },
                ImportEntry {
                    module: "java.util".into(),
                    name: "List".into(),
                    alias: None,
                    is_type_only: false,
                    is_side_effect: false,
                    is_wildcard: false,
                },
            ],
        };
        let output = java.render_imports(&imports);
        let lines: Vec<&str> = output.lines().collect();
        assert_eq!(lines[0], "import java.io.File;");
        assert_eq!(lines[1], "import java.util.List;");
        assert_eq!(lines[2], "import java.util.Map;");
    }

    #[test]
    fn test_render_imports_dedup() {
        let java = JavaLang::new();
        let imports = ImportGroup {
            entries: vec![
                ImportEntry {
                    module: "java.util".into(),
                    name: "List".into(),
                    alias: None,
                    is_type_only: false,
                    is_side_effect: false,
                    is_wildcard: false,
                },
                ImportEntry {
                    module: "java.util".into(),
                    name: "List".into(),
                    alias: None,
                    is_type_only: false,
                    is_side_effect: false,
                    is_wildcard: false,
                },
            ],
        };
        assert_eq!(java.render_imports(&imports), "import java.util.List;");
    }

    #[test]
    fn test_doc_comment_single() {
        let java = JavaLang::new();
        assert_eq!(
            java.render_doc_comment(&["A brief description."]),
            "/**\n * A brief description.\n */"
        );
    }

    #[test]
    fn test_doc_comment_multi() {
        let java = JavaLang::new();
        let doc = java.render_doc_comment(&["Container class.", "", "@param <T> the element type"]);
        assert_eq!(
            doc,
            "/**\n * Container class.\n *\n * @param <T> the element type\n */"
        );
    }

    #[test]
    fn test_string_literal() {
        let java = JavaLang::new();
        assert_eq!(java.render_string_literal("hello"), "\"hello\"");
        assert_eq!(java.render_string_literal("it\"s"), "\"it\\\"s\"");
        assert_eq!(java.render_string_literal("new\nline"), "\"new\\nline\"");
    }

    #[test]
    fn test_type_keyword() {
        let java = JavaLang::new();
        assert_eq!(java.type_keyword(TypeKind::Class), "class");
        assert_eq!(java.type_keyword(TypeKind::Struct), "class");
        assert_eq!(java.type_keyword(TypeKind::Interface), "interface");
        assert_eq!(java.type_keyword(TypeKind::Trait), "interface");
        assert_eq!(java.type_keyword(TypeKind::Enum), "enum");
    }

    #[test]
    fn test_visibility_top_level() {
        let java = JavaLang::new();
        assert_eq!(
            java.render_visibility(Visibility::Public, DeclarationContext::TopLevel),
            "public "
        );
        assert_eq!(
            java.render_visibility(Visibility::Private, DeclarationContext::TopLevel),
            ""
        );
    }

    #[test]
    fn test_visibility_member() {
        let java = JavaLang::new();
        assert_eq!(
            java.render_visibility(Visibility::Public, DeclarationContext::Member),
            "public "
        );
        assert_eq!(
            java.render_visibility(Visibility::Private, DeclarationContext::Member),
            "private "
        );
        assert_eq!(
            java.render_visibility(Visibility::Protected, DeclarationContext::Member),
            "protected "
        );
    }

    #[test]
    fn test_type_before_name() {
        let java = JavaLang::new();
        assert!(java.type_decl_syntax().type_before_name);
    }

    #[test]
    fn test_return_type_is_prefix() {
        let java = JavaLang::new();
        assert!(java.type_decl_syntax().return_type_is_prefix);
    }

    #[test]
    fn test_readonly_keyword() {
        let java = JavaLang::new();
        assert_eq!(java.enum_and_annotation().readonly_keyword, "final ");
    }

    #[test]
    fn test_no_async() {
        let java = JavaLang::new();
        assert_eq!(java.function_syntax().async_keyword, "");
    }

    #[test]
    fn test_import_group_order() {
        assert_eq!(import_group_order("java.util"), 0);
        assert_eq!(import_group_order("java.io"), 0);
        assert_eq!(import_group_order("javax.persistence"), 1);
        assert_eq!(import_group_order("com.example.model"), 2);
        assert_eq!(import_group_order("org.springframework"), 2);
    }

    #[test]
    fn test_java_builder_fluent() {
        let java = JavaLang::new().with_indent("\t").with_extension("jav");
        assert_eq!(java.file_extension(), "jav");
        assert_eq!(java.block_syntax().indent_unit, "\t");
    }

    #[test]
    fn test_module_separator() {
        let java = JavaLang::new();
        assert_eq!(java.module_separator(), Some("."));
    }

    #[test]
    fn test_enum_and_annotation_config() {
        let java = JavaLang::new();
        let ea = java.enum_and_annotation();
        assert_eq!(
            ea.variant_value_format,
            crate::lang::config::VariantValueFormat::ConstructorArg
        );
        assert!(ea.variants_before_fields);
        assert_eq!(ea.variant_section_terminator, ";");
    }
}