codeviz_java

Crates.iocodeviz_java
lib.rscodeviz_java
version0.2.2
sourcesrc
created_at2017-06-25 15:20:35.991038
updated_at2017-10-03 11:25:42.723864
descriptionFlexible code generator for Rust
homepagehttps://github.com/udoprog/codeviz
repositoryhttps://github.com/udoprog/codeviz
max_upload_size
id20598
size36,807
John-John Tedro (udoprog)

documentation

https://docs.rs/codeviz_java

README

codeviz

Build Status crates.io

codeviz is a simple code generator for rust, specifically written for use in reproto.

This project is inspired by JavaPoet (https://github.com/square/javapoet)

Example

#[macro_use]
extern crate codeviz_macros;
extern crate codeviz_java;

use codeviz_java::*;

fn main() {
  let string_type = Type::class("java.lang", "String");
  let list_type = Type::class("java.util", "List");
  let json_creator_type = Type::class("com.fasterxml.jackson.annotation", "JsonCreator");
  let list_of_strings = list_type.with_arguments(vec![&string_type]);

  let values_field = FieldSpec::new(mods![Modifier::Private, Modifier::Final],
                                    &list_of_strings,
                                    "values");

  let values_argument =
      ArgumentSpec::new(mods![Modifier::Final], &list_of_strings, "values");

  let mut constructor = ConstructorSpec::new(mods![Modifier::Public]);
  constructor.push_annotation(AnnotationSpec::new(json_creator_type));
  constructor.push_argument(&values_argument);
  constructor.push(java_stmt!["this.values = ", values_argument]);

  let mut values_getter = MethodSpec::new(mods![Modifier::Public], "getValues");
  values_getter.returns(&list_of_strings);
  values_getter.push(java_stmt!["return this.", &values_field]);

  let mut class = ClassSpec::new(mods![Modifier::Public], "Test");
  class.push_field(&values_field);
  class.push_constructor(&constructor);
  class.push_method(&values_getter);

  let mut file = FileSpec::new("se.tedro");
  file.push_class(&class);

  let result = file.format();
}
Commit count: 54

cargo fmt