Add "styled" CLI option and debug logging for disassembler

This commit is contained in:
Jeff 2024-09-14 14:31:40 -04:00
parent 8534f18c9a
commit 9418cd5b70
2 changed files with 32 additions and 12 deletions

View File

@ -211,7 +211,9 @@ impl Display for Chunk {
write!(
f,
"{}",
self.disassembler("Chunk Display").styled().disassemble()
self.disassembler("Chunk Display")
.styled(true)
.disassemble()
)
}
}
@ -221,7 +223,9 @@ impl Debug for Chunk {
write!(
f,
"{}",
self.disassembler("Chunk Debug Display").disassemble()
self.disassembler("Chunk Debug Display")
.styled(false)
.disassemble()
)
}
}
@ -309,9 +313,6 @@ impl<'a> ChunkDisassembler<'a> {
};
let mut disassembled = String::with_capacity(self.predict_length());
println!("capactity: {}", disassembled.capacity());
let name_line = style(center(self.name));
disassembled.push_str(&name_line);
@ -376,7 +377,20 @@ impl<'a> ChunkDisassembler<'a> {
disassembled.push_str(&center(&local_display));
}
println!("length: {}", disassembled.len());
let expected_length = self.predict_length();
let actual_length = disassembled.len();
if !self.styled && expected_length != actual_length {
log::debug!(
"Chunk disassembly was not optimized correctly, expected string length {expected_length}, got {actual_length}",
);
}
if self.styled && expected_length > actual_length {
log::debug!(
"Chunk disassembly was not optimized correctly, expected string length to be at least{expected_length}, got {actual_length}",
);
}
disassembled
}
@ -387,8 +401,8 @@ impl<'a> ChunkDisassembler<'a> {
self
}
pub fn styled(&mut self) -> &mut Self {
self.styled = true;
pub fn styled(&mut self, styled: bool) -> &mut Self {
self.styled = styled;
self
}

View File

@ -13,6 +13,9 @@ struct Cli {
#[arg(short, long)]
parse: bool,
#[arg(short, long)]
styled: bool,
path: Option<String>,
}
@ -43,7 +46,7 @@ fn main() {
if let Some(command) = &args.command {
if args.parse {
parse_and_display_errors(command);
parse_and_display_errors(command, args.styled);
} else {
run_and_display_errors(command);
}
@ -51,18 +54,21 @@ fn main() {
let source = read_to_string(path).expect("Failed to read file");
if args.parse {
parse_and_display_errors(&source);
parse_and_display_errors(&source, args.styled);
} else {
run_and_display_errors(&source);
}
}
}
fn parse_and_display_errors(source: &str) {
fn parse_and_display_errors(source: &str, pretty_print: bool) {
match parse(source) {
Ok(chunk) => println!(
"{}",
chunk.disassembler("Dust CLI Input").styled().disassemble()
chunk
.disassembler("Dust CLI Input")
.styled(pretty_print)
.disassemble()
),
Err(error) => {
eprintln!("{}", error.report());