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 20, 2024
1 parent 5dc93f2 commit 37d8f61
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 23 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
8 changes: 4 additions & 4 deletions src/processing/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
theme::{
Alignment, AuthorPositioning, ElementType, LoadThemeError, Margin, PresentationTheme, PresentationThemeSet,
},
third_party::{ThirdPartyRender, TypstRenderError},
third_party::{ThirdPartyRender, ThirdPartyRenderError},
};
use image::DynamicImage;
use serde::Deserialize;
Expand Down 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 Expand Up @@ -910,8 +910,8 @@ pub enum BuildError {
#[error("error parsing command at line {line}: {error}")]
CommandParse { line: usize, error: CommandParseError },

#[error("typst render failed: {0}")]
TypstRender(#[from] TypstRenderError),
#[error("third party render failed: {0}")]
ThirdPartyRender(#[from] ThirdPartyRenderError),

#[error("language {0:?} does not support execution")]
UnsupportedExecution(CodeLanguage),
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
16 changes: 8 additions & 8 deletions src/third_party.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl ThirdPartyRender {
Self { config, image_registry, root_dir }
}

pub(crate) fn render_typst(&self, input: &str, style: &TypstStyle) -> Result<Image, TypstRenderError> {
pub(crate) fn render_typst(&self, input: &str, style: &TypstStyle) -> Result<Image, ThirdPartyRenderError> {
let workdir = tempdir_in(&self.root_dir)?;
let mut typst_input = Self::generate_page_header(style)?;
typst_input.push_str(input);
Expand All @@ -43,7 +43,7 @@ impl ThirdPartyRender {
self.render_to_image(workdir.path(), &input_path)
}

pub(crate) fn render_latex(&self, input: &str, style: &TypstStyle) -> Result<Image, TypstRenderError> {
pub(crate) fn render_latex(&self, input: &str, style: &TypstStyle) -> Result<Image, ThirdPartyRenderError> {
let output = ThirdPartyTools::pandoc(&["--from", "latex", "--to", "typst"])
.stdin(input.as_bytes().into())
.run_and_capture_stdout()?;
Expand All @@ -52,7 +52,7 @@ impl ThirdPartyRender {
self.render_typst(&input, style)
}

pub(crate) fn render_mermaid(&self, input: &str, style: &MermaidStyle) -> Result<Image, TypstRenderError> {
pub(crate) fn render_mermaid(&self, input: &str, style: &MermaidStyle) -> Result<Image, ThirdPartyRenderError> {
let workdir = tempdir_in(&self.root_dir)?;
let output_path = workdir.path().join("output.png");
let input_path = workdir.path().join("input.mmd");
Expand All @@ -78,7 +78,7 @@ impl ThirdPartyRender {
Ok(image)
}

fn render_to_image(&self, base_path: &Path, path: &Path) -> Result<Image, TypstRenderError> {
fn render_to_image(&self, base_path: &Path, path: &Path) -> Result<Image, ThirdPartyRenderError> {
let output_path = base_path.join("output.png");
ThirdPartyTools::typst(&[
"compile",
Expand All @@ -99,7 +99,7 @@ impl ThirdPartyRender {
Ok(image)
}

fn generate_page_header(style: &TypstStyle) -> Result<String, TypstRenderError> {
fn generate_page_header(style: &TypstStyle) -> Result<String, ThirdPartyRenderError> {
let x_margin = style.horizontal_margin.unwrap_or(DEFAULT_HORIZONTAL_MARGIN);
let y_margin = style.vertical_margin.unwrap_or(DEFAULT_VERTICAL_MARGIN);
let background =
Expand All @@ -114,10 +114,10 @@ impl ThirdPartyRender {
Ok(header)
}

fn as_typst_color(color: &Color) -> Result<String, TypstRenderError> {
fn as_typst_color(color: &Color) -> Result<String, ThirdPartyRenderError> {
match color.as_rgb() {
Some((r, g, b)) => Ok(format!("rgb(\"#{r:02x}{g:02x}{b:02x}\")")),
None => Err(TypstRenderError::UnsupportedColor(color.to_string())),
None => Err(ThirdPartyRenderError::UnsupportedColor(color.to_string())),
}
}
}
Expand All @@ -133,7 +133,7 @@ impl Default for ThirdPartyRender {
}

#[derive(Debug, thiserror::Error)]
pub enum TypstRenderError {
pub enum ThirdPartyRenderError {
#[error(transparent)]
Execution(#[from] ExecutionError),

Expand Down

0 comments on commit 37d8f61

Please sign in to comment.