Skip to content

Commit

Permalink
chore: add better error message if unknown language is found
Browse files Browse the repository at this point in the history
  • Loading branch information
mfontanini committed Jun 14, 2024
1 parent 5dc93f2 commit f9949a8
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 12 deletions.
7 changes: 1 addition & 6 deletions src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/markdown/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions src/markdown/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ pub enum CodeLanguage {
Terraform,
TypeScript,
Typst,
Unknown,
Unknown(String),
Xml,
Yaml,
Vue,
Expand Down Expand Up @@ -308,7 +308,7 @@ impl FromStr for CodeLanguage {
"yaml" => Yaml,
"vue" => Vue,
"zig" => Zig,
_ => Unknown,
other => Unknown(other.to_string()),
};
Ok(language)
}
Expand Down
2 changes: 1 addition & 1 deletion src/processing/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ impl<'a> PresentationBuilder<'a> {
fn highlight_lines(&self, code: &Code) -> (Vec<HighlightedLine>, Rc<RefCell<HighlightContext>>) {
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);
Expand Down
2 changes: 1 addition & 1 deletion src/processing/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/render/highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f9949a8

Please sign in to comment.