Skip to content

Commit

Permalink
feat: Allow for vector types in custom flag
Browse files Browse the repository at this point in the history
  • Loading branch information
DashieTM committed Mar 21, 2024
1 parent 7852e06 commit 4b550f3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "re_set-lib"
version = "2.7.0"
version = "2.8.0"
edition = "2021"
description = "Data structure library for ReSet"
repository = "https://github.com/Xetibo/ReSet-Lib"
Expand Down
34 changes: 24 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![feature(trait_upcasting)]
#![feature(unsized_fn_params)]
use directories_next as dirs;
use std::{fmt, fs, path::PathBuf};
use std::{fmt, fs, path::PathBuf, slice::Iter};
use utils::{
flags::{Flag, Flags},
variant::{Empty, TVariant},
Expand Down Expand Up @@ -60,7 +60,7 @@ pub fn parse_flags(flags: &[String]) -> Flags {
match flag {
"--config" => handle_config(&mut parsed_flags, iter.next()),
"--plugins" => handle_plugins(&mut parsed_flags, iter.next()),
_ => handle_other(&mut parsed_flags, flag, iter.next()),
_ => handle_other(&mut parsed_flags, flag, &mut iter),
}
}
parsed_flags
Expand Down Expand Up @@ -128,7 +128,7 @@ fn handle_plugins<'a>(flags: &mut Flags<'a>, file: Option<&'a String>) {
flags.0.push(Flag::PluginDir(path));
}

fn handle_other<'a>(flags: &mut Flags<'a>, flag: &'a str, value: Option<&'a String>) {
fn handle_other<'a>(flags: &mut Flags<'a>, flag: &'a str, values: &mut Iter<String>) {
if !flag.starts_with('-') || !flag.starts_with("--") {
ERROR!(
"/tmp/reset_lib_log",
Expand All @@ -137,14 +137,28 @@ fn handle_other<'a>(flags: &mut Flags<'a>, flag: &'a str, value: Option<&'a Stri
);
return;
}
if let Some(value) = value {
flags.0.push(Flag::Other((
if values.len() == 0 {
return;
}
let mut parsed_flags = Vec::new();
loop {
let next = values.next();
if let Some(value) = next {
parsed_flags.push(value.clone());
} else {
break;
}
}
match parsed_flags.len() {
0 => flags
.0
.push(Flag::Other((flag.to_string(), Empty {}.into_variant()))),
1 => flags.0.push(Flag::Other((
flag.to_string(),
value.clone().into_variant(),
)))
} else {
flags
parsed_flags.pop().unwrap().into_variant(),
))),
_ => flags
.0
.push(Flag::Other((flag.to_string(), Empty {}.into_variant())))
.push(Flag::Other((flag.to_string(), parsed_flags.into_variant()))),
}
}
7 changes: 4 additions & 3 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fn test_custom_flag() {
String::from("binary name"),
String::from("--something"),
String::from("test.txt"),
String::from("test2.txt"),
];
let flags = parse_flags(&command_flags);
let matched_name: &String;
Expand All @@ -79,10 +80,10 @@ fn test_custom_flag() {
assert!(!flags.0.is_empty());
assert_eq!(matched_name, &String::from("--something"));
assert_eq!(
matched_value.to_value_cloned::<String>().unwrap(),
String::from("test.txt")
matched_value.to_value_cloned::<Vec<String>>().unwrap(),
vec![String::from("test.txt"), String::from("test2.txt")]
.into_variant()
.to_value_cloned::<String>()
.to_value_cloned::<Vec<String>>()
.unwrap()
);
}
Expand Down
18 changes: 15 additions & 3 deletions src/utils/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl TVariant for String {
Box::new(self.clone())
}
}
impl<T: Debug + Send + 'static> TVariant for Option<T>
impl<T: TVariant + 'static> TVariant for Option<T>
where
T: Clone,
{
Expand All @@ -119,7 +119,7 @@ where
Box::new(self.clone())
}
}
impl<T: Debug + Send + 'static> TVariant for Vec<T>
impl<T: TVariant + 'static> TVariant for Vec<T>
where
T: Clone,
{
Expand All @@ -130,7 +130,7 @@ where
Box::new(self.clone())
}
}
impl<K: Debug + Send + 'static, V: Debug + Send + 'static> TVariant for HashMap<K, V>
impl<K: TVariant + 'static, V: TVariant + Send + 'static> TVariant for HashMap<K, V>
where
K: Clone,
V: Clone,
Expand Down Expand Up @@ -158,6 +158,18 @@ pub struct Variant {
kind: TypeId,
}

impl Debug for Variant {}

impl TVariant for Variant {
fn into_variant(self) -> Variant {
self.clone()
}

fn value(&self) -> Box<dyn TVariant> {
Box::new(self.clone())
}
}

impl Clone for Variant {
fn clone(&self) -> Self {
Self {
Expand Down

0 comments on commit 4b550f3

Please sign in to comment.