From 6097dfaf312ba3a16f05f4b4039721625d1c224b Mon Sep 17 00:00:00 2001 From: Aprabhat19 Date: Thu, 30 Nov 2023 18:26:27 +0530 Subject: [PATCH] fix wasm function to return the description and category --- crates/euclid_wasm/src/lib.rs | 27 ++++++++++++++++++++------- crates/euclid_wasm/src/types.rs | 3 ++- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/crates/euclid_wasm/src/lib.rs b/crates/euclid_wasm/src/lib.rs index cab82f8ce411..78c7677fe75c 100644 --- a/crates/euclid_wasm/src/lib.rs +++ b/crates/euclid_wasm/src/lib.rs @@ -254,12 +254,25 @@ pub fn add_two(n1: i64, n2: i64) -> i64 { } #[wasm_bindgen(js_name = getDescriptionCategory)] -pub fn get_description_category(key: &str) -> JsResult { - let key = dir::DirKeyKind::from_str(key).map_err(|_| "Invalid key received".to_string())?; +pub fn get_description_category() -> JsResult { + let keys = dir::DirKeyKind::VARIANTS + .iter() + .copied() + .filter(|s| s != &"Connector") + .collect::>(); + let mut category: HashMap, Vec>> = HashMap::new(); + for key in keys { + let dir_key = + dir::DirKeyKind::from_str(key).map_err(|_| "Invalid key received".to_string())?; + let details = types::Details { + description: dir_key.get_detailed_message(), + kind: dir_key.clone(), + }; + category + .entry(dir_key.get_str("Category")) + .and_modify(|val| val.push(details.clone())) + .or_insert(vec![details]); + } - let result = types::Details { - description: key.get_detailed_message(), - category: key.get_str("Category"), - }; - Ok(serde_wasm_bindgen::to_value(&result)?) + Ok(serde_wasm_bindgen::to_value(&category)?) } diff --git a/crates/euclid_wasm/src/types.rs b/crates/euclid_wasm/src/types.rs index ea40449971bc..6353d9009c36 100644 --- a/crates/euclid_wasm/src/types.rs +++ b/crates/euclid_wasm/src/types.rs @@ -1,7 +1,8 @@ +use euclid::frontend::dir::DirKeyKind; use serde::Serialize; #[derive(Serialize, Clone)] pub struct Details<'a> { pub description: Option<&'a str>, - pub category: Option<&'a str>, + pub kind: DirKeyKind, }