diff options
Diffstat (limited to '')
-rw-r--r-- | ds100-macro/Cargo.toml | 10 | ||||
-rw-r--r-- | ds100-macro/src/lib.rs | 46 |
2 files changed, 56 insertions, 0 deletions
diff --git a/ds100-macro/Cargo.toml b/ds100-macro/Cargo.toml new file mode 100644 index 0000000..1b1f7ee --- /dev/null +++ b/ds100-macro/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "ds100-macro" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +proc-macro = true + +[dependencies] 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() +} |