1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-07-30 12:07:46 +00:00

feat: property handle errors

This commit is contained in:
Kevin Amado 2022-02-21 12:47:10 -05:00
parent ef80695a28
commit a7c7644475
4 changed files with 29 additions and 11 deletions

View file

@ -13,7 +13,6 @@ pub fn main() -> Result<(), JsValue> {
#[wasm_bindgen]
pub fn format(before: String, path: String) -> String {
let config = alejandra::config::Config::new();
let after = alejandra::format::string(&config, path, before);
return after;
alejandra::format::string_or_passthrough(&config, path, before)
}

View file

@ -33,7 +33,9 @@ pub fn stdin(config: crate::config::Config) -> std::io::Result<()> {
eprintln!("Formatting stdin, run with --help to see all options.");
let mut stdin = String::new();
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(())
}

View file

@ -2,19 +2,33 @@ pub fn string(
config: &crate::config::Config,
path: String,
string: String,
) -> String {
) -> std::io::Result<String> {
let tokens = rnix::tokenizer::Tokenizer::new(&string);
let ast = rnix::parser::parse(tokens);
for error in ast.errors() {
eprintln!("Error: {}, at: {}", error, path);
return ast.node().to_string();
let errors = ast.errors();
if !errors.is_empty() {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
errors[0].to_string(),
));
}
let green_node =
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(
@ -27,7 +41,7 @@ pub fn file(
let input_clone = input.clone();
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 changed = input_bytes != output_bytes;

View file

@ -16,8 +16,11 @@ fn cases() {
let path_in = format!("tests/cases/{}/in", case);
let path_out = format!("tests/cases/{}/out", case);
let content_in = std::fs::read_to_string(path_in.clone()).unwrap();
let content_got =
alejandra::format::string(&config, path_in, content_in.clone());
let content_got = alejandra::format::string_or_passthrough(
&config,
path_in,
content_in.clone(),
);
if should_update {
std::fs::File::create(&path_out)