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
//! Specialization for Java code generation.
//!
//! # Examples
//!
//! String quoting in Java:
//!
//! ```rust
//! #[feature(proc_macro_hygiene)]
//! use genco::prelude::*;
//!
//! let toks: java::Tokens = quote!(#("hello \n world".quoted()));
//! assert_eq!("\"hello \\n world\"", toks.to_string().unwrap());
//! ```

mod block_comment;

pub use self::block_comment::BlockComment;

use crate as genco;
use crate::{quote, quote_in, Formatter, ItemStr, Lang, LangItem};
use std::collections::{BTreeSet, HashMap};
use std::fmt;

/// Tokens container specialized for Java.
pub type Tokens = crate::Tokens<Java>;

impl_type_basics!(Java, TypeEnum<'a>, TypeTrait, TypeBox, TypeArgs, {Primitive, Void, Type, Optional, Local});

/// Trait implemented by all types
pub trait TypeTrait: 'static + fmt::Debug + LangItem<Java> {
    /// Coerce trait into an enum that can be used for type-specific operations
    fn as_enum(&self) -> TypeEnum<'_>;

    /// Get package type belongs to.
    fn name(&self) -> &str;

    /// Get package type belongs to.
    fn package(&self) -> Option<&str> {
        None
    }

    /// Get generic arguments associated with type.
    fn arguments(&self) -> Option<&[TypeBox]> {
        None
    }

    /// Process which kinds of imports to deal with.
    fn type_imports(&self, _: &mut BTreeSet<(ItemStr, ItemStr)>) {}
}

const JAVA_LANG: &'static str = "java.lang";
const SEP: &'static str = ".";

/// Short primitive type.
pub const SHORT: Primitive = Primitive {
    primitive: "short",
    boxed: "Short",
};

/// Integer primitive type.
pub const INTEGER: Primitive = Primitive {
    primitive: "int",
    boxed: "Integer",
};

/// Long primitive type.
pub const LONG: Primitive = Primitive {
    primitive: "long",
    boxed: "Long",
};

/// Float primitive type.
pub const FLOAT: Primitive = Primitive {
    primitive: "float",
    boxed: "Float",
};

/// Double primitive type.
pub const DOUBLE: Primitive = Primitive {
    primitive: "double",
    boxed: "Double",
};

/// Char primitive type.
pub const CHAR: Primitive = Primitive {
    primitive: "char",
    boxed: "Character",
};

/// Boolean primitive type.
pub const BOOLEAN: Primitive = Primitive {
    primitive: "boolean",
    boxed: "Boolean",
};

/// Byte primitive type.
pub const BYTE: Primitive = Primitive {
    primitive: "byte",
    boxed: "Byte",
};

/// Void type.
pub const VOID: Void = Void(());

/// Configuration for Java formatting.
#[derive(Debug)]
pub struct Config {
    /// Package to use.
    package: Option<ItemStr>,

    /// Types which has been imported into the local namespace.
    imported: HashMap<String, String>,
}

impl Config {
    /// Configure package to use.
    pub fn with_package(self, package: impl Into<ItemStr>) -> Self {
        Self {
            package: Some(package.into()),
            ..self
        }
    }
}

impl Default for Config {
    fn default() -> Self {
        Self {
            package: Default::default(),
            imported: Default::default(),
        }
    }
}

impl_modifier! {
    /// A Java modifier.
    ///
    /// A vector of modifiers have a custom implementation, allowing them to be
    /// formatted with a spacing between them in the language-recommended order.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use genco::prelude::*;
    /// use java::Modifier::*;
    ///
    /// let toks: java::Tokens = quote!(#(vec![Public, Final, Static]));
    ///
    /// assert_eq!("public static final", toks.to_string().unwrap());
    /// ```
    pub enum Modifier<Java> {
        /// The `default` modifier.
        Default => "default",
        /// The `public` modifier.
        Public => "public",
        /// The `protected` modifier.
        Protected => "protected",
        /// The `private` modifier.
        Private => "private",
        /// The `abstract` modifier.
        Abstract => "abstract",
        /// The `static` modifier.
        Static => "static",
        /// The `final` modifier.
        Final => "final",
        /// The `native` modifier.
        Native => "native",
    }
}

/// A class.
#[derive(Debug, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)]
pub struct Type {
    /// Package of the class.
    package: ItemStr,
    /// Name  of class.
    name: ItemStr,
    /// Path of class when nested.
    path: Vec<ItemStr>,
    /// Arguments of the class.
    arguments: Vec<TypeBox>,
}

impl Type {
    /// Extend the type with a nested path.
    ///
    /// This discards any arguments associated with it.
    pub fn path<P: Into<ItemStr>>(self, part: P) -> Self {
        let mut path = self.path;
        path.push(part.into());

        Self {
            package: self.package,
            name: self.name,
            path: path,
            arguments: vec![],
        }
    }

    /// Add arguments to the given variable.
    ///
    /// Only applies to classes, any other will return the same value.
    pub fn with_arguments(self, args: impl TypeArgs) -> Self {
        Self {
            package: self.package,
            name: self.name,
            path: self.path,
            arguments: args.into_args(),
        }
    }

