Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmostellar committed Feb 21, 2024
1 parent a923966 commit 2389fb9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
16 changes: 8 additions & 8 deletions wasm/src/lib.rs
@@ -1,3 +1,4 @@
use serde_json::Value;
use std::error::Error;
use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -52,7 +53,7 @@ pub fn format_document(input: &str, js_settings: JsValue, js_locales: JsValue) -
}
};

let locales = match read_js_value(js_locales) {
let locales: Value = match read_js_value(js_locales) {
Ok(locales) => locales,
Err(e) => {
let error_message = e.to_string();
Expand All @@ -65,7 +66,7 @@ pub fn format_document(input: &str, js_settings: JsValue, js_locales: JsValue) -
}

// Return value to the TypeScript side or throw an error.
match parse_input(input, settings, &locales) {
match parse_input(input, &settings, &locales) {
Ok(sections) => sections,
Err(e) => {
let error_message = e.to_string();
Expand All @@ -78,22 +79,21 @@ fn read_settings<T: serde::de::DeserializeOwned>(input: JsValue) -> Result<T, Bo
Ok(serde_wasm_bindgen::from_value(input)?)
}

use serde_json::Value;
fn read_js_value(js_value: JsValue) -> Result<Value, Box<dyn Error>> {
if let Some(a) = &js_value.as_string() {
Ok(serde_json::from_str(a)?)
if let Some(js_value) = &js_value.as_string() {
Ok(serde_json::from_str(js_value)?)
} else {
Err("Failed to read locale file.".into())
}
}

fn parse_input(
input: &str,
settings: PluginSettings,
settings: &PluginSettings,
locales: &Value,
) -> Result<String, Box<dyn Error>> {
let sections = tools::parsing::get_sections(input, &settings, locales)?;
let output = tools::formatting::get_formatted_string(sections, &settings, locales)?;
let sections = tools::parsing::get_sections(input, settings, locales)?;
let output = tools::formatting::get_formatted_string(sections, settings, locales)?;

Ok(output)
}
12 changes: 6 additions & 6 deletions wasm/src/utils.rs
Expand Up @@ -23,16 +23,16 @@ pub enum LocaleCategory {
pub fn get_locale_string(locales: &Value, category: LocaleCategory, key: &str) -> String {
match category {
LocaleCategory::Parsing => {
if let Some(msg) = locales["parsing"][key].as_str() {
if !msg.is_empty() {
return String::from(msg);
if let Some(message) = locales["parsing"][key].as_str() {
if !message.is_empty() {
return String::from(message);
}
}
}
LocaleCategory::Formatting => {
if let Some(msg) = locales["formatting"][key].as_str() {
if !msg.is_empty() {
return String::from(msg);
if let Some(message) = locales["formatting"][key].as_str() {
if !message.is_empty() {
return String::from(message);
}
}
}
Expand Down

0 comments on commit 2389fb9

Please sign in to comment.