Replace some `unwrap` calls with `ok_or(Error::OperationFailed)`.

pull/26/head
Nolan Darilek 4 months ago
parent 10ac1021ee
commit 507d0b5418
  1. 18
      src/backends/appkit.rs
  2. 7
      src/backends/av_foundation.rs
  3. 8
      src/lib.rs

@ -12,12 +12,12 @@ use crate::{Backend, BackendId, Error, Features, UtteranceId, Voice};
pub(crate) struct AppKit(*mut Object, *mut Object);
impl AppKit {
pub(crate) fn new() -> Self {
pub(crate) fn new() -> Result<Self, Error> {
info!("Initializing AppKit backend");
unsafe {
let obj: *mut Object = msg_send![class!(NSSpeechSynthesizer), new];
let mut decl =
ClassDecl::new("MyNSSpeechSynthesizerDelegate", class!(NSObject)).unwrap();
let mut decl = ClassDecl::new("MyNSSpeechSynthesizerDelegate", class!(NSObject))
.ok_or(Error::OperationFailed)?;
decl.add_ivar::<id>("synth");
decl.add_ivar::<id>("strings");
@ -81,11 +81,17 @@ impl AppKit {
let delegate_class = decl.register();
let delegate_obj: *mut Object = msg_send![delegate_class, new];
delegate_obj.as_mut().unwrap().set_ivar("synth", obj);
delegate_obj
.as_mut()
.ok_or(Error::OperationFailed)?
.set_ivar("synth", obj);
let strings: id = msg_send![class!(NSMutableArray), new];
delegate_obj.as_mut().unwrap().set_ivar("strings", strings);
delegate_obj
.as_mut()
.ok_or(Error::OperationFailed)?
.set_ivar("strings", strings);
let _: Object = msg_send![obj, setDelegate: delegate_obj];
AppKit(obj, delegate_obj)
Ok(AppKit(obj, delegate_obj))
}
}
}

@ -29,9 +29,10 @@ lazy_static! {
}
impl AvFoundation {
pub(crate) fn new() -> Self {
pub(crate) fn new() -> Result<Self, Error> {
info!("Initializing AVFoundation backend");
let mut decl = ClassDecl::new("MyNSSpeechSynthesizerDelegate", class!(NSObject)).unwrap();
let mut decl = ClassDecl::new("MyNSSpeechSynthesizerDelegate", class!(NSObject))
.ok_or(Error::OperationFailed)?;
decl.add_ivar::<u64>("backend_id");
extern "C" fn speech_synthesizer_did_start_speech_utterance(
@ -149,7 +150,7 @@ impl AvFoundation {
}
};
*backend_id += 1;
rv
Ok(rv)
}
}

@ -293,12 +293,12 @@ impl Tts {
Ok(Tts(Arc::new(RwLock::new(Box::new(tts)))))
}
#[cfg(target_os = "macos")]
Backends::AppKit => Ok(Tts(Arc::new(RwLock::new(
Box::new(backends::AppKit::new()),
)))),
Backends::AppKit => Ok(Tts(Arc::new(RwLock::new(Box::new(
backends::AppKit::new()?
))))),
#[cfg(any(target_os = "macos", target_os = "ios"))]
Backends::AvFoundation => Ok(Tts(Arc::new(RwLock::new(Box::new(
backends::AvFoundation::new(),
backends::AvFoundation::new()?,
))))),
#[cfg(target_os = "android")]
Backends::Android => {

Loading…
Cancel
Save