summaryrefslogtreecommitdiff
path: root/symbolmacro
diff options
context:
space:
mode:
authorstuebinm2021-06-23 18:20:26 +0200
committerstuebinm2021-06-23 18:20:26 +0200
commit4513c6626a34f737482c102825be7c2ca1b43eff (patch)
tree9accaa069e11ef9aabf45a3bdcd2dae0ca498833 /symbolmacro
initial commit
Diffstat (limited to 'symbolmacro')
-rw-r--r--symbolmacro/.gitignore1
-rw-r--r--symbolmacro/Cargo.lock5
-rw-r--r--symbolmacro/Cargo.toml11
-rw-r--r--symbolmacro/src/lib.rs53
4 files changed, 70 insertions, 0 deletions
diff --git a/symbolmacro/.gitignore b/symbolmacro/.gitignore
new file mode 100644
index 0000000..ea8c4bf
--- /dev/null
+++ b/symbolmacro/.gitignore
@@ -0,0 +1 @@
+/target
diff --git a/symbolmacro/Cargo.lock b/symbolmacro/Cargo.lock
new file mode 100644
index 0000000..1ed48a4
--- /dev/null
+++ b/symbolmacro/Cargo.lock
@@ -0,0 +1,5 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "symbolmacro"
+version = "0.1.0"
diff --git a/symbolmacro/Cargo.toml b/symbolmacro/Cargo.toml
new file mode 100644
index 0000000..78ad418
--- /dev/null
+++ b/symbolmacro/Cargo.toml
@@ -0,0 +1,11 @@
+[package]
+name = "symbolmacro"
+version = "0.1.0"
+authors = ["stuebinm <stuebinm@disroot.org>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+[lib]
+proc-macro = true
+
+[dependencies]
diff --git a/symbolmacro/src/lib.rs b/symbolmacro/src/lib.rs
new file mode 100644
index 0000000..f99ccd9
--- /dev/null
+++ b/symbolmacro/src/lib.rs
@@ -0,0 +1,53 @@
+
+extern crate proc_macro;
+
+use proc_macro::TokenStream;
+use std::{collections::HashMap, fs::File, io::BufReader};
+use std::io::prelude::*;
+
+
+
+fn fetchsymbols() -> HashMap<String, char> {
+ // who needs obvious filepaths, anyways?
+ let filename = "symbols";
+ let file = File::open(filename).unwrap();
+ let reader = BufReader::new(file);
+
+ reader.lines()
+ .filter_map(|l| l.ok())
+ .filter_map(|l| {
+ let segments: Vec<&str> = l
+ .split_whitespace()
+ .collect();
+ match segments[..] {
+ [] => None,
+ [s,"code:",code,..] => {
+ let codepoint = u32::from_str_radix(&code[2..], 16).ok()?;
+ let unichar = std::char::from_u32(codepoint)?;
+ Some((s[2..s.len()-1].to_string(), unichar))
+ },
+ _ => None,
+ }
+ })
+ .collect::<HashMap<String,char>>()
+}
+
+
+// rust macros are a strange kind of horrible, tbh
+#[proc_macro]
+pub fn make_symbols(_item: TokenStream) -> TokenStream {
+ let symbols = fetchsymbols();
+
+ format!(
+ "fn symbol(key: &str) -> Option<&'static str> {{
+ match key {{
+ {}
+ _ => None
+ }}
+ }}",
+ symbols.iter()
+ .map(|(k,v)| format!("\"{}\" => Some(\"{}\"),",k,v))
+ .collect::<Vec<_>>()
+ .join("\n")
+ ).parse().unwrap()
+}