1use wasm_bindgen::prelude::*;
2use wowlab_loadout::{TraitError, decode_trait_loadout, encode_minimal_loadout};
3use wowlab_parsers::{
4 ParsedSpellDescription, Profile, SimcParseError, parse_simc, parse_spell_desc,
5 render_global_string,
6};
7use wowlab_types::{wasm::to_js, wasm_error};
8
9#[rustfmt::skip]
10pub use wowlab_parsers::{
11 WasmAnalyzeResult, wasm_analyze_spell_desc, wasm_render_spell_desc_with_data,
12 wasm_tokenize_spell_desc,
13};
14
15wasm_error! {
16 #[derive(Debug)]
17 #[non_exhaustive]
18 pub enum WasmParsingError {
19 Parse(String) => "ParseError",
20 Decode(String) => "DecodeError",
21 Serialize(String) => "SerializeError",
22 }
23}
24
25impl From<SimcParseError> for WasmParsingError {
26 fn from(error: SimcParseError) -> Self {
27 Self::Parse(error.to_string())
28 }
29}
30
31impl From<TraitError> for WasmParsingError {
32 fn from(error: TraitError) -> Self {
33 Self::Decode(error.to_string())
34 }
35}
36
37#[wasm_bindgen(js_name = parseSimc)]
38pub fn wasm_parse_simc(input: &str) -> Result<Profile, WasmParsingError> {
39 Ok(parse_simc(input)?)
40}
41
42#[wasm_bindgen(js_name = parseSpellDesc)]
43pub fn wasm_parse_spell_desc(input: &str) -> Result<ParsedSpellDescription, WasmParsingError> {
44 let result = parse_spell_desc(input);
45
46 if result.errors.is_empty() {
47 Ok(result.ast)
48 } else {
49 Err(WasmParsingError::Parse(
50 result
51 .errors
52 .iter()
53 .map(ToString::to_string)
54 .collect::<Vec<_>>()
55 .join(", "),
56 ))
57 }
58}
59
60#[wasm_bindgen(js_name = renderGlobalString)]
61#[must_use]
62pub fn wasm_render_global_string(input: &str) -> String {
63 render_global_string(input)
64}
65
66#[wasm_bindgen(js_name = encodeMinimalLoadout)]
68#[must_use]
69pub fn wasm_encode_minimal_loadout(spec_id: u16) -> String {
70 encode_minimal_loadout(spec_id)
71}
72
73#[wasm_bindgen(js_name = extractSpecIdFromLoadout)]
74pub fn wasm_extract_spec_id_from_loadout(loadout: &str) -> Result<u32, WasmParsingError> {
75 let decoded = decode_trait_loadout(loadout)?;
76
77 Ok(u32::from(decoded.spec_id))
78}
79
80#[wasm_bindgen(js_name = decodeLoadout)]
81pub fn wasm_decode_loadout(loadout: &str) -> Result<JsValue, WasmParsingError> {
82 let decoded = decode_trait_loadout(loadout)?;
83
84 Ok(to_js(&decoded)?)
85}