From 7d3edccddac04a1b4e3e2ac92d0401828b0577ed Mon Sep 17 00:00:00 2001 From: Malloc Voidstar <1284317+AlyoshaVasilieva@users.noreply.github.com> Date: Mon, 6 Dec 2021 10:57:04 -0800 Subject: [PATCH 1/2] Convert all bool-returns to Results Additionally: * Make open2 fallible too * Use a Result the entire time in open and open2, instead of going from Option to Result * Specify c_int instead of i32 since apparently the size "may differ on some esoteric systems"; I suspect it won't compile on whatever those are but might as well improve the situation * Avoid a maybe-possible panic in get_voice_type. Probably can't happen but I'm not 100% certain, so I made it fallible * Add a missing null check to get_language --- speech-dispatcher/examples/hello_world.rs | 12 +- speech-dispatcher/src/lib.rs | 348 ++++++++++++++-------- 2 files changed, 228 insertions(+), 132 deletions(-) diff --git a/speech-dispatcher/examples/hello_world.rs b/speech-dispatcher/examples/hello_world.rs index 00ba239..9a3eaea 100644 --- a/speech-dispatcher/examples/hello_world.rs +++ b/speech-dispatcher/examples/hello_world.rs @@ -22,18 +22,18 @@ fn main() -> Result<(), Box> { connection.client_id() ), ); - connection.set_voice_rate(100); + connection.set_voice_rate(100)?; connection.say(Priority::Important, "This is faster."); - connection.set_voice_rate(0); - connection.set_spelling(true); + connection.set_voice_rate(0)?; + connection.set_spelling(true)?; connection.say(Priority::Important, "This is spelled."); - connection.set_spelling(false); - connection.set_punctuation(Punctuation::All); + connection.set_spelling(false)?; + connection.set_punctuation(Punctuation::All)?; connection.say( Priority::Important, "This statement, unlike others, has punctuation that is spoken!", ); - connection.set_punctuation(Punctuation::None); + connection.set_punctuation(Punctuation::None)?; let mut _input = String::new(); io::stdin().read_line(&mut _input).unwrap(); Ok(()) diff --git a/speech-dispatcher/src/lib.rs b/speech-dispatcher/src/lib.rs index 440abb5..1d29c11 100644 --- a/speech-dispatcher/src/lib.rs +++ b/speech-dispatcher/src/lib.rs @@ -5,7 +5,7 @@ use std::{ ffi::{CStr, CString}, fmt, marker::Send, - os::raw::c_char, + os::raw::{c_char, c_int}, sync::Mutex, }; @@ -82,8 +82,12 @@ pub enum CapitalLetters { Icon = SPDCapitalLetters::SPD_CAP_ICON, } -fn i32_to_bool(v: i32) -> bool { - v == 1 +/// Converts a `0` to a success and everything else to an error. +fn c_int_to_result(r: c_int) -> Result<(), SpeechDispatcherError> { + match r { + 0 => Ok(()), + _ => Err(SpeechDispatcherError::OperationFailed), + } } #[derive(Default)] @@ -151,7 +155,11 @@ unsafe extern "C" fn cb_im(msg_id: u64, client_id: u64, state: u32, index_mark: #[derive(Debug)] pub enum SpeechDispatcherError { + /// speech-dispatcher failed to initialize. Ensure speech-dispatcher is actually working on + /// your system; for example, does the command `spd-say hello` work? InitializationError, + /// The operation failed + OperationFailed, } impl std::error::Error for SpeechDispatcherError {} @@ -161,6 +169,7 @@ impl fmt::Display for SpeechDispatcherError { use SpeechDispatcherError::*; match self { InitializationError => write!(f, "Failed to initialize"), + OperationFailed => write!(f, "Operation failed"), } } } @@ -183,18 +192,14 @@ impl Connection { mode as u32, ); if c.is_null() { - None + Err(SpeechDispatcherError::InitializationError) } else { - Some(Self::setup_connection(c)) + Ok(Self::setup_connection(c)) } }; - if let Some(connection) = connection { - let mut c = Self(connection, 0); - c.setup(); - Ok(c) - } else { - Err(SpeechDispatcherError::InitializationError) - } + let mut c = Self(connection?, 0); + c.setup()?; + Ok(c) } pub unsafe fn open2>( @@ -204,7 +209,7 @@ impl Connection { mode: Mode, address: *mut Address, autospawn: bool, - ) -> Self { + ) -> Result { let auto_spawn = if autospawn { 1 } else { 0 }; let error_result = vec![CString::new("").unwrap().into_raw()].as_mut_ptr(); let clientname = CString::new(client_name.into()).unwrap(); @@ -220,11 +225,15 @@ impl Connection { auto_spawn, error_result, ); - Self::setup_connection(c) + if c.is_null() { + Err(SpeechDispatcherError::InitializationError) + } else { + Ok(Self::setup_connection(c)) + } }; - let mut c = Self(connection, 0); - c.setup(); - c + let mut c = Self(connection?, 0); + c.setup()?; + Ok(c) } unsafe fn setup_connection(c: *mut SPDConnection) -> *mut SPDConnection { @@ -237,7 +246,7 @@ impl Connection { c } - fn setup(&mut self) { + fn setup(&mut self) -> Result<(), SpeechDispatcherError> { let client_id = self.send_data("HISTORY GET CLIENT_ID\r\n", true); if let Some(client_id) = client_id { let client_id: Vec<&str> = client_id.split("\r\n").collect(); @@ -252,7 +261,9 @@ impl Connection { } } callbacks.lock().unwrap().insert(self.1, Default::default()); - self.set_notification_on(Notification::All); + self.set_notification_on(Notification::All) + .map_err(|_| SpeechDispatcherError::InitializationError)?; + Ok(()) } pub fn close(&self) { @@ -281,107 +292,123 @@ impl Connection { } } - pub fn stop(&self) -> bool { + pub fn stop(&self) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_stop(self.0) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn stop_all(&self) -> bool { + pub fn stop_all(&self) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_stop_all(self.0) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn stop_uid(&self, target_uid: i32) -> bool { + pub fn stop_uid(&self, target_uid: i32) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_stop_uid(self.0, target_uid) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn cancel(&self) -> bool { + pub fn cancel(&self) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_cancel(self.0) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn cancel_all(&self) -> bool { + pub fn cancel_all(&self) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_cancel_all(self.0) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn cancel_uid(&self, target_uid: i32) -> bool { + pub fn cancel_uid(&self, target_uid: i32) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_cancel_uid(self.0, target_uid) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn pause(&self) -> bool { + pub fn pause(&self) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_pause(self.0) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn pause_all(&self) -> bool { + pub fn pause_all(&self) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_pause_all(self.0) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn pause_uid(&self, target_uid: i32) -> bool { + pub fn pause_uid(&self, target_uid: i32) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_pause_uid(self.0, target_uid) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn resume(&self) -> bool { + pub fn resume(&self) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_resume(self.0) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn resume_all(&self) -> bool { + pub fn resume_all(&self) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_resume_all(self.0) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn resume_uid(&self, target_uid: i32) -> bool { + pub fn resume_uid(&self, target_uid: i32) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_resume_uid(self.0, target_uid) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn key>(&self, priority: Priority, key_name: S) -> bool { + pub fn key>( + &self, + priority: Priority, + key_name: S, + ) -> Result<(), SpeechDispatcherError> { let param = CString::new(key_name.into()).unwrap(); let v = unsafe { spd_key(self.0, priority as u32, param.as_ptr()) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn char>(&self, priority: Priority, char: S) -> bool { + pub fn char>( + &self, + priority: Priority, + char: S, + ) -> Result<(), SpeechDispatcherError> { let param = CString::new(char.into()).unwrap(); let v = unsafe { spd_char(self.0, priority as u32, param.as_ptr()) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn wchar(&self, priority: Priority, wchar: i32) -> bool { + pub fn wchar(&self, priority: Priority, wchar: i32) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_wchar(self.0, priority as u32, wchar as wchar_t) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn sound_icon>(&self, priority: Priority, icon_name: S) -> bool { + pub fn sound_icon>( + &self, + priority: Priority, + icon_name: S, + ) -> Result<(), SpeechDispatcherError> { let param = CString::new(icon_name.into()).unwrap(); let v = unsafe { spd_char(self.0, priority as u32, param.as_ptr()) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_voice_type(&self, voice_type: VoiceType) -> bool { + pub fn set_voice_type(&self, voice_type: VoiceType) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_set_voice_type(self.0, voice_type as u32) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_voice_type_all(&self, voice_type: VoiceType) -> bool { + pub fn set_voice_type_all(&self, voice_type: VoiceType) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_set_voice_type_all(self.0, voice_type as u32) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_voice_type_uid(&self, voice_type: VoiceType, target_uid: u32) -> bool { + pub fn set_voice_type_uid( + &self, + voice_type: VoiceType, + target_uid: u32, + ) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_set_voice_type_uid(self.0, voice_type as u32, target_uid) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn get_voice_type(&self) -> VoiceType { + pub fn get_voice_type(&self) -> Result { let v = unsafe { spd_get_voice_type(self.0) }; - match v { + Ok(match v { SPDVoiceType::SPD_MALE1 => VoiceType::Male1, SPDVoiceType::SPD_MALE2 => VoiceType::Male2, SPDVoiceType::SPD_MALE3 => VoiceType::Male3, @@ -390,213 +417,282 @@ impl Connection { SPDVoiceType::SPD_FEMALE3 => VoiceType::Female3, SPDVoiceType::SPD_CHILD_MALE => VoiceType::ChildMale, SPDVoiceType::SPD_CHILD_FEMALE => VoiceType::ChildFemale, - _ => panic!("Invalid voice type"), - } + _ => return Err(SpeechDispatcherError::OperationFailed), // can this happen? + }) } - pub fn set_synthesis_voice>(&self, voice_name: S) -> bool { + pub fn set_synthesis_voice>( + &self, + voice_name: S, + ) -> Result<(), SpeechDispatcherError> { let param = CString::new(voice_name.into()).unwrap(); let v = unsafe { spd_set_synthesis_voice(self.0, param.as_ptr()) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_synthesis_voice_all>(&self, voice_name: S) -> bool { + pub fn set_synthesis_voice_all>( + &self, + voice_name: S, + ) -> Result<(), SpeechDispatcherError> { let param = CString::new(voice_name.into()).unwrap(); let v = unsafe { spd_set_synthesis_voice_all(self.0, param.as_ptr()) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_synthesis_voice_uid>(&self, voice_name: S, target_uid: u32) -> bool { + pub fn set_synthesis_voice_uid>( + &self, + voice_name: S, + target_uid: u32, + ) -> Result<(), SpeechDispatcherError> { let param = CString::new(voice_name.into()).unwrap(); let v = unsafe { spd_set_synthesis_voice_uid(self.0, param.as_ptr(), target_uid) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_data_mode(&self, mode: DataMode) -> bool { + pub fn set_data_mode(&self, mode: DataMode) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_set_data_mode(self.0, mode as u32) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_notification_on(&self, notification: Notification) -> bool { + pub fn set_notification_on( + &self, + notification: Notification, + ) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_set_notification_on(self.0, notification as u32) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_notification_off(&self, notification: Notification) -> bool { + pub fn set_notification_off( + &self, + notification: Notification, + ) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_set_notification_off(self.0, notification as u32) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_notification>(&self, notification: Notification, state: S) -> bool { + pub fn set_notification>( + &self, + notification: Notification, + state: S, + ) -> Result<(), SpeechDispatcherError> { let param = CString::new(state.into()).unwrap(); let v = unsafe { spd_set_notification(self.0, notification as u32, param.as_ptr()) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_voice_rate(&self, rate: i32) -> bool { + pub fn set_voice_rate(&self, rate: i32) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_set_voice_rate(self.0, rate) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_voice_rate_all(&self, rate: i32) -> bool { + pub fn set_voice_rate_all(&self, rate: i32) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_set_voice_rate_all(self.0, rate) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_voice_rate_uid(&self, rate: i32, target_uid: u32) -> bool { + pub fn set_voice_rate_uid( + &self, + rate: i32, + target_uid: u32, + ) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_set_voice_rate_uid(self.0, rate, target_uid) }; - i32_to_bool(v) + c_int_to_result(v) } pub fn get_voice_rate(&self) -> i32 { unsafe { spd_get_voice_rate(self.0) } } - pub fn set_voice_pitch(&self, pitch: i32) -> bool { + pub fn set_voice_pitch(&self, pitch: i32) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_set_voice_pitch(self.0, pitch) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_voice_pitch_all(&self, pitch: i32) -> bool { + pub fn set_voice_pitch_all(&self, pitch: i32) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_set_voice_pitch_all(self.0, pitch) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_voice_pitch_uid(&self, pitch: i32, target_uid: u32) -> bool { + pub fn set_voice_pitch_uid( + &self, + pitch: i32, + target_uid: u32, + ) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_set_voice_pitch_uid(self.0, pitch, target_uid) }; - i32_to_bool(v) + c_int_to_result(v) } pub fn get_voice_pitch(&self) -> i32 { unsafe { spd_get_voice_pitch(self.0) } } - pub fn set_volume(&self, volume: i32) -> bool { + pub fn set_volume(&self, volume: i32) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_set_volume(self.0, volume) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_volume_all(&self, volume: i32) -> bool { + pub fn set_volume_all(&self, volume: i32) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_set_volume_all(self.0, volume) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_volume_uid(&self, volume: i32, target_uid: u32) -> bool { + pub fn set_volume_uid( + &self, + volume: i32, + target_uid: u32, + ) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_set_volume_uid(self.0, volume, target_uid) }; - i32_to_bool(v) + c_int_to_result(v) } pub fn get_volume(&self) -> i32 { unsafe { spd_get_volume(self.0) } } - pub fn set_punctuation(&self, punctuation: Punctuation) -> bool { + pub fn set_punctuation(&self, punctuation: Punctuation) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_set_punctuation(self.0, punctuation as u32) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_punctuation_all(&self, punctuation: Punctuation) -> bool { + pub fn set_punctuation_all( + &self, + punctuation: Punctuation, + ) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_set_punctuation_all(self.0, punctuation as u32) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_punctuation_uid(&self, punctuation: Punctuation, target_uid: u32) -> bool { + pub fn set_punctuation_uid( + &self, + punctuation: Punctuation, + target_uid: u32, + ) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_set_punctuation_uid(self.0, punctuation as u32, target_uid) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_capital_letters(&self, capital_letters: CapitalLetters) -> bool { + pub fn set_capital_letters( + &self, + capital_letters: CapitalLetters, + ) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_set_capital_letters(self.0, capital_letters as u32) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_capital_letters_all(&self, capital_letters: CapitalLetters) -> bool { + pub fn set_capital_letters_all( + &self, + capital_letters: CapitalLetters, + ) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_set_capital_letters_all(self.0, capital_letters as u32) }; - i32_to_bool(v) + c_int_to_result(v) } pub fn set_capital_letters_uid( &self, capital_letters: CapitalLetters, target_uid: u32, - ) -> bool { + ) -> Result<(), SpeechDispatcherError> { let v = unsafe { spd_set_capital_letters_uid(self.0, capital_letters as u32, target_uid) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_spelling(&self, spelling: bool) -> bool { + pub fn set_spelling(&self, spelling: bool) -> Result<(), SpeechDispatcherError> { let s = if spelling { SPDSpelling::SPD_SPELL_ON } else { SPDSpelling::SPD_SPELL_OFF }; let v = unsafe { spd_set_spelling(self.0, s) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_spelling_all(&self, spelling: bool) -> bool { + pub fn set_spelling_all(&self, spelling: bool) -> Result<(), SpeechDispatcherError> { let s = if spelling { SPDSpelling::SPD_SPELL_ON } else { SPDSpelling::SPD_SPELL_OFF }; let v = unsafe { spd_set_spelling_all(self.0, s) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_spelling_uid(&self, spelling: bool, target_uid: u32) -> bool { + pub fn set_spelling_uid( + &self, + spelling: bool, + target_uid: u32, + ) -> Result<(), SpeechDispatcherError> { let s = if spelling { SPDSpelling::SPD_SPELL_ON } else { SPDSpelling::SPD_SPELL_OFF }; let v = unsafe { spd_set_spelling_uid(self.0, s, target_uid) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_language>(&self, language: S) -> bool { + pub fn set_language>(&self, language: S) -> Result<(), SpeechDispatcherError> { let param = CString::new(language.into()).unwrap(); let v = unsafe { spd_set_language(self.0, param.as_ptr()) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_language_all>(&self, language: S) -> bool { + pub fn set_language_all>( + &self, + language: S, + ) -> Result<(), SpeechDispatcherError> { let param = CString::new(language.into()).unwrap(); let v = unsafe { spd_set_language_all(self.0, param.as_ptr()) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_language_uid>(&self, language: S, target_uid: u32) -> bool { + pub fn set_language_uid>( + &self, + language: S, + target_uid: u32, + ) -> Result<(), SpeechDispatcherError> { let param = CString::new(language.into()).unwrap(); let v = unsafe { spd_set_language_uid(self.0, param.as_ptr(), target_uid) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn get_language(&self) -> &str { - let v = unsafe { CStr::from_ptr(spd_get_language(self.0)) }; - v.to_str().unwrap() + pub fn get_language(&self) -> Result<&str, SpeechDispatcherError> { + let language = unsafe { spd_get_language(self.0) }; + if language.is_null() { + Err(SpeechDispatcherError::OperationFailed) + } else { + let language = unsafe { CStr::from_ptr(language) }; + language + .to_str() + .map_err(|_| SpeechDispatcherError::OperationFailed) + } } - pub fn set_output_module>(&self, output_module: S) -> bool { + pub fn set_output_module>( + &self, + output_module: S, + ) -> Result<(), SpeechDispatcherError> { let param = CString::new(output_module.into()).unwrap(); let v = unsafe { spd_set_output_module(self.0, param.as_ptr()) }; - i32_to_bool(v) + c_int_to_result(v) } - pub fn set_output_module_all>(&self, output_module: S) -> bool { + pub fn set_output_module_all>( + &self, + output_module: S, + ) -> Result<(), SpeechDispatcherError> { let param = CString::new(output_module.into()).unwrap(); let v = unsafe { spd_set_output_module_all(self.0, param.as_ptr()) }; - i32_to_bool(v) + c_int_to_result(v) } pub fn set_output_module_uid>( &self, output_module: S, target_uid: u32, - ) -> bool { + ) -> Result<(), SpeechDispatcherError> { let param = CString::new(output_module.into()).unwrap(); let v = unsafe { spd_set_output_module_uid(self.0, param.as_ptr(), target_uid) }; - i32_to_bool(v) + c_int_to_result(v) } pub fn send_data>(&self, data: S, wait_for_reply: bool) -> Option { From 83b1ac5a76723296376273356db29c72553308a4 Mon Sep 17 00:00:00 2001 From: Malloc Voidstar <1284317+AlyoshaVasilieva@users.noreply.github.com> Date: Mon, 6 Dec 2021 10:58:29 -0800 Subject: [PATCH 2/2] Change SpeechDispatcherError to Error, format Also lowercase Display strings to be in line with https://rust-lang.github.io/api-guidelines/interoperability.html#examples-of-error-messages --- speech-dispatcher/src/lib.rs | 198 ++++++++++++----------------------- 1 file changed, 69 insertions(+), 129 deletions(-) diff --git a/speech-dispatcher/src/lib.rs b/speech-dispatcher/src/lib.rs index 1d29c11..3f34f85 100644 --- a/speech-dispatcher/src/lib.rs +++ b/speech-dispatcher/src/lib.rs @@ -83,10 +83,10 @@ pub enum CapitalLetters { } /// Converts a `0` to a success and everything else to an error. -fn c_int_to_result(r: c_int) -> Result<(), SpeechDispatcherError> { +fn c_int_to_result(r: c_int) -> Result<(), Error> { match r { 0 => Ok(()), - _ => Err(SpeechDispatcherError::OperationFailed), + _ => Err(Error::OperationFailed), } } @@ -154,7 +154,7 @@ unsafe extern "C" fn cb_im(msg_id: u64, client_id: u64, state: u32, index_mark: } #[derive(Debug)] -pub enum SpeechDispatcherError { +pub enum Error { /// speech-dispatcher failed to initialize. Ensure speech-dispatcher is actually working on /// your system; for example, does the command `spd-say hello` work? InitializationError, @@ -162,14 +162,14 @@ pub enum SpeechDispatcherError { OperationFailed, } -impl std::error::Error for SpeechDispatcherError {} +impl std::error::Error for Error {} -impl fmt::Display for SpeechDispatcherError { +impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use SpeechDispatcherError::*; + use Error::*; match self { - InitializationError => write!(f, "Failed to initialize"), - OperationFailed => write!(f, "Operation failed"), + InitializationError => write!(f, "failed to initialize"), + OperationFailed => write!(f, "operation failed"), } } } @@ -180,7 +180,7 @@ impl Connection { connection_name: S, user_name: S, mode: Mode, - ) -> Result { + ) -> Result { let clientname = CString::new(client_name.into()).unwrap(); let connectionname = CString::new(connection_name.into()).unwrap(); let username = CString::new(user_name.into()).unwrap(); @@ -192,7 +192,7 @@ impl Connection { mode as u32, ); if c.is_null() { - Err(SpeechDispatcherError::InitializationError) + Err(Error::InitializationError) } else { Ok(Self::setup_connection(c)) } @@ -209,7 +209,7 @@ impl Connection { mode: Mode, address: *mut Address, autospawn: bool, - ) -> Result { + ) -> Result { let auto_spawn = if autospawn { 1 } else { 0 }; let error_result = vec![CString::new("").unwrap().into_raw()].as_mut_ptr(); let clientname = CString::new(client_name.into()).unwrap(); @@ -226,7 +226,7 @@ impl Connection { error_result, ); if c.is_null() { - Err(SpeechDispatcherError::InitializationError) + Err(Error::InitializationError) } else { Ok(Self::setup_connection(c)) } @@ -246,7 +246,7 @@ impl Connection { c } - fn setup(&mut self) -> Result<(), SpeechDispatcherError> { + fn setup(&mut self) -> Result<(), Error> { let client_id = self.send_data("HISTORY GET CLIENT_ID\r\n", true); if let Some(client_id) = client_id { let client_id: Vec<&str> = client_id.split("\r\n").collect(); @@ -262,7 +262,7 @@ impl Connection { } callbacks.lock().unwrap().insert(self.1, Default::default()); self.set_notification_on(Notification::All) - .map_err(|_| SpeechDispatcherError::InitializationError)?; + .map_err(|_| Error::InitializationError)?; Ok(()) } @@ -292,87 +292,79 @@ impl Connection { } } - pub fn stop(&self) -> Result<(), SpeechDispatcherError> { + pub fn stop(&self) -> Result<(), Error> { let v = unsafe { spd_stop(self.0) }; c_int_to_result(v) } - pub fn stop_all(&self) -> Result<(), SpeechDispatcherError> { + pub fn stop_all(&self) -> Result<(), Error> { let v = unsafe { spd_stop_all(self.0) }; c_int_to_result(v) } - pub fn stop_uid(&self, target_uid: i32) -> Result<(), SpeechDispatcherError> { + pub fn stop_uid(&self, target_uid: i32) -> Result<(), Error> { let v = unsafe { spd_stop_uid(self.0, target_uid) }; c_int_to_result(v) } - pub fn cancel(&self) -> Result<(), SpeechDispatcherError> { + pub fn cancel(&self) -> Result<(), Error> { let v = unsafe { spd_cancel(self.0) }; c_int_to_result(v) } - pub fn cancel_all(&self) -> Result<(), SpeechDispatcherError> { + pub fn cancel_all(&self) -> Result<(), Error> { let v = unsafe { spd_cancel_all(self.0) }; c_int_to_result(v) } - pub fn cancel_uid(&self, target_uid: i32) -> Result<(), SpeechDispatcherError> { + pub fn cancel_uid(&self, target_uid: i32) -> Result<(), Error> { let v = unsafe { spd_cancel_uid(self.0, target_uid) }; c_int_to_result(v) } - pub fn pause(&self) -> Result<(), SpeechDispatcherError> { + pub fn pause(&self) -> Result<(), Error> { let v = unsafe { spd_pause(self.0) }; c_int_to_result(v) } - pub fn pause_all(&self) -> Result<(), SpeechDispatcherError> { + pub fn pause_all(&self) -> Result<(), Error> { let v = unsafe { spd_pause_all(self.0) }; c_int_to_result(v) } - pub fn pause_uid(&self, target_uid: i32) -> Result<(), SpeechDispatcherError> { + pub fn pause_uid(&self, target_uid: i32) -> Result<(), Error> { let v = unsafe { spd_pause_uid(self.0, target_uid) }; c_int_to_result(v) } - pub fn resume(&self) -> Result<(), SpeechDispatcherError> { + pub fn resume(&self) -> Result<(), Error> { let v = unsafe { spd_resume(self.0) }; c_int_to_result(v) } - pub fn resume_all(&self) -> Result<(), SpeechDispatcherError> { + pub fn resume_all(&self) -> Result<(), Error> { let v = unsafe { spd_resume_all(self.0) }; c_int_to_result(v) } - pub fn resume_uid(&self, target_uid: i32) -> Result<(), SpeechDispatcherError> { + pub fn resume_uid(&self, target_uid: i32) -> Result<(), Error> { let v = unsafe { spd_resume_uid(self.0, target_uid) }; c_int_to_result(v) } - pub fn key>( - &self, - priority: Priority, - key_name: S, - ) -> Result<(), SpeechDispatcherError> { + pub fn key>(&self, priority: Priority, key_name: S) -> Result<(), Error> { let param = CString::new(key_name.into()).unwrap(); let v = unsafe { spd_key(self.0, priority as u32, param.as_ptr()) }; c_int_to_result(v) } - pub fn char>( - &self, - priority: Priority, - char: S, - ) -> Result<(), SpeechDispatcherError> { + pub fn char>(&self, priority: Priority, char: S) -> Result<(), Error> { let param = CString::new(char.into()).unwrap(); let v = unsafe { spd_char(self.0, priority as u32, param.as_ptr()) }; c_int_to_result(v) } - pub fn wchar(&self, priority: Priority, wchar: i32) -> Result<(), SpeechDispatcherError> { + pub fn wchar(&self, priority: Priority, wchar: i32) -> Result<(), Error> { let v = unsafe { spd_wchar(self.0, priority as u32, wchar as wchar_t) }; c_int_to_result(v) } @@ -381,32 +373,28 @@ impl Connection { &self, priority: Priority, icon_name: S, - ) -> Result<(), SpeechDispatcherError> { + ) -> Result<(), Error> { let param = CString::new(icon_name.into()).unwrap(); let v = unsafe { spd_char(self.0, priority as u32, param.as_ptr()) }; c_int_to_result(v) } - pub fn set_voice_type(&self, voice_type: VoiceType) -> Result<(), SpeechDispatcherError> { + pub fn set_voice_type(&self, voice_type: VoiceType) -> Result<(), Error> { let v = unsafe { spd_set_voice_type(self.0, voice_type as u32) }; c_int_to_result(v) } - pub fn set_voice_type_all(&self, voice_type: VoiceType) -> Result<(), SpeechDispatcherError> { + pub fn set_voice_type_all(&self, voice_type: VoiceType) -> Result<(), Error> { let v = unsafe { spd_set_voice_type_all(self.0, voice_type as u32) }; c_int_to_result(v) } - pub fn set_voice_type_uid( - &self, - voice_type: VoiceType, - target_uid: u32, - ) -> Result<(), SpeechDispatcherError> { + pub fn set_voice_type_uid(&self, voice_type: VoiceType, target_uid: u32) -> Result<(), Error> { let v = unsafe { spd_set_voice_type_uid(self.0, voice_type as u32, target_uid) }; c_int_to_result(v) } - pub fn get_voice_type(&self) -> Result { + pub fn get_voice_type(&self) -> Result { let v = unsafe { spd_get_voice_type(self.0) }; Ok(match v { SPDVoiceType::SPD_MALE1 => VoiceType::Male1, @@ -417,23 +405,17 @@ impl Connection { SPDVoiceType::SPD_FEMALE3 => VoiceType::Female3, SPDVoiceType::SPD_CHILD_MALE => VoiceType::ChildMale, SPDVoiceType::SPD_CHILD_FEMALE => VoiceType::ChildFemale, - _ => return Err(SpeechDispatcherError::OperationFailed), // can this happen? + _ => return Err(Error::OperationFailed), // can this happen? }) } - pub fn set_synthesis_voice>( - &self, - voice_name: S, - ) -> Result<(), SpeechDispatcherError> { + pub fn set_synthesis_voice>(&self, voice_name: S) -> Result<(), Error> { let param = CString::new(voice_name.into()).unwrap(); let v = unsafe { spd_set_synthesis_voice(self.0, param.as_ptr()) }; c_int_to_result(v) } - pub fn set_synthesis_voice_all>( - &self, - voice_name: S, - ) -> Result<(), SpeechDispatcherError> { + pub fn set_synthesis_voice_all>(&self, voice_name: S) -> Result<(), Error> { let param = CString::new(voice_name.into()).unwrap(); let v = unsafe { spd_set_synthesis_voice_all(self.0, param.as_ptr()) }; c_int_to_result(v) @@ -443,29 +425,23 @@ impl Connection { &self, voice_name: S, target_uid: u32, - ) -> Result<(), SpeechDispatcherError> { + ) -> Result<(), Error> { let param = CString::new(voice_name.into()).unwrap(); let v = unsafe { spd_set_synthesis_voice_uid(self.0, param.as_ptr(), target_uid) }; c_int_to_result(v) } - pub fn set_data_mode(&self, mode: DataMode) -> Result<(), SpeechDispatcherError> { + pub fn set_data_mode(&self, mode: DataMode) -> Result<(), Error> { let v = unsafe { spd_set_data_mode(self.0, mode as u32) }; c_int_to_result(v) } - pub fn set_notification_on( - &self, - notification: Notification, - ) -> Result<(), SpeechDispatcherError> { + pub fn set_notification_on(&self, notification: Notification) -> Result<(), Error> { let v = unsafe { spd_set_notification_on(self.0, notification as u32) }; c_int_to_result(v) } - pub fn set_notification_off( - &self, - notification: Notification, - ) -> Result<(), SpeechDispatcherError> { + pub fn set_notification_off(&self, notification: Notification) -> Result<(), Error> { let v = unsafe { spd_set_notification_off(self.0, notification as u32) }; c_int_to_result(v) } @@ -474,27 +450,23 @@ impl Connection { &self, notification: Notification, state: S, - ) -> Result<(), SpeechDispatcherError> { + ) -> Result<(), Error> { let param = CString::new(state.into()).unwrap(); let v = unsafe { spd_set_notification(self.0, notification as u32, param.as_ptr()) }; c_int_to_result(v) } - pub fn set_voice_rate(&self, rate: i32) -> Result<(), SpeechDispatcherError> { + pub fn set_voice_rate(&self, rate: i32) -> Result<(), Error> { let v = unsafe { spd_set_voice_rate(self.0, rate) }; c_int_to_result(v) } - pub fn set_voice_rate_all(&self, rate: i32) -> Result<(), SpeechDispatcherError> { + pub fn set_voice_rate_all(&self, rate: i32) -> Result<(), Error> { let v = unsafe { spd_set_voice_rate_all(self.0, rate) }; c_int_to_result(v) } - pub fn set_voice_rate_uid( - &self, - rate: i32, - target_uid: u32, - ) -> Result<(), SpeechDispatcherError> { + pub fn set_voice_rate_uid(&self, rate: i32, target_uid: u32) -> Result<(), Error> { let v = unsafe { spd_set_voice_rate_uid(self.0, rate, target_uid) }; c_int_to_result(v) } @@ -503,21 +475,17 @@ impl Connection { unsafe { spd_get_voice_rate(self.0) } } - pub fn set_voice_pitch(&self, pitch: i32) -> Result<(), SpeechDispatcherError> { + pub fn set_voice_pitch(&self, pitch: i32) -> Result<(), Error> { let v = unsafe { spd_set_voice_pitch(self.0, pitch) }; c_int_to_result(v) } - pub fn set_voice_pitch_all(&self, pitch: i32) -> Result<(), SpeechDispatcherError> { + pub fn set_voice_pitch_all(&self, pitch: i32) -> Result<(), Error> { let v = unsafe { spd_set_voice_pitch_all(self.0, pitch) }; c_int_to_result(v) } - pub fn set_voice_pitch_uid( - &self, - pitch: i32, - target_uid: u32, - ) -> Result<(), SpeechDispatcherError> { + pub fn set_voice_pitch_uid(&self, pitch: i32, target_uid: u32) -> Result<(), Error> { let v = unsafe { spd_set_voice_pitch_uid(self.0, pitch, target_uid) }; c_int_to_result(v) } @@ -526,21 +494,17 @@ impl Connection { unsafe { spd_get_voice_pitch(self.0) } } - pub fn set_volume(&self, volume: i32) -> Result<(), SpeechDispatcherError> { + pub fn set_volume(&self, volume: i32) -> Result<(), Error> { let v = unsafe { spd_set_volume(self.0, volume) }; c_int_to_result(v) } - pub fn set_volume_all(&self, volume: i32) -> Result<(), SpeechDispatcherError> { + pub fn set_volume_all(&self, volume: i32) -> Result<(), Error> { let v = unsafe { spd_set_volume_all(self.0, volume) }; c_int_to_result(v) } - pub fn set_volume_uid( - &self, - volume: i32, - target_uid: u32, - ) -> Result<(), SpeechDispatcherError> { + pub fn set_volume_uid(&self, volume: i32, target_uid: u32) -> Result<(), Error> { let v = unsafe { spd_set_volume_uid(self.0, volume, target_uid) }; c_int_to_result(v) } @@ -549,15 +513,12 @@ impl Connection { unsafe { spd_get_volume(self.0) } } - pub fn set_punctuation(&self, punctuation: Punctuation) -> Result<(), SpeechDispatcherError> { + pub fn set_punctuation(&self, punctuation: Punctuation) -> Result<(), Error> { let v = unsafe { spd_set_punctuation(self.0, punctuation as u32) }; c_int_to_result(v) } - pub fn set_punctuation_all( - &self, - punctuation: Punctuation, - ) -> Result<(), SpeechDispatcherError> { + pub fn set_punctuation_all(&self, punctuation: Punctuation) -> Result<(), Error> { let v = unsafe { spd_set_punctuation_all(self.0, punctuation as u32) }; c_int_to_result(v) } @@ -566,23 +527,17 @@ impl Connection { &self, punctuation: Punctuation, target_uid: u32, - ) -> Result<(), SpeechDispatcherError> { + ) -> Result<(), Error> { let v = unsafe { spd_set_punctuation_uid(self.0, punctuation as u32, target_uid) }; c_int_to_result(v) } - pub fn set_capital_letters( - &self, - capital_letters: CapitalLetters, - ) -> Result<(), SpeechDispatcherError> { + pub fn set_capital_letters(&self, capital_letters: CapitalLetters) -> Result<(), Error> { let v = unsafe { spd_set_capital_letters(self.0, capital_letters as u32) }; c_int_to_result(v) } - pub fn set_capital_letters_all( - &self, - capital_letters: CapitalLetters, - ) -> Result<(), SpeechDispatcherError> { + pub fn set_capital_letters_all(&self, capital_letters: CapitalLetters) -> Result<(), Error> { let v = unsafe { spd_set_capital_letters_all(self.0, capital_letters as u32) }; c_int_to_result(v) } @@ -591,12 +546,12 @@ impl Connection { &self, capital_letters: CapitalLetters, target_uid: u32, - ) -> Result<(), SpeechDispatcherError> { + ) -> Result<(), Error> { let v = unsafe { spd_set_capital_letters_uid(self.0, capital_letters as u32, target_uid) }; c_int_to_result(v) } - pub fn set_spelling(&self, spelling: bool) -> Result<(), SpeechDispatcherError> { + pub fn set_spelling(&self, spelling: bool) -> Result<(), Error> { let s = if spelling { SPDSpelling::SPD_SPELL_ON } else { @@ -606,7 +561,7 @@ impl Connection { c_int_to_result(v) } - pub fn set_spelling_all(&self, spelling: bool) -> Result<(), SpeechDispatcherError> { + pub fn set_spelling_all(&self, spelling: bool) -> Result<(), Error> { let s = if spelling { SPDSpelling::SPD_SPELL_ON } else { @@ -616,11 +571,7 @@ impl Connection { c_int_to_result(v) } - pub fn set_spelling_uid( - &self, - spelling: bool, - target_uid: u32, - ) -> Result<(), SpeechDispatcherError> { + pub fn set_spelling_uid(&self, spelling: bool, target_uid: u32) -> Result<(), Error> { let s = if spelling { SPDSpelling::SPD_SPELL_ON } else { @@ -630,16 +581,13 @@ impl Connection { c_int_to_result(v) } - pub fn set_language>(&self, language: S) -> Result<(), SpeechDispatcherError> { + pub fn set_language>(&self, language: S) -> Result<(), Error> { let param = CString::new(language.into()).unwrap(); let v = unsafe { spd_set_language(self.0, param.as_ptr()) }; c_int_to_result(v) } - pub fn set_language_all>( - &self, - language: S, - ) -> Result<(), SpeechDispatcherError> { + pub fn set_language_all>(&self, language: S) -> Result<(), Error> { let param = CString::new(language.into()).unwrap(); let v = unsafe { spd_set_language_all(self.0, param.as_ptr()) }; c_int_to_result(v) @@ -649,37 +597,29 @@ impl Connection { &self, language: S, target_uid: u32, - ) -> Result<(), SpeechDispatcherError> { + ) -> Result<(), Error> { let param = CString::new(language.into()).unwrap(); let v = unsafe { spd_set_language_uid(self.0, param.as_ptr(), target_uid) }; c_int_to_result(v) } - pub fn get_language(&self) -> Result<&str, SpeechDispatcherError> { + pub fn get_language(&self) -> Result<&str, Error> { let language = unsafe { spd_get_language(self.0) }; if language.is_null() { - Err(SpeechDispatcherError::OperationFailed) + Err(Error::OperationFailed) } else { let language = unsafe { CStr::from_ptr(language) }; - language - .to_str() - .map_err(|_| SpeechDispatcherError::OperationFailed) + language.to_str().map_err(|_| Error::OperationFailed) } } - pub fn set_output_module>( - &self, - output_module: S, - ) -> Result<(), SpeechDispatcherError> { + pub fn set_output_module>(&self, output_module: S) -> Result<(), Error> { let param = CString::new(output_module.into()).unwrap(); let v = unsafe { spd_set_output_module(self.0, param.as_ptr()) }; c_int_to_result(v) } - pub fn set_output_module_all>( - &self, - output_module: S, - ) -> Result<(), SpeechDispatcherError> { + pub fn set_output_module_all>(&self, output_module: S) -> Result<(), Error> { let param = CString::new(output_module.into()).unwrap(); let v = unsafe { spd_set_output_module_all(self.0, param.as_ptr()) }; c_int_to_result(v) @@ -689,7 +629,7 @@ impl Connection { &self, output_module: S, target_uid: u32, - ) -> Result<(), SpeechDispatcherError> { + ) -> Result<(), Error> { let param = CString::new(output_module.into()).unwrap(); let v = unsafe { spd_set_output_module_uid(self.0, param.as_ptr(), target_uid) }; c_int_to_result(v)