    /// Get the raw type.
    ///
    /// A raw type is one without generic arguments.
    pub fn as_raw(self) -> Self {
        Self {
            package: self.package,
            name: self.name,
            path: self.path,
            arguments: vec![],
        }
    }

    /// Check if type is generic.
    pub fn is_generic(&self) -> bool {
        !self.arguments.is_empty()
    }
}

impl LangItem<Java> for Type {
    fn format(&self, out: &mut Formatter, config: &mut Config, level: usize) -> fmt::Result {
        {
            let file_package = config.package.as_ref().map(|p| p.as_ref());
            let imported = config.imported.get(self.name.as_ref()).map(String::as_str);
            let pkg = Some(self.package.as_ref());

            if self.package.as_ref() != JAVA_LANG && imported != pkg && file_package != pkg {
                out.write_str(self.package.as_ref())?;
                out.write_str(SEP)?;
            }
        }

        {
            out.write_str(self.name.as_ref())?;

            let mut it = self.path.iter();

            while let Some(n) = it.next() {
                out.write_str(".")?;
                out.write_str(n.as_ref())?;
            }
        }

        if !self.arguments.is_empty() {
            out.write_str("<")?;

            let mut it = self.arguments.iter().peekable();

            while let Some(argument) = it.next() {
                argument.format(out, config, level + 1usize)?;

                if it.peek().is_some() {
                    out.write_str(", ")?;
                }
            }

            out.write_str(">")?;
        }

        Ok(())
    }

    fn as_import(&self) -> Option<&dyn TypeTrait> {
        Some(self)
    }
}

impl TypeTrait for Type {
    fn as_enum(&self) -> TypeEnum<'_> {
        TypeEnum::Type(self)
    }

    fn name(&self) -> &str {
        &*self.name
    }

    fn package(&self) -> Option<&str> {
        Some(&*self.package)
    }

    fn arguments(&self) -> Option<&[TypeBox]> {
        Some(&self.arguments)
    }

    fn type_imports(&self, modules: &mut BTreeSet<(ItemStr, ItemStr)>) {
        for argument in &self.arguments {
            if let TypeEnum::Type(ty) = argument.as_enum() {
                ty.type_imports(modules);
            }
        }

        modules.insert((self.package.clone(), self.name.clone()));
    }
}

/// The void type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Void(());

impl TypeTrait for Void {
    fn as_enum(&self) -> TypeEnum<'_> {
        TypeEnum::Void(self)
    }

    fn name(&self) -> &str {
        "void"
    }
}

impl LangItem<Java> for Void {
    fn format(&self, out: &mut Formatter, _: &mut Config, level: usize) -> fmt::Result {
        if level > 0 {
            out.write_str("Void")
        } else {
            out.write_str("void")
        }
    }
}

/// A primitive type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Primitive {
    /// The boxed variant of the primitive type.
    boxed: &'static str,
    /// The primitive-primitive type.
    primitive: &'static str,
}

impl Primitive {
    /// Get a boxed version of a primitive type.
    pub const fn into_boxed(self) -> Type {
        Type {
            package: ItemStr::Static(JAVA_LANG),
            name: ItemStr::Static(self.boxed),
            path: vec![],
            arguments: vec![],
        }
    }
}

impl LangItem<Java> for Primitive {
    fn format(&self, out: &mut Formatter, _: &mut Config, level: usize) -> fmt::Result {
        if level > 0 {
            out.write_str(self.boxed)
        } else {
            out.write_str(self.primitive)
        }
    }
}

impl TypeTrait for Primitive {
    fn name(&self) -> &str {
        self.primitive
    }

    fn as_enum(&self) -> TypeEnum<'_> {
        TypeEnum::Primitive(self)
    }

    fn package(&self) -> Option<&str> {
        Some(JAVA_LANG)
    }
}

/// A local name with no specific qualification.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Local {
    /// Name of class.
    name: ItemStr,
}

impl TypeTrait for Local {
    fn as_enum(&self) -> TypeEnum<'_> {
        TypeEnum::Local(self)
    }

    fn name(&self) -> &str {
        &*self.name
    }
}

impl LangItem<Java> for Local {
    fn format(&self, out: &mut Formatter, _: &mut Config, _: usize) -> fmt::Result {
        out.write_str(&*self.name)
    }

    fn as_import(&self) -> Option<&dyn TypeTrait> {
        Some(self)
    }
}

/// An optional type.
#[derive(Debug, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)]
pub struct Optional {
    /// The type that is optional.
    pub value: TypeBox,
    /// The complete optional field type, including wrapper.
    pub field: TypeBox,
}

impl TypeTrait for Optional {
    fn as_enum(&self) -> TypeEnum<'_> {
        TypeEnum::Optional(self)
    }

    fn name(&self) -> &str {
        self.value.name()
    }

    fn package(&self) -> Option<&str> {
        self.value.package()
    }

    fn arguments(&self) -> Option<&[TypeBox]> {
        self.value.arguments()
    }

    fn type_imports(&self, modules: &mut BTreeSet<(ItemStr, ItemStr)>) {
        self.value.type_imports(modules);
    }
}

