fix(cheddar): Fix errors if filename does not have an extension

This commit is contained in:
Vincent Ambo 2019-12-22 15:21:23 +00:00
parent f1c018af18
commit a36e9f817d

View file

@ -142,7 +142,7 @@ fn format_markdown() {
.expect("Markdown rendering failed"); .expect("Markdown rendering failed");
} }
fn format_code(extension: String) { fn format_code(extension: Option<&str>) {
let stdin = io::stdin(); let stdin = io::stdin();
let mut stdin = stdin.lock(); let mut stdin = stdin.lock();
let mut linebuf = String::new(); let mut linebuf = String::new();
@ -153,7 +153,8 @@ fn format_code(extension: String) {
// Set up the highlighter // Set up the highlighter
let theme = &THEMES.themes["InspiredGitHub"]; 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)) .or_else(|| SYNTAXES.find_syntax_by_first_line(&linebuf))
.unwrap_or_else(|| SYNTAXES.find_syntax_plain_text()); .unwrap_or_else(|| SYNTAXES.find_syntax_plain_text());
@ -189,12 +190,9 @@ fn format_code(extension: String) {
} }
fn main() { fn main() {
let extension = args_extension() let extension = args_extension();
.expect("cheddar should be invoked with a filename!"); match extension.as_ref().map(String::as_str) {
Some("md") => format_markdown(),
if extension == "md" { extension => format_code(extension),
format_markdown();
} else {
format_code(extension);
} }
} }