From 47114718a11fb4154356210231baa5905f92f677 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Sun, 22 Aug 2021 14:17:59 -0400 Subject: [PATCH] Allow multi form elements --- src/pattern.rs | 66 +++++++++++++++++++++++++++-------- src/pattern/collide.rs | 6 ++-- src/pattern/fade.rs | 2 +- src/pattern/flashing.rs | 6 ++-- src/pattern/moving_pixel.rs | 2 +- src/pattern/moving_rainbow.rs | 6 ++-- src/pattern/orb.rs | 6 ++-- src/pattern/solid.rs | 2 +- 8 files changed, 66 insertions(+), 30 deletions(-) diff --git a/src/pattern.rs b/src/pattern.rs index 5204254..060b86c 100644 --- a/src/pattern.rs +++ b/src/pattern.rs @@ -22,56 +22,92 @@ pub trait FormRender { fn render(&self) -> String; } pub trait InputRender { - fn render(&self, name: &str) -> String; + fn render(&self, name: &str, multi_index: Option) -> String; } impl InputRender for bool { - fn render(&self, name: &str) -> String { + fn render(&self, name: &str, multi_index: Option) -> String { format!( - r#""#, + r#""#, if *self { " checked" } else { "" }, - name = name, + if let Some(i) = multi_index { + format!( + r#" name="{name}-{i}" rust-form-multi="{name}""#, + name = name, + i = i + ) + } else { + format!(r#" name="{name}""#, name = name) + }, + name = name ) } } impl InputRender for Rgb { - fn render(&self, name: &str) -> String { + fn render(&self, name: &str, multi_index: Option) -> String { format!( - r#""#, + r#""#, self.to_hex_str(), - name = name, + if let Some(i) = multi_index { + format!( + r#" name="{name}-{i}" rust-form-multi="{name}""#, + name = name, + i = i + ) + } else { + format!(r#" name="{name}""#, name = name) + }, + name = name ) } } impl InputRender for Vec { - fn render(&self, name: &str) -> String { + fn render(&self, name: &str, _multi_index: Option) -> String { self.iter() .chain(iter::once(&Rgb::default())) .enumerate() .fold(String::new(), |acc, (i, c)| { - acc + &c.render(&format!("{}-{}", name, i)) + "\n" + acc + &c.render(name, Some(i)) + "\n" }) } } impl InputRender for u8 { - fn render(&self, name: &str) -> String { + fn render(&self, name: &str, multi_index: Option) -> String { format!( - r#""#, + r#""#, self, - name = name, + if let Some(i) = multi_index { + format!( + r#" name="{name}-{i}" rust-form-multi="{name}""#, + name = name, + i = i + ) + } else { + format!(r#" name="{name}""#, name = name) + }, + name = name ) } } impl InputRender for u16 { - fn render(&self, name: &str) -> String { + fn render(&self, name: &str, multi_index: Option) -> String { format!( - r#""#, + r#""#, self, - name = name, + if let Some(i) = multi_index { + format!( + r#" name="{name}-{i}" rust-form-multi="{name}""#, + name = name, + i = i + ) + } else { + format!(r#" name="{name}""#, name = name) + }, + name = name ) } } diff --git a/src/pattern/collide.rs b/src/pattern/collide.rs index 94c3663..f22c7e6 100644 --- a/src/pattern/collide.rs +++ b/src/pattern/collide.rs @@ -25,9 +25,9 @@ impl Default for CollideParams { impl FormRender for CollideParams { fn render(&self) -> String { [ - self.left_color.render("left_color"), - self.right_color.render("right_color"), - self.conjoined_color.render("conjoined_color"), + self.left_color.render("left_color", None), + self.right_color.render("right_color", None), + self.conjoined_color.render("conjoined_color", None), ] .concat() } diff --git a/src/pattern/fade.rs b/src/pattern/fade.rs index 548c4ed..b3580c5 100644 --- a/src/pattern/fade.rs +++ b/src/pattern/fade.rs @@ -19,7 +19,7 @@ impl Default for FadeParams { impl FormRender for FadeParams { fn render(&self) -> String { - self.color.render("color") + self.color.render("color", None) } } diff --git a/src/pattern/flashing.rs b/src/pattern/flashing.rs index 39d5403..51e06d0 100644 --- a/src/pattern/flashing.rs +++ b/src/pattern/flashing.rs @@ -27,9 +27,9 @@ impl Default for FlashingParams { impl FormRender for FlashingParams { fn render(&self) -> String { [ - self.colors.render("colors"), - self.width.render("width"), - self.tick_rate.render("tick_rate"), + self.colors.render("colors", None), + self.width.render("width", None), + self.tick_rate.render("tick_rate", None), ] .concat() } diff --git a/src/pattern/moving_pixel.rs b/src/pattern/moving_pixel.rs index 996d1e9..a92ebc4 100644 --- a/src/pattern/moving_pixel.rs +++ b/src/pattern/moving_pixel.rs @@ -19,7 +19,7 @@ impl Default for MovingPixelParams { impl FormRender for MovingPixelParams { fn render(&self) -> String { - self.color.render("color") + self.color.render("color", None) } } diff --git a/src/pattern/moving_rainbow.rs b/src/pattern/moving_rainbow.rs index 7648dc2..cd93808 100644 --- a/src/pattern/moving_rainbow.rs +++ b/src/pattern/moving_rainbow.rs @@ -25,9 +25,9 @@ impl Default for MovingRainbowParams { impl FormRender for MovingRainbowParams { fn render(&self) -> String { [ - self.width.render("width"), - self.forward.render("forward"), - self.skip.render("skip"), + self.width.render("width", None), + self.forward.render("forward", None), + self.skip.render("skip", None), ] .concat() } diff --git a/src/pattern/orb.rs b/src/pattern/orb.rs index b1ae808..9e09eba 100644 --- a/src/pattern/orb.rs +++ b/src/pattern/orb.rs @@ -25,9 +25,9 @@ impl Default for OrbParams { impl FormRender for OrbParams { fn render(&self) -> String { [ - self.color.render("color"), - self.center_width.render("center_width"), - self.backoff_width.render("backoff_width"), + self.color.render("color", None), + self.center_width.render("center_width", None), + self.backoff_width.render("backoff_width", None), ] .concat() } diff --git a/src/pattern/solid.rs b/src/pattern/solid.rs index f501511..bbb01e5 100644 --- a/src/pattern/solid.rs +++ b/src/pattern/solid.rs @@ -19,7 +19,7 @@ impl Default for SolidParams { impl FormRender for SolidParams { fn render(&self) -> String { - self.color.render("color") + self.color.render("color", None) } }