diff options
author | Vincent Ambo <tazjin@google.com> | 2019-12-22T15·21+0000 |
---|---|---|
committer | Vincent Ambo <tazjin@google.com> | 2019-12-22T15·21+0000 |
commit | a36e9f817da280ffb3188b33df8b6e84e2127a46 (patch) | |
tree | b84df320333bcebb577806f3f98815d6d244deba /tools/cheddar/src | |
parent | f1c018af187d2c21226c4ddb4d6c7057bded6546 (diff) |
fix(cheddar): Fix errors if filename does not have an extension r/291
Diffstat (limited to 'tools/cheddar/src')
-rw-r--r-- | tools/cheddar/src/main.rs | 16 |
1 files 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), } } |