diff options
Diffstat (limited to '')
-rw-r--r-- | symbolmacro/src/lib.rs | 53 |
1 files changed, 53 insertions, 0 deletions
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() +} |