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

This commit is contained in:
Nolan Darilek 2022-06-14 13:09:50 -05:00
parent 10ac1021ee
commit 507d0b5418
3 changed files with 20 additions and 13 deletions

View File

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

View File

@ -29,9 +29,10 @@ lazy_static! {
} }
impl AvFoundation { impl AvFoundation {
pub(crate) fn new() -> Self { pub(crate) fn new() -> Result<Self, Error> {
info!("Initializing AVFoundation backend"); 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"); decl.add_ivar::<u64>("backend_id");
extern "C" fn speech_synthesizer_did_start_speech_utterance( extern "C" fn speech_synthesizer_did_start_speech_utterance(
@ -149,7 +150,7 @@ impl AvFoundation {
} }
}; };
*backend_id += 1; *backend_id += 1;
rv Ok(rv)
} }
} }

View File

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