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
This commit is contained in:
Malloc Voidstar 2021-12-06 10:57:04 -08:00
parent e0170aa011
commit 7d3edccdda
No known key found for this signature in database
GPG Key ID: C34BA7CBAF747755
2 changed files with 228 additions and 132 deletions

View File

@ -22,18 +22,18 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
connection.client_id() connection.client_id()
), ),
); );
connection.set_voice_rate(100); connection.set_voice_rate(100)?;
connection.say(Priority::Important, "This is faster."); connection.say(Priority::Important, "This is faster.");
connection.set_voice_rate(0); connection.set_voice_rate(0)?;
connection.set_spelling(true); connection.set_spelling(true)?;
connection.say(Priority::Important, "This is spelled."); connection.say(Priority::Important, "This is spelled.");
connection.set_spelling(false); connection.set_spelling(false)?;
connection.set_punctuation(Punctuation::All); connection.set_punctuation(Punctuation::All)?;
connection.say( connection.say(
Priority::Important, Priority::Important,
"This statement, unlike others, has punctuation that is spoken!", "This statement, unlike others, has punctuation that is spoken!",
); );
connection.set_punctuation(Punctuation::None); connection.set_punctuation(Punctuation::None)?;
let mut _input = String::new(); let mut _input = String::new();
io::stdin().read_line(&mut _input).unwrap(); io::stdin().read_line(&mut _input).unwrap();
Ok(()) Ok(())

View File

