From a36e9f817da280ffb3188b33df8b6e84e2127a46 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 22 Dec 2019 15:21:23 +0000 Subject: fix(cheddar): Fix errors if filename does not have an extension --- tools/cheddar/src/main.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/tools/cheddar/src/main.rs b/tools/cheddar/src/main.rs index eeb8bb6480ff..d0115d391c9d 100644 --- a/tools/cheddar/src/main.rs +++ b/tools/cheddar/src/main.rs @@ -142,7 +142,7 @@ fn format_markdown() { .expect("Markdown rendering failed"); } -fn format_code(extension: String) { +fn format_code(extension: Option<&str>) { let stdin = io::stdin(); let mut stdin = stdin.lock(); let mut linebuf = String::new(); @@ -153,7 +153,8 @@ fn format_code(extension: String) { // Set up the highlighter let theme = &THEMES.themes["InspiredGitHub"]; - let syntax = SYNTAXES.find_syntax_by_extension(&extension) + let syntax = extension + .and_then(|e| SYNTAXES.find_syntax_by_extension(e)) .or_else(|| SYNTAXES.find_syntax_by_first_line(&linebuf)) .unwrap_or_else(|| SYNTAXES.find_syntax_plain_text()); @@ -189,12 +190,9 @@ fn format_code(extension: String) { } fn main() { - let extension = args_extension() - .expect("cheddar should be invoked with a filename!"); - - if extension == "md" { - format_markdown(); - } else { - format_code(extension); + let extension = args_extension(); + match extension.as_ref().map(String::as_str) { + Some("md") => format_markdown(), + extension => format_code(extension), } } -- cgit 1.4.1