From 2535e6c0469511c581a5a92ebb554ccbbae80903 Mon Sep 17 00:00:00 2001 From: nils <48135649+Nilstrieb@users.noreply.github.com> Date: Mon, 20 Sep 2021 15:34:14 +0200 Subject: [PATCH] template things --- examples/compiler.rs | 24 +++++++++++++++++++++++ src/lib.rs | 46 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 examples/compiler.rs diff --git a/examples/compiler.rs b/examples/compiler.rs new file mode 100644 index 0000000..8e20680 --- /dev/null +++ b/examples/compiler.rs @@ -0,0 +1,24 @@ +use badargs::{CliArg, CliArgInfo, Template}; +use std::collections::HashMap; + +#[derive(Default)] +struct OutFile; + +fn main() { + let args = badargs::badargs(Template { + options: { + let mut map = HashMap::new(); + map.insert( + Box::new(OutFile), + CliArgInfo { + name: "output".to_string(), + allow_short: true, + takes_value: true, + }, + ); + map + }, + }); + + let outfile = args.get::(); +} diff --git a/src/lib.rs b/src/lib.rs index 31e1bb2..d0b71dc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,43 @@ -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - assert_eq!(2 + 2, 4); +use std::any::{Any, TypeId}; +use std::collections::HashMap; + +#[derive(Debug, Clone)] +pub enum CliOption { + Flag(bool), + Value(String), +} + +pub trait CliArg { + type Content; +} + +#[derive(Debug, Clone, Default)] +pub struct Template { + pub options: HashMap, CliArgInfo>, +} + +pub struct CliArgInfo { + pub name: String, + pub allow_short: bool, + pub takes_value: bool, +} + +#[derive(Debug, Clone, Default)] +pub struct BadArgs { + options: HashMap, +} + +impl BadArgs { + pub fn get(&self) -> Option<&CliArgInfo> { + self.options.get(&T::type_id()) } } + +pub fn badargs(template: Template) -> BadArgs { + let options = template + .options + .into_iter() + .map(|(key, value)| (key.type_id(), value)) + .collect(); + BadArgs { options } +}