From 164e2be12b8ccdca36bbc4685100a6297b2537e5 Mon Sep 17 00:00:00 2001 From: v3xro <175618876+v3xro@users.noreply.github.com> Date: Fri, 26 Jul 2024 17:02:33 +0100 Subject: [PATCH 1/2] feat: allow using Formatter::Function from WASM, add value_formatter in Tooltip --- charming/src/element/formatter.rs | 9 +++++++-- charming/src/element/tooltip.rs | 9 +++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/charming/src/element/formatter.rs b/charming/src/element/formatter.rs index 3b55904..49ea0ef 100644 --- a/charming/src/element/formatter.rs +++ b/charming/src/element/formatter.rs @@ -1,11 +1,15 @@ -use serde::Serialize; - use super::RawString; +use serde::Serialize; #[derive(Serialize)] #[serde(untagged)] pub enum Formatter { String(String), + + #[cfg(target_arch = "wasm32")] + Function(#[serde(with = "serde_wasm_bindgen::preserve")] web_sys::js_sys::Function), + + #[cfg(not(target_arch = "wasm32"))] Function(RawString), } @@ -15,6 +19,7 @@ impl From<&str> for Formatter { } } +#[cfg(not(target_arch = "wasm32"))] impl From for Formatter { fn from(s: RawString) -> Self { Formatter::Function(s) diff --git a/charming/src/element/tooltip.rs b/charming/src/element/tooltip.rs index 7102386..a006a1d 100644 --- a/charming/src/element/tooltip.rs +++ b/charming/src/element/tooltip.rs @@ -36,6 +36,9 @@ pub struct Tooltip { #[serde(skip_serializing_if = "Option::is_none")] formatter: Option, + #[serde(skip_serializing_if = "Option::is_none")] + value_formatter: Option, + #[serde(skip_serializing_if = "Option::is_none")] position: Option, @@ -65,6 +68,7 @@ impl Tooltip { trigger_on: None, axis_pointer: None, formatter: None, + value_formatter: None, position: None, padding: None, background_color: None, @@ -93,6 +97,11 @@ impl Tooltip { self } + pub fn value_formatter>(mut self, value_formatter: F) -> Self { + self.value_formatter = Some(value_formatter.into()); + self + } + pub fn position>(mut self, position: S) -> Self { self.position = Some(position.into()); self From a756a0eaae931b6983f1647fa9c74e8576e048d3 Mon Sep 17 00:00:00 2001 From: v3xro <175618876+v3xro@users.noreply.github.com> Date: Fri, 6 Sep 2024 15:22:40 +0100 Subject: [PATCH 2/2] refactor: ensure you can create a function (with or without args) for both wasm and non-wasm targets --- charming/src/element/formatter.rs | 65 ++++++++++++++++++++++++++--- charming/src/element/symbol_size.rs | 15 +++++-- 2 files changed, 71 insertions(+), 9 deletions(-) diff --git a/charming/src/element/formatter.rs b/charming/src/element/formatter.rs index 49ea0ef..c9e6f55 100644 --- a/charming/src/element/formatter.rs +++ b/charming/src/element/formatter.rs @@ -2,15 +2,62 @@ use super::RawString; use serde::Serialize; #[derive(Serialize)] -#[serde(untagged)] -pub enum Formatter { - String(String), +#[serde(transparent)] +pub struct FormatterFunction { + #[cfg(not(target_arch = "wasm32"))] + pub value: RawString, + #[cfg(target_arch = "wasm32")] + pub value: web_sys::js_sys::Function, +} + +impl FormatterFunction { + #[cfg(not(target_arch = "wasm32"))] + pub fn new_no_args(body: &str) -> FormatterFunction { + FormatterFunction { + value: RawString::from(format!("function() {{ {} }}", body)), + } + } #[cfg(target_arch = "wasm32")] - Function(#[serde(with = "serde_wasm_bindgen::preserve")] web_sys::js_sys::Function), + pub fn new_no_args(body: &str) -> FormatterFunction { + FormatterFunction { + value: web_sys::js_sys::Function::new_no_args(body), + } + } #[cfg(not(target_arch = "wasm32"))] - Function(RawString), + pub fn new_with_args(args: &str, body: &str) -> FormatterFunction { + FormatterFunction { + value: RawString::from(format!("function({}) {{ {} }}", args, body)), + } + } + + #[cfg(target_arch = "wasm32")] + pub fn new_with_args(args: &str, body: &str) -> FormatterFunction { + FormatterFunction { + value: web_sys::js_sys::Function::new_with_args(args, body), + } + } +} + +#[cfg(not(target_arch = "wasm32"))] + +impl From for FormatterFunction +where + S: Into, +{ + fn from(s: S) -> Self { + FormatterFunction { + value: RawString::from(s), + } + } +} + +#[derive(Serialize)] +#[serde(untagged)] +pub enum Formatter { + String(String), + Function(FormatterFunction), } impl From<&str> for Formatter { @@ -22,6 +69,12 @@ impl From<&str> for Formatter { #[cfg(not(target_arch = "wasm32"))] impl From for Formatter { fn from(s: RawString) -> Self { - Formatter::Function(s) + Formatter::Function(FormatterFunction { value: s }) + } +} + +impl From for Formatter { + fn from(f: FormatterFunction) -> Self { + Formatter::Function(f) } } diff --git a/charming/src/element/symbol_size.rs b/charming/src/element/symbol_size.rs index 242c72a..27f540b 100644 --- a/charming/src/element/symbol_size.rs +++ b/charming/src/element/symbol_size.rs @@ -1,12 +1,12 @@ use serde::Serialize; -use super::RawString; +use super::{FormatterFunction, RawString}; #[derive(Serialize)] #[serde(untagged)] pub enum SymbolSize { Number(f64), - Function(RawString), + Function(FormatterFunction), } impl From for SymbolSize { @@ -21,8 +21,17 @@ impl From for SymbolSize { } } +#[cfg(not(target_arch = "wasm32"))] impl From<&str> for SymbolSize { fn from(s: &str) -> Self { - SymbolSize::Function(RawString::from(s)) + SymbolSize::Function(FormatterFunction { + value: RawString::from(s), + }) + } +} + +impl From for SymbolSize { + fn from(f: FormatterFunction) -> Self { + SymbolSize::Function(f) } }