Skip to content

Commit

Permalink
chore(results): improve error messages for invalid JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Bear-03 committed Oct 27, 2024
1 parent 3043a07 commit 3b17efe
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
4 changes: 2 additions & 2 deletions crates/vosk/src/recognition/batch.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
errors::AcceptWaveformError,
results::{result_from_json_cstr, Word},
results::{result_from_json_c_str, Word},
};
use crate::models::BatchModel;
use vosk_sys::*;
Expand Down Expand Up @@ -58,7 +58,7 @@ impl BatchRecognizer {

/// Gets the front of the result queue
pub fn front_result(&mut self) -> Word {
unsafe { result_from_json_cstr(vosk_batch_recognizer_front_result(self.0.as_ptr())) }
unsafe { result_from_json_c_str(vosk_batch_recognizer_front_result(self.0.as_ptr())) }
}

/// Removes the front of the result queue
Expand Down
5 changes: 3 additions & 2 deletions crates/vosk/src/recognition/results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,10 @@ pub struct PartialResult<'a> {
}

/// Generic function to retrieve a given type of result from the recognizer.
pub(super) unsafe fn result_from_json_cstr<'de, T: Deserialize<'de>>(ptr: *const c_char) -> T {
pub(super) unsafe fn result_from_json_c_str<'de, T: Deserialize<'de>>(ptr: *const c_char) -> T {
// UNWRAP: Panics in here will never be the caller's fault, but rather some
// edge case that was not thought of and should be reported, so it does not
// make sense to return a Result.
serde_json::from_str(CStr::from_ptr(ptr).to_str().unwrap()).unwrap()
let string = CStr::from_ptr(ptr).to_str().unwrap();
serde_json::from_str(string).unwrap_or_else(|_| panic!("Invalid JSON: {string:?}"))
}
8 changes: 4 additions & 4 deletions crates/vosk/src/recognition/sequential.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{
result_from_json_cstr, AcceptWaveformError, CompleteResult, DecodingState, PartialResult,
result_from_json_c_str, AcceptWaveformError, CompleteResult, DecodingState, PartialResult,
};
use crate::models::{Model, SpeakerModel};

Expand Down Expand Up @@ -203,7 +203,7 @@ impl Recognizer {
/// [`CompleteResult::Single`]: crate::CompleteResult::Single
#[must_use]
pub fn result(&mut self) -> CompleteResult {
unsafe { result_from_json_cstr(vosk_recognizer_result(self.0.as_ptr())) }
unsafe { result_from_json_c_str(vosk_recognizer_result(self.0.as_ptr())) }
}

/// Returns partial speech recognition, which is not yet finalized and may change after
Expand All @@ -214,7 +214,7 @@ impl Recognizer {
/// [`set_partial_words`]: Self::set_partial_words
#[must_use]
pub fn partial_result(&mut self) -> PartialResult {
unsafe { result_from_json_cstr(vosk_recognizer_partial_result(self.0.as_ptr())) }
unsafe { result_from_json_c_str(vosk_recognizer_partial_result(self.0.as_ptr())) }
}

/// Returns speech recognition result. Like [`result`] but it does not
Expand All @@ -223,7 +223,7 @@ impl Recognizer {
/// [`result`]: Self::result
#[must_use]
pub fn final_result(&mut self) -> CompleteResult {
unsafe { result_from_json_cstr(vosk_recognizer_final_result(self.0.as_ptr())) }
unsafe { result_from_json_c_str(vosk_recognizer_final_result(self.0.as_ptr())) }
}

/// Resets current results and data so the recognition can continue from scratch
Expand Down

0 comments on commit 3b17efe

Please sign in to comment.