Skip to content

Commit

Permalink
Return the cdynlib crate type.
Browse files Browse the repository at this point in the history
  • Loading branch information
clementwanjau committed May 4, 2024
1 parent be7b3f0 commit d14fd51
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ license-file = "LICENSE"
keywords = ["apexcharts", "wasm", "rust", "web"]
readme = "README.md"

[lib]
crate-type = ["cdylib"]

[dependencies]
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
Expand Down
23 changes: 19 additions & 4 deletions src/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,26 @@ extern "C" {
/// ## Usage
///
/// ```rust
/// use apexcharts_rs::{ApexChart, ChartOptions};
///
/// let options = ChartOptions::from_file("path/to/options.json");
/// use apexcharts_rs::prelude::{ApexChart, ChartOptions};
///
/// let options_str = r#"
/// {
/// "chart": {
/// "type": "line"
/// },
/// "series": [
/// {
/// "name": "Series 1",
/// "data": [30, 40, 35, 50, 49, 125]
/// }
/// ]
/// }"#;
/// let filename = "options.json";
/// std::fs::write(filename, options_str).unwrap();
/// let options = ChartOptions::from_file(filename);
/// let chart = ApexChart::new(&options.into());
/// chart.render("chart-id");
/// // chart.render("chart-id");
/// std::fs::remove_file(filename).unwrap();
/// ```
pub type ApexChart;

Expand Down
33 changes: 24 additions & 9 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ use wasm_bindgen_futures::js_sys;
///
/// ## Usage
/// ```rust
/// let options = apexcharts_rs::ChartOptions::from_file("path/to/options.json");
///
/// // or
///
/// let options_str = r#"
/// {
Expand All @@ -25,7 +22,7 @@ use wasm_bindgen_futures::js_sys;
/// }
/// ]
/// }"#;
/// let options = apexcharts_rs::ChartOptions::from_string(String::from(options_str));
/// let options = apexcharts_rs::prelude::ChartOptions::from_string(String::from(options_str));
/// ```
#[derive(Clone, Debug)]
pub struct ChartOptions {
Expand All @@ -47,8 +44,8 @@ impl ChartOptions {
/// }
/// ]
/// }"#;
/// let options = apexcharts_rs::ChartOptions::from_string(options_str);
/// let chart = apexcharts_rs::ApexChart::new(&options.into());
/// let options = apexcharts_rs::prelude::ChartOptions::from_string(String::from(options_str));
/// let chart = apexcharts_rs::prelude::ApexChart::new(&options.into());
/// ```
pub fn from_string(options: String) -> Self {
Self {
Expand All @@ -57,8 +54,23 @@ impl ChartOptions {
}
/// Create a new instance of ChartOptions from a json file.
/// ```rust
/// let options = apexcharts_rs::ChartOptions::from_file("path/to/options.json");
/// let chart = apexcharts_rs::ApexChart::new(&options.into());
/// let options_str = r#"
/// {
/// "chart": {
/// "type": "line"
/// },
/// "series": [
/// {
/// "name": "Series 1",
/// "data": [30, 40, 35, 50, 49, 125]
/// }
/// ]
/// }"#;
/// let filename = "options.json";
/// std::fs::write(filename, options_str).unwrap();
/// let options = apexcharts_rs::prelude::ChartOptions::from_file(filename);
/// let chart = apexcharts_rs::prelude::ApexChart::new(&options.into());
/// std::fs::remove_file(filename).unwrap();
/// ```
pub fn from_file(file_path: &str) -> Self {
// Read the file and return the content as a string
Expand All @@ -81,7 +93,7 @@ impl ChartOptions {

impl From<ChartOptions> for JsValue {
fn from(options: ChartOptions) -> JsValue {
JsValue::from_str(&options.options)
JsValue::from_str(options.options.as_str())
}
}

Expand Down Expand Up @@ -159,6 +171,9 @@ pub enum SeriesData {
///
/// ## Usage
/// ```rust
/// use apexcharts_rs::prelude::{ChartSeries, SeriesData, ChartOptions, ApexChart};
/// use wasm_bindgen::prelude::*;
///
/// let series = vec![ChartSeries {
/// name: "Series 1".to_string(),
/// data: SeriesData::Single(vec![10.0, 20.0, 30.0]),
Expand Down

0 comments on commit d14fd51

Please sign in to comment.