Skip to content

Commit

Permalink
Add the ability to format more than one file at a time.
Browse files Browse the repository at this point in the history
  • Loading branch information
parno committed Nov 16, 2023
1 parent 7345329 commit 4e8b5a4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 42 deletions.
32 changes: 13 additions & 19 deletions examples/wip.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
verus! {

#[allow(unused_macros)]
macro_rules! no_usize_overflows {
($e:expr,) => {
true
};
($($e:expr),*) => {
no_usize_overflows!(@@internal 0, $($e),*)
};
(@@internal $total:expr,) => {
true
};
(@@internal $total:expr, $a:expr) => {
usize::MAX - $total >= $a
};
(@@internal $total:expr, $a:expr, $($rest:expr),*) => {
usize::MAX - $total >= $a
&&
no_usize_overflows!(@@internal ($total + $a), $($rest),*)
};
pub fn clone_vec_u8() {
let i = 0;
while i < v.len()
invariant
i <= v.len(),
i == out.len(),
forall|j| #![auto] 0 <= j < i ==> out@[j] == v@[j],
ensures
i > 0,
decreases 72,
{
i = i + 1;
}
}

} // verus!
54 changes: 31 additions & 23 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,59 @@ struct Args {
/// Run in 'check' mode. Exits with 0 only if the file is formatted correctly.
#[arg(long = "check")]
check: bool,
/// Input file to be formatted
file: PathBuf,
/// Input files to be formatted
files: Vec<PathBuf>,
/// Print debugging output (can be repeated for more detail)
#[arg(short = 'd', long = "debug", action = clap::ArgAction::Count)]
debug_level: u8,
}

// TODO: Call rustfmt on the code too (maybe include an option to skip it)
fn main() -> anyhow::Result<()> {
let args = Args::parse();

tracing_subscriber::fmt()
.with_timer(tracing_subscriber::fmt::time::uptime())
.with_level(true)
.with_target(false)
.with_max_level(match args.debug_level {
0 => tracing::Level::WARN,
1 => tracing::Level::INFO,
2 => tracing::Level::DEBUG,
_ => tracing::Level::TRACE,
})
.init();

let unparsed_file = fs::read_to_string(&args.file)?;
fn format_file(file: &PathBuf, check: bool) -> anyhow::Result<()> {
let unparsed_file = fs::read_to_string(file)?;
let formatted_output = parse_and_format(&unparsed_file)?;

if args.check {
if check {
if unparsed_file == formatted_output {
info!("✨Perfectly formatted✨");
return Ok(());
} else {
info!("Found some differences");
info!("Found some differences in {}", file.display());
error!("Input found not to be well formatted");
let diff = similar::udiff::unified_diff(
similar::Algorithm::Patience,
&unparsed_file,
&formatted_output,
3,
Some(("original", "formatted")),
);
);
println!("{diff}");
return Err(anyhow!("invalid formatting"));
}
} else {
fs::write(args.file, formatted_output)?;
fs::write(file, formatted_output)?;
Ok(())
}
}

// TODO: Call rustfmt on the code too (maybe include an option to skip it)
fn main() -> anyhow::Result<()> {
let args = Args::parse();
if args.files.len() == 0 {
return Err(anyhow!("No files specified"));
}

tracing_subscriber::fmt()
.with_timer(tracing_subscriber::fmt::time::uptime())
.with_level(true)
.with_target(false)
.with_max_level(match args.debug_level {
0 => tracing::Level::WARN,
1 => tracing::Level::INFO,
2 => tracing::Level::DEBUG,
_ => tracing::Level::TRACE,
})
.init();
// TODO: This errors out when we first find an ill-formatted file.
// Consider going through all of the files, regardless.
args.files.iter().try_fold((), |_, file| format_file(&file, args.check))
}

0 comments on commit 4e8b5a4

Please sign in to comment.