From dc3f5254968084846b41718415029e249e8b13dd Mon Sep 17 00:00:00 2001 From: magic-akari Date: Mon, 28 Aug 2023 23:20:00 +0800 Subject: [PATCH] refactor: merge exported `format` function --- README.md | 4 ++-- crates/ruff_fmt/src/config.rs | 2 +- crates/ruff_fmt/src/lib.rs | 19 +++++++------------ crates/ruff_fmt/src/test.rs | 2 +- 4 files changed, 11 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index ad4c950..c4709f0 100644 --- a/README.md +++ b/README.md @@ -36,10 +36,10 @@ console.log(formatted); with custom options: ```javascript -import init, { format_with_config } from "@wasm-fmt/ruff_fmt"; +import init, { format } from "@wasm-fmt/ruff_fmt"; // ... -const formatted = format_with_config(input, { +const formatted = format(input, { indent_style: 4, line_width: 88, quote_style: "double", diff --git a/crates/ruff_fmt/src/config.rs b/crates/ruff_fmt/src/config.rs index 24cbaa5..740a278 100644 --- a/crates/ruff_fmt/src/config.rs +++ b/crates/ruff_fmt/src/config.rs @@ -17,7 +17,7 @@ export interface Config { magic_trailing_comma?: "respect" | "ignore"; }"#; -#[derive(Deserialize)] +#[derive(Deserialize, Default)] #[serde(rename_all = "snake_case")] pub(crate) struct Config { indent_style: Option, diff --git a/crates/ruff_fmt/src/lib.rs b/crates/ruff_fmt/src/lib.rs index a19a08b..2c15a2b 100644 --- a/crates/ruff_fmt/src/lib.rs +++ b/crates/ruff_fmt/src/lib.rs @@ -6,18 +6,13 @@ use config::Config as InnerConfig; use ruff_python_formatter::format_module; use wasm_bindgen::prelude::*; -#[wasm_bindgen] -pub fn format(input: &str) -> Result { - format_module(input, Default::default()) - .map(|result| result.into_code()) - .map_err(|err| err.to_string()) -} - -#[wasm_bindgen] -pub fn format_with_config(input: &str, config: &Config) -> Result { - let config: &JsValue = config.as_ref(); - let config: InnerConfig = - serde_wasm_bindgen::from_value(config.clone()).map_err(|op| op.to_string())?; +#[wasm_bindgen(skip_typescript)] +pub fn format(input: &str, config: Option) -> Result { + let config: InnerConfig = if let Some(config) = config { + serde_wasm_bindgen::from_value(config.clone()).map_err(|op| op.to_string())? + } else { + Default::default() + }; format_module(input, config.into()) .map(|result| result.into_code()) diff --git a/crates/ruff_fmt/src/test.rs b/crates/ruff_fmt/src/test.rs index 8463012..1c53a40 100644 --- a/crates/ruff_fmt/src/test.rs +++ b/crates/ruff_fmt/src/test.rs @@ -16,7 +16,7 @@ mod tests { let mut expect = String::new(); File::open(extect_path).and_then(|mut file| file.read_to_string(&mut expect)).unwrap(); - let actual = format(&actual).unwrap(); + let actual = format(&actual, None).unwrap(); assert_eq!(actual, expect); }