From f9949a8f025b108a33227fcd2ccd6219c7ac3116 Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Fri, 14 Jun 2024 06:26:43 -0700 Subject: [PATCH] chore: add better error message if unknown language is found --- src/execute.rs | 7 +------ src/markdown/code.rs | 2 +- src/markdown/elements.rs | 4 ++-- src/processing/builder.rs | 2 +- src/processing/code.rs | 2 +- src/render/highlighting.rs | 2 +- 6 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/execute.rs b/src/execute.rs index ed3f2570..a9ad213e 100644 --- a/src/execute.rs +++ b/src/execute.rs @@ -36,7 +36,7 @@ impl CodeExecutor { return Err(LoadExecutorsError::InvalidExecutor(path, "non .sh extension")); } let language: CodeLanguage = match name.parse() { - Ok(CodeLanguage::Unknown) => { + Ok(CodeLanguage::Unknown(_)) => { return Err(LoadExecutorsError::InvalidExecutor(path, "unknown language")); } Ok(language) => language, @@ -295,11 +295,6 @@ echo 'hello world' assert_eq!(state.output, expected_lines); } - #[test] - fn validate_builtin_executors() { - assert!(EXECUTORS.get(&CodeLanguage::Unknown).is_none(), "unknown language in executors"); - } - #[test] fn custom_executor() { let dir = tempdir().unwrap(); diff --git a/src/markdown/code.rs b/src/markdown/code.rs index fd2a6564..e2d1adb6 100644 --- a/src/markdown/code.rs +++ b/src/markdown/code.rs @@ -180,7 +180,7 @@ mod test { #[test] fn unknown_language() { - assert_eq!(parse_language("potato"), CodeLanguage::Unknown); + assert_eq!(parse_language("potato"), CodeLanguage::Unknown("potato".to_string())); } #[test] diff --git a/src/markdown/elements.rs b/src/markdown/elements.rs index 216d4b1f..0b8f3638 100644 --- a/src/markdown/elements.rs +++ b/src/markdown/elements.rs @@ -237,7 +237,7 @@ pub enum CodeLanguage { Terraform, TypeScript, Typst, - Unknown, + Unknown(String), Xml, Yaml, Vue, @@ -308,7 +308,7 @@ impl FromStr for CodeLanguage { "yaml" => Yaml, "vue" => Vue, "zig" => Zig, - _ => Unknown, + other => Unknown(other.to_string()), }; Ok(language) } diff --git a/src/processing/builder.rs b/src/processing/builder.rs index 94ee4607..9aca80c3 100644 --- a/src/processing/builder.rs +++ b/src/processing/builder.rs @@ -706,7 +706,7 @@ impl<'a> PresentationBuilder<'a> { fn highlight_lines(&self, code: &Code) -> (Vec, Rc>) { let lines = CodePreparer::new(&self.theme).prepare(code); let block_length = lines.iter().map(|line| line.width()).max().unwrap_or(0); - let mut empty_highlighter = self.highlighter.language_highlighter(&CodeLanguage::Unknown); + let mut empty_highlighter = self.highlighter.language_highlighter(&CodeLanguage::Unknown(String::new())); let mut code_highlighter = self.highlighter.language_highlighter(&code.language); let padding_style = { let mut highlighter = self.highlighter.language_highlighter(&CodeLanguage::Rust); diff --git a/src/processing/code.rs b/src/processing/code.rs index 14882aa5..c39f43ea 100644 --- a/src/processing/code.rs +++ b/src/processing/code.rs @@ -189,7 +189,7 @@ mod test { let input_lines = "hi\n".repeat(total_lines); let code = Code { contents: input_lines, - language: CodeLanguage::Unknown, + language: CodeLanguage::Unknown("".to_string()), attributes: CodeAttributes { line_numbers: true, ..Default::default() }, }; let lines = CodePreparer { theme: &Default::default() }.prepare(&code); diff --git a/src/render/highlighting.rs b/src/render/highlighting.rs index 9b4f92d8..0f74e045 100644 --- a/src/render/highlighting.rs +++ b/src/render/highlighting.rs @@ -157,7 +157,7 @@ impl CodeHighlighter { TypeScript => "ts", Typst => "txt", // default to plain text so we get the same look&feel - Unknown => "txt", + Unknown(_) => "txt", Vue => "vue", Xml => "xml", Yaml => "yaml",