Skip to content

Commit

Permalink
refactor: merge exported format function
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Aug 28, 2023
1 parent c527f1d commit dc3f525
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_fmt/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IndentStyle>,
Expand Down
19 changes: 7 additions & 12 deletions crates/ruff_fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> {
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<String, String> {
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<Config>) -> Result<String, String> {
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())
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_fmt/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit dc3f525

Please sign in to comment.