Skip to content

Commit

Permalink
Update dependency, better error handling for snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
svenslaggare committed Jan 25, 2025
1 parent c9aa9ad commit b3aa328
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ toml = "0.8"
serde_json = "1"

git2 = "0.20"
comrak = "0.33"
comrak = "0.35"
tempfile = "3"
libc = "0.2"
crossterm = "0.28"
Expand Down
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl App {
InputCommand::ConvertFile { path, destination } => {
let path = self.get_path(path)?;

if helpers::where_is_binary("pandoc").is_none() {
if helpers::where_is_binary(Path::new("pandoc")).is_none() {
return Err(AppError::FailedToConvert(
"pandoc not installed - see https://www.baeldung.com/linux/pdf-markdown-conversion".to_owned()
));
Expand Down
8 changes: 6 additions & 2 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,14 @@ impl TablePrinter {
}
}

pub fn where_is_binary(name: &str) -> Option<PathBuf> {
pub fn where_is_binary(binary: &Path) -> Option<PathBuf> {
if binary.is_absolute() {
return Some(binary.to_owned());
}

let path_variable = std::env::var("PATH").ok()?;
for path_entry in path_variable.split(":") {
let filename = Path::new(&path_entry).join(name);
let filename = Path::new(&path_entry).join(binary);
if filename.exists() {
return Some(filename);
}
Expand Down
36 changes: 36 additions & 0 deletions src/snippets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use fnv::FnvHashMap;
use thiserror::Error;

use crate::config::SnippetFileConfig;
use crate::helpers::where_is_binary;

pub type SnippetResult<T> = Result<T, SnippetError>;

Expand All @@ -17,6 +18,12 @@ pub enum SnippetError {
#[error("No runner found for '{0}'")]
RunnerNotFound(String),

#[error("Executable '{0}' not found")]
ExecutableNotFound(String),

#[error("Compiler '{0}' not found")]
CompilerNotFound(String),

#[error("The configuration type is not valid for this runner")]
InvalidConfigType,

Expand Down Expand Up @@ -145,6 +152,8 @@ impl Default for PythonSnippetRunner {

impl SnippetRunner for PythonSnippetRunner {
fn run(&self, source_code: &str) -> SnippetResult<String> {
assert_executable_exists(&self.config.executable)?;

let mut source_code_file = tempfile::Builder::new()
.suffix(".py")
.tempfile()?;
Expand Down Expand Up @@ -193,6 +202,8 @@ impl Default for BashSnippetRunner {

impl SnippetRunner for BashSnippetRunner {
fn run(&self, source_code: &str) -> SnippetResult<String> {
assert_executable_exists(&self.config.executable)?;

let mut source_code_file = tempfile::Builder::new()
.suffix(".sh")
.tempfile()?;
Expand Down Expand Up @@ -246,6 +257,8 @@ impl Default for CppSnippetRunner {

impl SnippetRunner for CppSnippetRunner {
fn run(&self, source_code: &str) -> SnippetResult<String> {
assert_compiler_exists(&self.config.compiler_executable)?;

let mut source_code_file = tempfile::Builder::new()
.suffix(".cpp")
.tempfile()?;
Expand Down Expand Up @@ -318,6 +331,8 @@ impl Default for RustSnippetRunner {

impl SnippetRunner for RustSnippetRunner {
fn run(&self, source_code: &str) -> SnippetResult<String> {
assert_compiler_exists(&self.config.compiler_executable)?;

let mut source_code_file = tempfile::Builder::new()
.suffix(".rs")
.tempfile()?;
Expand Down Expand Up @@ -388,6 +403,8 @@ impl Default for JavaScriptSnippetRunner {

impl SnippetRunner for JavaScriptSnippetRunner {
fn run(&self, source_code: &str) -> SnippetResult<String> {
assert_executable_exists(&self.config.executable)?;

let mut source_code_file = tempfile::Builder::new()
.suffix(".js")
.tempfile()?;
Expand Down Expand Up @@ -438,6 +455,9 @@ impl Default for TypeScriptSnippetRunner {

impl SnippetRunner for TypeScriptSnippetRunner {
fn run(&self, source_code: &str) -> SnippetResult<String> {
assert_compiler_exists(&self.config.compiler_executable)?;
assert_executable_exists(&self.config.node_executable)?;

let mut source_code_file = tempfile::Builder::new()
.suffix(".ts")
.tempfile()?;
Expand Down Expand Up @@ -515,6 +535,22 @@ impl Drop for DeleteFileGuard {
}
}

fn assert_compiler_exists(executable: &Path) -> SnippetResult<()> {
if where_is_binary(&executable).is_none() {
return Err(SnippetError::CompilerNotFound(executable.to_str().unwrap().to_owned()));
}

Ok(())
}

fn assert_executable_exists(executable: &Path) -> SnippetResult<()> {
if where_is_binary(&executable).is_none() {
return Err(SnippetError::ExecutableNotFound(executable.to_str().unwrap().to_owned()));
}

Ok(())
}

#[test]
fn test_manager_success1() {
let manager = SnippetRunnerManger::default();
Expand Down

0 comments on commit b3aa328

Please sign in to comment.