Skip to content

Commit

Permalink
🔮 impl From for arrays/vecs of &str to enum with variant...
Browse files Browse the repository at this point in the history
[&str; N], Vec<&str>, Vec<String> -> Enum1::Variant(Vec<Enum2::String(v)>)
  • Loading branch information
mockersf committed Dec 18, 2019
1 parent 7fab34a commit bf007ff
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 12 deletions.
24 changes: 12 additions & 12 deletions examples/stacked_bar_chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.color(DefWithConditionMarkPropFieldDefGradientStringNullBuilder::default()
.field("weather")
.scale(ScaleBuilder::default()
.domain(vec![
Equal::String("sun".to_string()),
Equal::String("fog".to_string()),
Equal::String("drizzle".to_string()),
Equal::String("rain".to_string()),
Equal::String("snow".to_string()),
.domain([
"sun",
"fog",
"drizzle",
"rain",
"snow",
])
.range(vec![
RangeRange::String("#e7ba52".to_string()),
RangeRange::String("#c7c7c7".to_string()),
RangeRange::String("#aec7e8".to_string()),
RangeRange::String("#1f77b4".to_string()),
RangeRange::String("#9467bd".to_string()),
.range([
"#e7ba52",
"#c7c7c7",
"#aec7e8",
"#1f77b4",
"#9467bd",
])
.build()?)
.build()?)
Expand Down
54 changes: 54 additions & 0 deletions src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ macro_rules! from_into_string{
};
}

// enum that have a `String(String)`variant
// `grep -B 5 "String(String)" src/schema.rs | grep "pub enum" | sort | sed 's/pub enum \(.*\) {/\1/'`
from_into_string!(
ClearUnion,
Color,
Expand Down Expand Up @@ -86,6 +88,58 @@ from_into_string!(
ValueUnion,
);

// for every enum with a variant that takes a vec of an enum with a String(String) variant
// could be better with const generics : https://github.com/rust-lang/rust/issues/44580
// as in https://github.com/rust-lang/rust/issues/61415. For now, macro will generate impl for arrays up to size 32
macro_rules! from_into_array_of_str{
( $( $e:ident::$v:ident(Vec<$t:ident>) ),* $(,)? ) => {
from_into_array_of_str!($( $e::$v(Vec<$t>), )*, 32,31,30,29,28,27,26,25,24,23,
22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0);
};
( $( $e:ident::$v:ident(Vec<$t:ident>) ,)*, $end:expr ) => {
// implementations for Vec
$(
impl From<Vec<&str>> for $e
{
fn from(v: Vec<&str>) -> Self {
$e::$v(v.iter().map(|s| $t::String(s.to_string())).collect())
}
}
impl From<Vec<String>> for $e
{
fn from(v: Vec<String>) -> Self {
$e::$v(v.into_iter().map(|s| $t::String(s)).collect())
}
}
)*
};
( $( $e:ident::$v:ident(Vec<$t:ident>) ,)*, $i:expr, $($tail:expr),+ ) => {
// implementations for array of size $i
$(
impl From<[&str; $i]> for $e
{
fn from(v: [&str; $i]) -> Self {
$e::$v(v.iter().map(|s| $t::String(s.to_string())).collect())
}
}
)*
from_into_array_of_str!($( $e::$v(Vec<$t>), )*, $($tail),*);
};
}

// enums that have a variant that take a Vec of an enum with a `String` variant
// grep -B 5 "String(String)" src/schema.rs | grep "pub enum" | sort | sed 's/pub enum \(.*\) {/\1/' | \
// xargs -I % sh -c "grep '(Vec<%>),' src/schema.rs | \
// xargs -I {} sh -c 'grep -B 5 \"{}\" src/schema.rs | grep \"pub enum\" | sed \"s/pub enum \(.*\) {/\1/\" | \
// xargs -I $ sh -c \"echo \\\"$::{}\\\"\"'" | sort | uniq
from_into_array_of_str!(
DomainUnion::UnionArray(Vec<Equal>),
InitValue::UnionArray(Vec<Equal>),
ScaleRange::UnionArray(Vec<RangeRange>),
SortArray::UnionArray(Vec<Equal>),
SortUnion::UnionArray(Vec<Equal>),
);

// #[cfg(test)]
// mod tests {
// use super::*;
Expand Down

1 comment on commit bf007ff

@mockersf
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixes #1

Please sign in to comment.