From f97f746ab9d65adcbcf0e0b4ad8ded083040edbd Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Wed, 20 Dec 2023 14:55:33 -0500 Subject: [PATCH] minor refactor --- rust-whisper-lib/src/lib.rs | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/rust-whisper-lib/src/lib.rs b/rust-whisper-lib/src/lib.rs index 84bbebb..5b4d99f 100644 --- a/rust-whisper-lib/src/lib.rs +++ b/rust-whisper-lib/src/lib.rs @@ -199,21 +199,14 @@ struct Engine { } fn new_engine(model_path: Option, model_buffer: Option>, threads: i32) -> Result { - if model_path.is_some() { - let model_path = model_path.unwrap(); - return match WhisperContext::new(&model_path.clone()) { - Ok(ctx) => Ok(Engine{ctx: ctx, threads: threads}), - Err(msg) => Err(format!("failed to load {}: {}", model_path, msg)), - }; + let whisper_context_result = match model_path { + Some(model_path) => WhisperContext::new(&model_path), + None => WhisperContext::new_from_buffer(&model_buffer.unwrap()), + }; + match whisper_context_result { + Ok(ctx) => Ok(Engine{ctx: ctx, threads: threads}), + Err(msg) => Err(format!("failed to load model: {}", msg)), } - if model_buffer.is_some() { - let model_buffer = model_buffer.unwrap(); - return match WhisperContext::new_from_buffer(&model_buffer) { - Ok(ctx) => Ok(Engine{ctx: ctx, threads: threads}), - Err(msg) => Err(format!("failed to load buffer: {}", msg)), - }; - } - Err("neither model path nor buffer provided".to_string()) } impl Engine {