Skip to content

Commit

Permalink
refactor example
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed May 11, 2024
1 parent 7d9686c commit a78b9c8
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions markup_fmt/examples/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
use markup_fmt::{detect_language, format_text};
use std::{env, fs};
use markup_fmt::{config::FormatOptions, detect_language, format_text};
use std::{convert::Infallible, env, error::Error, fs, io};

fn main() {
fn main() -> Result<(), Box<dyn Error>> {
let file_path = env::args().nth(1).unwrap();
let language = detect_language(&file_path).unwrap();
let code = fs::read_to_string(file_path).unwrap();
let code = fs::read_to_string(file_path)?;
let options = match fs::read_to_string("markup_fmt.toml") {
Ok(s) => toml::from_str(&s)?,
Err(error) => {
if error.kind() == io::ErrorKind::NotFound {
FormatOptions::default()
} else {
return Err(Box::new(error));
}
}
};

let formatted = format_text(&code, language, &Default::default(), |_, code, _| {
Ok::<_, ()>(code.into())
})
.unwrap();
let formatted = format_text(&code, language, &options, |_, code, _| {
Ok::<_, Infallible>(code.into())
})?;
print!("{formatted}");
Ok(())
}

0 comments on commit a78b9c8

Please sign in to comment.