impl Optional {
    /// Get the field type (includes optionality).
    pub fn as_field(self) -> TypeBox {
        self.field.clone()
    }

    /// Get the value type (strips optionality).
    pub fn as_value(self) -> TypeBox {
        self.value.clone()
    }
}

impl LangItem<Java> for Optional {
    fn format(&self, out: &mut Formatter, config: &mut Config, level: usize) -> fmt::Result {
        self.field.format(out, config, level)
    }

    fn as_import(&self) -> Option<&dyn TypeTrait> {
        Some(self)
    }
}

/// Language specialization for Java.
pub struct Java(());

impl Java {
    fn imports(tokens: &Tokens, config: &mut Config) -> Option<Tokens> {
        let mut modules = BTreeSet::new();

        let file_package = config.package.as_ref().map(|p| p.as_ref());

        for import in tokens.walk_imports() {
            import.type_imports(&mut modules);
        }

        if modules.is_empty() {
            return None;
        }

        let mut out = Tokens::new();

        for (package, name) in modules {
            if config.imported.contains_key(&*name) {
                continue;
            }

            if &*package == JAVA_LANG {
                continue;
            }

            if Some(&*package) == file_package.as_deref() {
                continue;
            }

            out.append(quote!(import #(package.clone())#(SEP)#(name.clone());));
            out.push();

            config
                .imported
                .insert(name.to_string(), package.to_string());
        }

        Some(out)
    }
}

impl Lang for Java {
    type Config = Config;
    type Import = dyn TypeTrait;

    fn quote_string(out: &mut Formatter, input: &str) -> fmt::Result {
        use std::fmt::Write as _;

        out.write_char('"')?;

        for c in input.chars() {
            match c {
                '\t' => out.write_str("\\t")?,
                '\u{0007}' => out.write_str("\\b")?,
                '\n' => out.write_str("\\n")?,
                '\r' => out.write_str("\\r")?,
                '\u{0014}' => out.write_str("\\f")?,
                '\'' => out.write_str("\\'")?,
                '"' => out.write_str("\\\"")?,
                '\\' => out.write_str("\\\\")?,
                c => out.write_char(c)?,
            }
        }

        out.write_char('"')?;

        Ok(())
    }

    fn write_file(
        tokens: Tokens,
        out: &mut Formatter,
        config: &mut Self::Config,
        level: usize,
    ) -> fmt::Result {
        let mut toks = Tokens::new();

        if let Some(ref package) = config.package {
            quote_in!(toks => package #package;);
            toks.push_line();
        }

        if let Some(imports) = Self::imports(&tokens, config) {
            toks.append(imports);
            toks.push_line();
        }

        toks.extend(tokens);
        toks.format(out, config, level)
    }
}

/// Setup an imported element.
///
/// # Examples
///
/// ```rust
/// #[feature(proc_macro_hygiene)]
/// use genco::prelude::*;
///
/// let integer = java::imported("java.lang", "Integer");
/// let a = java::imported("java.io", "A");
/// let b = java::imported("java.io", "B");
/// let ob = java::imported("java.util", "B");
/// let ob_a = ob.clone().with_arguments(a.clone());
///
/// let toks = quote! {
///     #integer
///     #a
///     #b
///     #ob
///     #ob_a
/// };
///
/// assert_eq!(
///     vec![
///         "import java.io.A;",
///         "import java.io.B;",
///         "",
///         "Integer",
///         "A",
///         "B",
///         "java.util.B",
///         "java.util.B<A>"
///     ],
///     toks.to_file_vec().unwrap()
/// );
/// ```
pub fn imported<P: Into<ItemStr>, N: Into<ItemStr>>(package: P, name: N) -> Type {
    Type {
        package: package.into(),
        name: name.into(),
        path: vec![],
        arguments: vec![],
    }
}

/// Setup a local element from borrowed components.
pub fn local<N: Into<ItemStr>>(name: N) -> Local {
    Local { name: name.into() }
}

/// Setup an optional type.
pub fn optional<I: Into<TypeBox>, F: Into<TypeBox>>(value: I, field: F) -> Optional {
    Optional {
        value: value.into(),
        field: field.into(),
    }
}

/// Format a doc comment where each line is preceeded by `///`.
///
/// # Examples
///
/// ```rust
/// #[feature(proc_macro_hygiene)]
/// use genco::prelude::*;
///
/// use std::iter;
///
/// let toks = quote! {
///     #(java::block_comment(vec!["first line", "second line"]))
///     #(java::block_comment(iter::empty::<&str>()))
///     #(java::block_comment(vec!["third line"]))
/// };
///
/// assert_eq!(
///     vec![
///         "/**",
///         " * first line",
///         " * second line",
///         " */",
///         "/**",
///         " * third line",
///         " */",
///     ],
///     toks.to_file_vec().unwrap()
/// );
/// ```
pub fn block_comment<T>(comment: T) -> BlockComment<T>
where
    T: IntoIterator,
    T::Item: Into<ItemStr>,
{
    BlockComment(comment)
}