aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc-André Lureau <marcandre.lureau@redhat.com>2021-02-12 12:52:11 +0400
committerMarc-André Lureau <marcandre.lureau@redhat.com>2021-02-12 15:05:57 +0400
commit3e25e1ca1772fc3f2039f739f8f920450dc68e50 (patch)
tree7ba6b927558f85ff4a85ecdd5a36b686c7f6a76a
parent9133a0b8022d1fb063a81cc2ba3b627c14ccdfd1 (diff)
downloadkeycodemapdb-3e25e1ca1772fc3f2039f739f8f920450dc68e50.zip
keycodemapdb-3e25e1ca1772fc3f2039f739f8f920450dc68e50.tar.gz
keycodemapdb-3e25e1ca1772fc3f2039f739f8f920450dc68e50.tar.bz2
gen: add --lang rust
The static maps use the phf crate. Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
-rw-r--r--README1
-rwxr-xr-xtools/keymap-gen49
2 files changed, 50 insertions, 0 deletions
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(),