@ -5,7 +5,7 @@ use std::{
ffi::{CStr, CString}, ffi::{CStr, CString},
fmt, fmt,
marker::Send, marker::Send,
os::raw::c_char, os::raw::{c_char, c_int},
sync::Mutex, sync::Mutex,
}; };
@ -82,8 +82,12 @@ pub enum CapitalLetters {
Icon = SPDCapitalLetters::SPD_CAP_ICON, Icon = SPDCapitalLetters::SPD_CAP_ICON,
} }
fn i32_to_bool(v: i32) -> bool { /// Converts a `0` to a success and everything else to an error.
v == 1 fn c_int_to_result(r: c_int) -> Result<(), SpeechDispatcherError> {
match r {
0 => Ok(()),
_ => Err(SpeechDispatcherError::OperationFailed),
}
} }
#[derive(Default)] #[derive(Default)]
@ -151,7 +155,11 @@ unsafe extern "C" fn cb_im(msg_id: u64, client_id: u64, state: u32, index_mark:
#[derive(Debug)] #[derive(Debug)]
pub enum SpeechDispatcherError { 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, InitializationError,
/// The operation failed
OperationFailed,
} }
impl std::error::Error for SpeechDispatcherError {} impl std::error::Error for SpeechDispatcherError {}
@ -161,6 +169,7 @@ impl fmt::Display for SpeechDispatcherError {
use SpeechDispatcherError::*; use SpeechDispatcherError::*;
match self { match self {
InitializationError => write!(f, "Failed to initialize"), InitializationError => write!(f, "Failed to initialize"),
OperationFailed => write!(f, "Operation failed"),
} }
} }
} }
@ -183,18 +192,14 @@ impl Connection {
mode as u32, mode as u32,
); );
if c.is_null() { if c.is_null() {
None Err(SpeechDispatcherError::InitializationError)
} else { } else {
Some(Self::setup_connection(c)) Ok(Self::setup_connection(c))
} }
}; };
if let Some(connection) = connection { let mut c = Self(connection?, 0);
let mut c = Self(connection, 0); c.setup()?;
c.setup(); Ok(c)
Ok(c)
} else {
Err(SpeechDispatcherError::InitializationError)
}
} }
pub unsafe fn open2<S: Into<String>>( pub unsafe fn open2<S: Into<String>>(
@ -204,7 +209,7 @@ impl Connection {
mode: Mode, mode: Mode,
address: *mut Address, address: *mut Address,
autospawn: bool, autospawn: bool,
) -> Self { ) -> Result<Self, SpeechDispatcherError> {
let auto_spawn = if autospawn { 1 } else { 0 }; let auto_spawn = if autospawn { 1 } else { 0 };
let error_result = vec![CString::new("").unwrap().into_raw()].as_mut_ptr(); let error_result = vec![CString::new("").unwrap().into_raw()].as_mut_ptr();
let clientname = CString::new(client_name.into()).unwrap(); let clientname = CString::new(client_name.into()).unwrap();
@ -220,11 +225,15 @@ impl Connection {
auto_spawn, auto_spawn,
error_result, 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); let mut c = Self(connection?, 0);
c.setup(); c.setup()?;
c Ok(c)
} }
unsafe fn setup_connection(c: *mut SPDConnection) -> *mut SPDConnection { unsafe fn setup_connection(c: *mut SPDConnection) -> *mut SPDConnection {
@ -237,7 +246,7 @@ impl Connection {
c c
} }
fn setup(&mut self) { fn setup(&mut self) -> Result<(), SpeechDispatcherError> {
let client_id = self.send_data("HISTORY GET CLIENT_ID\r\n", true); let client_id = self.send_data("HISTORY GET CLIENT_ID\r\n", true);
if let Some(client_id) = client_id { if let Some(client_id) = client_id {
let client_id: Vec<&str> = client_id.split("\r\n").collect(); 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()); 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) { 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) }; 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) }; 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) }; 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) }; 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) }; 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) }; 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) }; 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) }; 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) }; 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) }; 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) }; 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) }; let v = unsafe { spd_resume_uid(self.0, target_uid) };
i32_to_bool(v) c_int_to_result(v)
} }
pub fn key<S: Into<String>>(&self, priority: Priority, key_name: S) -> bool { pub fn key<S: Into<String>>(
&self,
priority: Priority,
key_name: S,
) -> Result<(), SpeechDispatcherError> {
let param = CString::new(key_name.into()).unwrap(); let param = CString::new(key_name.into()).unwrap();
let v = unsafe { spd_key(self.0, priority as u32, param.as_ptr()) }; 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<S: Into<String>>(&self, priority: Priority, char: S) -> bool { pub fn char<S: Into<String>>(
&self,
priority: Priority,
char: S,
) -> Result<(), SpeechDispatcherError> {
let param = CString::new(char.into()).unwrap(); let param = CString::new(char.into()).unwrap();
let v = unsafe { spd_char(self.0, priority as u32, param.as_ptr()) }; 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) }; 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<S: Into<String>>(&self, priority: Priority, icon_name: S) -> bool { pub fn sound_icon<S: Into<String>>(
&self,
priority: Priority,
icon_name: S,
) -> Result<(), SpeechDispatcherError> {
let param = CString::new(icon_name.into()).unwrap(); let param = CString::new(icon_name.into()).unwrap();
let v = unsafe { spd_char(self.0, priority as u32, param.as_ptr()) }; 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) }; 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) }; 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) }; 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<VoiceType, SpeechDispatcherError> {
let v = unsafe { spd_get_voice_type(self.0) }; let v = unsafe { spd_get_voice_type(self.0) };
match v { Ok(match v {
SPDVoiceType::SPD_MALE1 => VoiceType::Male1, SPDVoiceType::SPD_MALE1 => VoiceType::Male1,
SPDVoiceType::SPD_MALE2 => VoiceType::Male2, SPDVoiceType::SPD_MALE2 => VoiceType::Male2,
SPDVoiceType::SPD_MALE3 => VoiceType::Male3, SPDVoiceType::SPD_MALE3 => VoiceType::Male3,
@ -390,213 +417,282 @@ impl Connection {
SPDVoiceType::SPD_FEMALE3 => VoiceType::Female3, SPDVoiceType::SPD_FEMALE3 => VoiceType::Female3,
SPDVoiceType::SPD_CHILD_MALE => VoiceType::ChildMale, SPDVoiceType::SPD_CHILD_MALE => VoiceType::ChildMale,
SPDVoiceType::SPD_CHILD_FEMALE => VoiceType::ChildFemale, SPDVoiceType::SPD_CHILD_FEMALE => VoiceType::ChildFemale,
_ => panic!("Invalid voice type"), _ => return Err(SpeechDispatcherError::OperationFailed), // can this happen?
} })
} }
pub fn set_synthesis_voice<S: Into<String>>(&self, voice_name: S) -> bool { pub fn set_synthesis_voice<S: Into<String>>(
&self,
voice_name: S,
) -> Result<(), SpeechDispatcherError> {
let param = CString::new(voice_name.into()).unwrap(); let param = CString::new(voice_name.into()).unwrap();
let v = unsafe { spd_set_synthesis_voice(self.0, param.as_ptr()) }; 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<S: Into<String>>(&self, voice_name: S) -> bool { pub fn set_synthesis_voice_all<S: Into<String>>(
&self,
voice_name: S,
) -> Result<(), SpeechDispatcherError> {
let param = CString::new(voice_name.into()).unwrap(); let param = CString::new(voice_name.into()).unwrap();
let v = unsafe { spd_set_synthesis_voice_all(self.0, param.as_ptr()) }; 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<S: Into<String>>(&self, voice_name: S, target_uid: u32) -> bool { pub fn set_synthesis_voice_uid<S: Into<String>>(
&self,
voice_name: S,
target_uid: u32,
) -> Result<(), SpeechDispatcherError> {
let param = CString::new(voice_name.into()).unwrap(); let param = CString::new(voice_name.into()).unwrap();
let v = unsafe { spd_set_synthesis_voice_uid(self.0, param.as_ptr(), target_uid) }; 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) }; 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) }; 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) }; 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<S: Into<String>>(&self, notification: Notification, state: S) -> bool { pub fn set_notification<S: Into<String>>(
&self,
notification: Notification,
state: S,
) -> Result<(), SpeechDispatcherError> {
let param = CString::new(state.into()).unwrap(); let param = CString::new(state.into()).unwrap();
let v = unsafe { spd_set_notification(self.0, notification as u32, param.as_ptr()) }; 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) }; 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) }; 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) }; 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 { pub fn get_voice_rate(&self) -> i32 {
unsafe { spd_get_voice_rate(self.0) } 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) }; 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) }; 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) }; 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 { pub fn get_voice_pitch(&self) -> i32 {
unsafe { spd_get_voice_pitch(self.0) } 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) }; 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) }; 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) }; 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 { pub fn get_volume(&self) -> i32 {
unsafe { spd_get_volume(self.0) } 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) }; 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) }; 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) }; 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) }; 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) }; 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( pub fn set_capital_letters_uid(
&self, &self,
capital_letters: CapitalLetters, capital_letters: CapitalLetters,
target_uid: u32, target_uid: u32,
) -> bool { ) -> Result<(), SpeechDispatcherError> {
let v = unsafe { spd_set_capital_letters_uid(self.0, capital_letters as u32, target_uid) }; 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 { let s = if spelling {
SPDSpelling::SPD_SPELL_ON SPDSpelling::SPD_SPELL_ON
} else { } else {
SPDSpelling::SPD_SPELL_OFF SPDSpelling::SPD_SPELL_OFF
}; };
let v = unsafe { spd_set_spelling(self.0, s) }; 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 { let s = if spelling {
SPDSpelling::SPD_SPELL_ON SPDSpelling::SPD_SPELL_ON
} else { } else {
SPDSpelling::SPD_SPELL_OFF SPDSpelling::SPD_SPELL_OFF
}; };
let v = unsafe { spd_set_spelling_all(self.0, s) }; 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 { let s = if spelling {
SPDSpelling::SPD_SPELL_ON SPDSpelling::SPD_SPELL_ON
} else { } else {
SPDSpelling::SPD_SPELL_OFF SPDSpelling::SPD_SPELL_OFF
}; };
let v = unsafe { spd_set_spelling_uid(self.0, s, target_uid) }; 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<S: Into<String>>(&self, language: S) -> bool { pub fn set_language<S: Into<String>>(&self, language: S) -> Result<(), SpeechDispatcherError> {
let param = CString::new(language.into()).unwrap(); let param = CString::new(language.into()).unwrap();
let v = unsafe { spd_set_language(self.0, param.as_ptr()) }; 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<S: Into<String>>(&self, language: S) -> bool { pub fn set_language_all<S: Into<String>>(
&self,
language: S,
) -> Result<(), SpeechDispatcherError> {
let param = CString::new(language.into()).unwrap(); let param = CString::new(language.into()).unwrap();
let v = unsafe { spd_set_language_all(self.0, param.as_ptr()) }; 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<S: Into<String>>(&self, language: S, target_uid: u32) -> bool { pub fn set_language_uid<S: Into<String>>(
&self,
language: S,
target_uid: u32,
) -> Result<(), SpeechDispatcherError> {
let param = CString::new(language.into()).unwrap(); let param = CString::new(language.into()).unwrap();
let v = unsafe { spd_set_language_uid(self.0, param.as_ptr(), target_uid) }; 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 { pub fn get_language(&self) -> Result<&str, SpeechDispatcherError> {
let v = unsafe { CStr::from_ptr(spd_get_language(self.0)) }; let language = unsafe { spd_get_language(self.0) };
v.to_str().unwrap() 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<S: Into<String>>(&self, output_module: S) -> bool { pub fn set_output_module<S: Into<String>>(
&self,
output_module: S,
) -> Result<(), SpeechDispatcherError> {
let param = CString::new(output_module.into()).unwrap(); let param = CString::new(output_module.into()).unwrap();
let v = unsafe { spd_set_output_module(self.0, param.as_ptr()) }; 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<S: Into<String>>(&self, output_module: S) -> bool { pub fn set_output_module_all<S: Into<String>>(
&self,
output_module: S,
) -> Result<(), SpeechDispatcherError> {
let param = CString::new(output_module.into()).unwrap(); let param = CString::new(output_module.into()).unwrap();
let v = unsafe { spd_set_output_module_all(self.0, param.as_ptr()) }; 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<S: Into<String>>( pub fn set_output_module_uid<S: Into<String>>(
&self, &self,
output_module: S, output_module: S,
target_uid: u32, target_uid: u32,
) -> bool { ) -> Result<(), SpeechDispatcherError> {
let param = CString::new(output_module.into()).unwrap(); let param = CString::new(output_module.into()).unwrap();
let v = unsafe { spd_set_output_module_uid(self.0, param.as_ptr(), target_uid) }; 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<S: Into<String>>(&self, data: S, wait_for_reply: bool) -> Option<String> { pub fn send_data<S: Into<String>>(&self, data: S, wait_for_reply: bool) -> Option<String> {