From 3e25e1ca1772fc3f2039f739f8f920450dc68e50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Fri, 12 Feb 2021 12:52:11 +0400 Subject: gen: add --lang rust MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The static maps use the phf crate. Signed-off-by: Marc-André Lureau --- README | 1 + tools/keymap-gen | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/README b/README index a3c3c9f..8b4a845 100644 --- a/README +++ b/README @@ -85,6 +85,7 @@ programming languages / environments - GLib2 (standard C, but with GLib2 data types) - Python - Perl + - Rust Usage diff --git a/tools/keymap-gen b/tools/keymap-gen index 22b4f71..4e2fafd 100755 --- a/tools/keymap-gen +++ b/tools/keymap-gen @@ -752,6 +752,54 @@ class StdCppHeaderLanguageGenerator(CppHeaderLanguageGenerator): def __init__(self): super(StdCppHeaderLanguageGenerator, self).__init__("unsigned short", "char *", "unsigned int") + +class RustLanguageGenerator(LanguageSrcGenerator): + + def _boilerplate(self, lines): + print("//") + for line in lines: + print("// %s" % line) + print("//") + + def _array_start(self, varname, length, defvalue, fromtype, totype): + if fromtype == self.TYPE_ENUM: + raise NotImplementedError("Enums not supported as source in Rust generator") + + totypename = "&'static str" if totype == self.TYPE_STRING else "u16" + if fromtype != self.TYPE_STRING: + print("pub static %s: &'static [%s] = &[" % (varname.upper(), totypename)) + else: + print("pub static %s: phf::Map<&'static str, %s> = phf::phf_map! {" % + (varname.upper(), totypename)) + + def _array_end(self, fromtype, totype): + if fromtype != self.TYPE_STRING: + print("];") + else: + print("};") + + def _array_entry(self, index, value, comment, fromtype, totype): + none = "\"\"" if totype == self.TYPE_STRING else "0" + if fromtype == self.TYPE_INT: + if value is None: + print(" %s, // %s" % (none, comment)) + elif totype == self.TYPE_INT: + print(" 0x%x, // %s" % (value, comment)) + elif totype == self.TYPE_ENUM: + print(" %s, // %s" % (value, comment)) + else: + print(" \"%s\", // %s" % (value, comment)) + else: + if value is None: + print(" \"%s\" => %s, // %s" % (index, none, comment)) + elif totype == self.TYPE_INT: + print(" \"%s\" => 0x%x, // %s" % (index, value, comment)) + elif totype == self.TYPE_ENUM: + print(" \"%s\" => %s, // %s" % (index, value, comment)) + else: + print(" \"%s\" => \"%s\", // %s" % (index, value, comment)) + + class PythonLanguageGenerator(LanguageSrcGenerator): def _boilerplate(self, lines): @@ -984,6 +1032,7 @@ SRC_GENERATORS = { "python3": PythonLanguageGenerator(), "perl": PerlLanguageGenerator(), "js": JavaScriptLanguageGenerator(), + "rust": RustLanguageGenerator(), } DOC_GENERATORS = { "pod": PodLanguageGenerator(), -- cgit v1.1