From 5f6a5cd8195b4cd54cc81ecd6978b00ac7c90c97 Mon Sep 17 00:00:00 2001 From: stuebinm Date: Sun, 31 Jul 2022 00:14:47 +0200 Subject: hacky hack json --- ds100-macro/Cargo.toml | 10 ++++++++++ ds100-macro/src/lib.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 ds100-macro/Cargo.toml create mode 100644 ds100-macro/src/lib.rs (limited to 'ds100-macro') 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 { + // 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::>() +} + + +// 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::>() + .join("\n") + ).parse().unwrap() +} -- cgit v1.2.3