mirror of
https://github.com/RGBCube/alejandra
synced 2025-07-31 04:27:45 +00:00
feat: property handle errors
This commit is contained in:
parent
ef80695a28
commit
a7c7644475
4 changed files with 29 additions and 11 deletions
|
@ -13,7 +13,6 @@ pub fn main() -> Result<(), JsValue> {
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn format(before: String, path: String) -> String {
|
pub fn format(before: String, path: String) -> String {
|
||||||
let config = alejandra::config::Config::new();
|
let config = alejandra::config::Config::new();
|
||||||
let after = alejandra::format::string(&config, path, before);
|
|
||||||
|
|
||||||
return after;
|
alejandra::format::string_or_passthrough(&config, path, before)
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,9 @@ pub fn stdin(config: crate::config::Config) -> std::io::Result<()> {
|
||||||
eprintln!("Formatting stdin, run with --help to see all options.");
|
eprintln!("Formatting stdin, run with --help to see all options.");
|
||||||
let mut stdin = String::new();
|
let mut stdin = String::new();
|
||||||
std::io::stdin().read_to_string(&mut stdin).unwrap();
|
std::io::stdin().read_to_string(&mut stdin).unwrap();
|
||||||
print!("{}", crate::format::string(&config, "stdin".to_string(), stdin));
|
|
||||||
|
let stdout = crate::format::string(&config, "stdin".to_string(), stdin)?;
|
||||||
|
print!("{}", stdout);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,19 +2,33 @@ pub fn string(
|
||||||
config: &crate::config::Config,
|
config: &crate::config::Config,
|
||||||
path: String,
|
path: String,
|
||||||
string: String,
|
string: String,
|
||||||
) -> String {
|
) -> std::io::Result<String> {
|
||||||
let tokens = rnix::tokenizer::Tokenizer::new(&string);
|
let tokens = rnix::tokenizer::Tokenizer::new(&string);
|
||||||
let ast = rnix::parser::parse(tokens);
|
let ast = rnix::parser::parse(tokens);
|
||||||
|
|
||||||
for error in ast.errors() {
|
let errors = ast.errors();
|
||||||
eprintln!("Error: {}, at: {}", error, path);
|
if !errors.is_empty() {
|
||||||
return ast.node().to_string();
|
return Err(std::io::Error::new(
|
||||||
|
std::io::ErrorKind::InvalidInput,
|
||||||
|
errors[0].to_string(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let green_node =
|
let green_node =
|
||||||
crate::builder::build(config, ast.node().into(), false, path).unwrap();
|
crate::builder::build(config, ast.node().into(), false, path).unwrap();
|
||||||
|
|
||||||
green_node.to_string()
|
Ok(green_node.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn string_or_passthrough(
|
||||||
|
config: &crate::config::Config,
|
||||||
|
path: String,
|
||||||
|
before: String,
|
||||||
|
) -> String {
|
||||||
|
match crate::format::string(&config, path, before.clone()) {
|
||||||
|
Ok(after) => after,
|
||||||
|
Err(_) => before,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn file(
|
pub fn file(
|
||||||
|
@ -27,7 +41,7 @@ pub fn file(
|
||||||
let input_clone = input.clone();
|
let input_clone = input.clone();
|
||||||
let input_bytes = input_clone.as_bytes();
|
let input_bytes = input_clone.as_bytes();
|
||||||
|
|
||||||
let output = crate::format::string(config, path.clone(), input);
|
let output = crate::format::string(config, path.clone(), input)?;
|
||||||
let output_bytes = output.as_bytes();
|
let output_bytes = output.as_bytes();
|
||||||
|
|
||||||
let changed = input_bytes != output_bytes;
|
let changed = input_bytes != output_bytes;
|
||||||
|
|
|
@ -16,8 +16,11 @@ fn cases() {
|
||||||
let path_in = format!("tests/cases/{}/in", case);
|
let path_in = format!("tests/cases/{}/in", case);
|
||||||
let path_out = format!("tests/cases/{}/out", case);
|
let path_out = format!("tests/cases/{}/out", case);
|
||||||
let content_in = std::fs::read_to_string(path_in.clone()).unwrap();
|
let content_in = std::fs::read_to_string(path_in.clone()).unwrap();
|
||||||
let content_got =
|
let content_got = alejandra::format::string_or_passthrough(
|
||||||
alejandra::format::string(&config, path_in, content_in.clone());
|
&config,
|
||||||
|
path_in,
|
||||||
|
content_in.clone(),
|
||||||
|
);
|
||||||
|
|
||||||
if should_update {
|
if should_update {
|
||||||
std::fs::File::create(&path_out)
|
std::fs::File::create(&path_out)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue