diff options
Diffstat (limited to '')
-rw-r--r-- | ds100-macro/src/lib.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/ds100-macro/src/lib.rs b/ds100-macro/src/lib.rs new file mode 100644 index 0000000..d99c504 --- /dev/null +++ b/ds100-macro/src/lib.rs @@ -0,0 +1,46 @@ + +extern crate proc_macro; + +use proc_macro::TokenStream; +use std::{collections::HashMap, fs::File, io::BufReader}; +use std::io::prelude::*; + + + +fn readds100() -> HashMap<String, String> { + // who needs obvious filepaths, anyways? + let filename = "./DBNetz-Betriebsstellenverzeichnis-Stand2021-10.csv"; + let file = File::open(filename).unwrap(); + let reader = BufReader::new(file); + + reader.lines() + .filter_map(|l| l.ok()) + .skip(1) + .map(|l| { + let segments: Vec<&str> = l + .split(";") + .collect(); + (segments[1].to_owned(),segments[2].to_owned()) + }) + .collect::<HashMap<String,String>>() +} + + +// rust macros are a strange kind of horrible, tbh +#[proc_macro] +pub fn make_ds100(_item: TokenStream) -> TokenStream { + let symbols = readds100(); + + format!( + "pub fn ds100(key: &str) -> Option<&str> {{ + match key {{ + {} + _ => None + }} + }}", + symbols.iter() + .map(|(k,v)| format!("\"{}\" => Some(\"{}\"),",k,v)) + .collect::<Vec<_>>() + .join("\n") + ).parse().unwrap() +} |