Skip to content

Commit

Permalink
refactor: ensure you can create a function (with or without args) for…
Browse files Browse the repository at this point in the history
… both wasm and non-wasm targets
  • Loading branch information
v3xro committed Sep 6, 2024
1 parent 3fa603f commit 9c8d993
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 9 deletions.
65 changes: 59 additions & 6 deletions charming/src/element/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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(FormatterFunction),
}

impl From<&str> for Formatter {
Expand All @@ -22,6 +69,12 @@ 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)
}
}

0 comments on commit 9c8d993

Please sign in to comment.