Skip to content

Commit

Permalink
Implement safe unwrapping with default fallbacks.
Browse files Browse the repository at this point in the history
  • Loading branch information
clementwanjau committed Dec 19, 2024
1 parent ee78f45 commit 504540c
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/yew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,28 @@ impl Component for ApexChartComponent {
let mut labels_data = None;
let series_data = match props.r#type {
ChartType::Pie | ChartType::Donut | ChartType::RadialBar => {
let chart_serie = props.series.first().unwrap();
match chart_serie.data {
SeriesData::Radial(ref data) => {
let data_values = data.iter().map(|(_, y)| *y).collect::<Vec<_>>();
labels_data = Some(data.iter().map(|(x, _)| x.clone()).collect::<Vec<_>>());
serde_json::to_value(data_values).unwrap()
match props.series.first()
{
Some(chart_serie) => {
match chart_serie.data {
SeriesData::Radial(ref data) => {
let data_values = data.iter().map(|(_, y)| *y).collect::<Vec<_>>();
labels_data = Some(data.iter().map(|(x, _)| x.clone()).collect::<Vec<_>>());
serde_json::to_value(data_values).unwrap_or_default()
},
_=> {
serde_json::to_value(&props.series).unwrap_or_default()
}
}
},
_=> {
serde_json::to_value(&props.series).unwrap()
Value::Array(vec![])
}
}

},
_=> {
serde_json::to_value(&props.series).unwrap()
serde_json::to_value(&props.series).unwrap_or_default()
}
};
let options = if props.options.is_empty() {
Expand All @@ -145,15 +153,15 @@ impl Component for ApexChartComponent {
}
)
} else {
let mut options = serde_json::from_str::<serde_json::Value>(&props.options).unwrap();
let mut options = serde_json::from_str::<Value>(&props.options).unwrap();
options["chart"]["type"] = Value::String(props.r#type.to_string());
options["chart"]["width"] = Value::String(props.width.clone());
options["chart"]["height"] = Value::String(props.height.clone());
options["series"] = series_data;
if let Some(labels) = labels_data {
options["labels"] = Value::Array(labels.iter().map(|label| Value::String(label.clone())).collect());
}
serde_json::to_string(&options).unwrap()
serde_json::to_string(&options).unwrap_or_default()
};
Self {
chart: ApexChart::new(&JsValue::from_str(&options)),
Expand Down

0 comments on commit 504540c

Please sign in to comment.