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
#[macro_use]
extern crate log;
#[macro_use]
extern crate genco;
extern crate reproto_backend as backend;
#[macro_use]
extern crate serde_derive;
extern crate reproto_core as core;
extern crate reproto_manifest as manifest;
extern crate toml;
extern crate serde;

mod processor;
mod java_backend;
mod java_options;
mod java_file;
mod listeners;
mod java_field;
mod module;
mod codegen;
mod utils;

use self::ErrorKind::*;
use backend::{ArgMatches, Environment};
use backend::errors::*;
use core::Context;
use java_backend::JavaBackend;
use java_options::JavaOptions;
use listeners::{Configure, Listeners};
use manifest::{Lang, Manifest, NoModule, TryFromToml, self as m};
use std::path::Path;
use std::rc::Rc;
use utils::Utils;

pub const JAVA_CONTEXT: &str = "java";

#[derive(Default)]
pub struct JavaLang;

impl Lang for JavaLang {
    type Module = JavaModule;
}

#[derive(Debug)]
pub enum JavaModule {
    Jackson,
    Lombok,
    Grpc,
    Builder,
    ConstructorProperties,
    Mutable,
    Nullable,
    OkHttp(module::OkHttpConfig),
}

impl TryFromToml for JavaModule {
    fn try_from_string(path: &Path, id: &str, value: String) -> m::errors::Result<Self> {
        use self::JavaModule::*;

        let result = match id {
            "jackson" => Jackson,
            "lombok" => Lombok,
            "grpc" => Grpc,
            "builder" => Builder,
            "constructor_properties" => ConstructorProperties,
            "mutable" => Mutable,
            "nullable" => Nullable,
            "okhttp" => OkHttp(module::OkHttpConfig::default()),
            _ => return NoModule::illegal(path, id, value),
        };

        Ok(result)
    }

    fn try_from_value(path: &Path, id: &str, value: toml::Value) -> m::errors::Result<Self> {
        use self::JavaModule::*;

        let result = match id {
            "jackson" => Jackson,
            "lombok" => Lombok,
            "grpc" => Grpc,
            "builder" => Builder,
            "constructor_properties" => ConstructorProperties,
            "mutable" => Mutable,
            "nullable" => Nullable,
            "okhttp" => OkHttp(value.try_into()?),
            _ => return NoModule::illegal(path, id, value),
        };

        Ok(result)
    }
}

fn setup_listeners(modules: Vec<JavaModule>, utils: &Rc<Utils>) -> JavaOptions {
    use self::JavaModule::*;

    let mut options = JavaOptions::new();

    for module in modules {
        let listener: Box<Listeners> = match module {
            Jackson => Box::new(module::Jackson),
            Lombok => Box::new(module::Lombok),
            Grpc => Box::new(module::Grpc),
            Builder => Box::new(module::Builder),
            ConstructorProperties => Box::new(module::ConstructorProperties),
            Mutable => Box::new(module::Mutable),
            Nullable => Box::new(module::Nullable),
            OkHttp(config) => Box::new(module::OkHttp::new(config)),
        };

        listener.configure(Configure {
            options: &mut options,
            utils: utils,
        })
    }

    options
}

pub fn compile(
    _ctx: Rc<Context>,
    env: Environment,
    _matches: &ArgMatches,
    manifest: Manifest<JavaLang>,
) -> Result<()> {
    let out = manifest.output.ok_or(MissingOutput)?;
    let env = Rc::new(env);
    let utils = Rc::new(Utils::new(&env));
    let options = setup_listeners(manifest.modules, &utils);
    let backend = JavaBackend::new(&env, &utils, options);
    backend.compile(&out)
}