reproto-backend-java 0.3.36

Rethinking Protocol Generators
//! Module that adds the @ConstructorProperties annotation to every constructor.

use codegen::{ClassAdded, ClassCodegen, Configure};
use core::errors::Result;
use genco::java::imported;
use genco::{Java, Quoted, Tokens};

pub struct Module;

impl Module {
    pub fn initialize(self, e: Configure) {
        e.options
            .class_generators
            .push(Box::new(ConstructorProperties::new()));
    }
}

pub struct ConstructorProperties {
    annotation: Java<'static>,
}

impl ConstructorProperties {
    pub fn new() -> ConstructorProperties {
        ConstructorProperties {
            annotation: imported("java.beans", "ConstructorProperties"),
        }
    }
}

impl ClassCodegen for ConstructorProperties {
    fn generate(&self, e: ClassAdded) -> Result<()> {
        let args: Tokens<Java> = e.names.iter().cloned().map(Quoted::quoted).collect();
        let a = toks!["@", self.annotation.clone(), "({", args.join(", "), "})"];

        for c in &mut e.spec.constructors {
            c.annotation(a.clone());
        }

        Ok(())
    }
}