Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow using Formatter::Function from WASM #67

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 61 additions & 3 deletions charming/src/element/formatter.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,63 @@
use super::RawString;
use serde::Serialize;

use super::RawString;
#[derive(Serialize)]
#[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")]
pub fn new_no_args(body: &str) -> FormatterFunction {
FormatterFunction {
value: web_sys::js_sys::Function::new_no_args(body),
}
}

#[cfg(not(target_arch = "wasm32"))]
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<S> From<S> for FormatterFunction
where
S: Into<String>,
{
fn from(s: S) -> Self {
FormatterFunction {
value: RawString::from(s),
}
}
}

#[derive(Serialize)]
#[serde(untagged)]
pub enum Formatter {
String(String),
Function(RawString),
Function(FormatterFunction),
}

impl From<&str> for Formatter {
Expand All @@ -15,8 +66,15 @@ impl From<&str> for Formatter {
}
}

#[cfg(not(target_arch = "wasm32"))]
impl From<RawString> for Formatter {
fn from(s: RawString) -> Self {
Formatter::Function(s)
Formatter::Function(FormatterFunction { value: s })
}
}

impl From<FormatterFunction> for Formatter {
fn from(f: FormatterFunction) -> Self {
Formatter::Function(f)
}
}
15 changes: 12 additions & 3 deletions charming/src/element/symbol_size.rs
Original file line number Diff line number Diff line change
@@ -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<i64> for SymbolSize {
Expand All @@ -21,8 +21,17 @@ impl From<f64> 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<FormatterFunction> for SymbolSize {
fn from(f: FormatterFunction) -> Self {
SymbolSize::Function(f)
}
}
9 changes: 9 additions & 0 deletions charming/src/element/tooltip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ pub struct Tooltip {
#[serde(skip_serializing_if = "Option::is_none")]
formatter: Option<Formatter>,

#[serde(skip_serializing_if = "Option::is_none")]
value_formatter: Option<Formatter>,

#[serde(skip_serializing_if = "Option::is_none")]
position: Option<String>,

Expand Down Expand Up @@ -65,6 +68,7 @@ impl Tooltip {
trigger_on: None,
axis_pointer: None,
formatter: None,
value_formatter: None,
position: None,
padding: None,
background_color: None,
Expand Down Expand Up @@ -93,6 +97,11 @@ impl Tooltip {
self
}

pub fn value_formatter<F: Into<Formatter>>(mut self, value_formatter: F) -> Self {
self.value_formatter = Some(value_formatter.into());
self
}

pub fn position<S: Into<String>>(mut self, position: S) -> Self {
self.position = Some(position.into());
self
Expand Down