1#!/usr/bin/env python3
2# ex: set filetype=python:
3
4"""Generate code to handle XDR enum types"""
5
6from generators import SourceGenerator, create_jinja2_environment
7from xdr_ast import _XdrEnum, public_apis
8
9
10class XdrEnumGenerator(SourceGenerator):
11    """Generate source code for XDR enum types"""
12
13    def __init__(self, language: str, peer: str):
14        """Initialize an instance of this class"""
15        self.environment = create_jinja2_environment(language, "enum")
16        self.peer = peer
17
18    def emit_declaration(self, node: _XdrEnum) -> None:
19        """Emit one declaration pair for an XDR enum type"""
20        if node.name in public_apis:
21            template = self.environment.get_template("declaration/close.j2")
22            print(template.render(name=node.name))
23
24    def emit_definition(self, node: _XdrEnum) -> None:
25        """Emit one definition for an XDR enum type"""
26        template = self.environment.get_template("definition/open.j2")
27        print(template.render(name=node.name))
28
29        template = self.environment.get_template("definition/enumerator.j2")
30        for enumerator in node.enumerators:
31            print(template.render(name=enumerator.name, value=enumerator.value))
32
33        template = self.environment.get_template("definition/close.j2")
34        print(template.render(name=node.name))
35
36    def emit_decoder(self, node: _XdrEnum) -> None:
37        """Emit one decoder function for an XDR enum type"""
38        template = self.environment.get_template("decoder/enum.j2")
39        print(template.render(name=node.name))
40
41    def emit_encoder(self, node: _XdrEnum) -> None:
42        """Emit one encoder function for an XDR enum type"""
43        template = self.environment.get_template("encoder/enum.j2")
44        print(template.render(name=node.name))
45