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)