From ac4ebea05a949e6115f40e1f315d49cb3896891b Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Sun, 24 Mar 2019 21:30:45 +0000 Subject: [PATCH] Create list of supported features and check them before a method is called. --- src/backends/speech_dispatcher.rs | 11 ++++- src/backends/web.rs | 9 ++++ src/lib.rs | 74 ++++++++++++++++++++++++++----- 3 files changed, 81 insertions(+), 13 deletions(-) diff --git a/src/backends/speech_dispatcher.rs b/src/backends/speech_dispatcher.rs index ed0b93b..06d7518 100644 --- a/src/backends/speech_dispatcher.rs +++ b/src/backends/speech_dispatcher.rs @@ -4,7 +4,7 @@ use std::u8; use log::{info, trace}; use speech_dispatcher::*; -use crate::{Backend, Error}; +use crate::{Backend, Error, Features}; pub struct SpeechDispatcher(Connection); @@ -29,6 +29,15 @@ fn i32_to_u8(v: i32) -> u8 { } impl Backend for SpeechDispatcher { + fn supported_features(&self) -> Features { + Features { + stop: true, + rate: true, + pitch: true, + volume: true, + } + } + fn speak(&self, text: &str, interrupt: bool) -> Result<(), Error> { trace!("speak({}, {})", text, interrupt); if interrupt { diff --git a/src/backends/web.rs b/src/backends/web.rs index 89f7f72..554bc2f 100644 --- a/src/backends/web.rs +++ b/src/backends/web.rs @@ -24,6 +24,15 @@ impl Web { } impl Backend for Web { + fn supported_features(&self) -> Features { + Features { + stop: true, + rate: true, + pitch: true, + volume: true, + } + } + fn speak(&self, text: &str, interrupt: bool) -> Result<(), Error> { trace!("speak({}, {})", text, interrupt); let utterance = SpeechSynthesisUtterance::new_with_text(text).unwrap(); diff --git a/src/lib.rs b/src/lib.rs index c7c10a2..910eda9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,7 +37,15 @@ impl convert::From for io::Error { } } -trait Backend { +pub struct Features { + stop: bool, + rate: bool, + pitch: bool, + volume: bool, +} + +pub trait Backend { + fn supported_features(&self) -> Features; fn speak(&self, text: &str, interrupt: bool) -> Result<(), Error>; fn stop(&self) -> Result<(), Error>; fn get_rate(&self) -> Result; @@ -78,6 +86,13 @@ impl TTS { tts } + /** + * Returns the features supported by this TTS engine + */ + pub fn supported_features(&self) -> Features { + self.0.supported_features() + } + /** * Speaks the specified text, optionally interrupting current speech. */ @@ -90,52 +105,87 @@ impl TTS { * Stops current speech. */ pub fn stop(&self) -> Result<&Self, Error> { - self.0.stop()?; - Ok(self) + let Features { stop, .. } = self.supported_features(); + if stop { + self.0.stop()?; + Ok(self) + } else { + Err(Error("Feature not supported".to_string())) + } } /** * Gets the current speech rate. */ pub fn get_rate(&self) -> Result { - self.0.get_rate() + let Features { rate, .. } = self.supported_features(); + if rate { + self.0.get_rate() + } else { + Err(Error("Feature not supported".to_string())) + } } /** * Sets the desired speech rate. */ pub fn set_rate(&mut self, rate: u8) -> Result<&Self, Error> { - self.0.set_rate(rate)?; - Ok(self) + let Features { rate: rate_feature, .. } = self.supported_features(); + if rate_feature { + self.0.set_rate(rate)?; + Ok(self) + } else { + Err(Error("Unsupported feature".to_string())) + } } /** * Gets the current speech pitch. */ pub fn get_pitch(&self) -> Result { - self.0.get_pitch() + let Features { pitch, .. } = self.supported_features(); + if pitch { + self.0.get_pitch() + } else { + Err(Error("Feature not supported".to_string())) + } } /** * Sets the desired speech pitch. */ pub fn set_pitch(&mut self, pitch: u8) -> Result<&Self, Error> { - self.0.set_pitch(pitch)?; - Ok(self) + let Features { pitch: pitch_feature, .. } = self.supported_features(); + if pitch_feature { + self.0.set_pitch(pitch)?; + Ok(self) + } else { + Err(Error("Unsupported feature".to_string())) + } } /** * Gets the current speech volume. */ pub fn get_volume(&self) -> Result { - self.0.get_volume() + let Features { volume, .. } = self.supported_features(); + if volume { + self.0.get_volume() + } else { + Err(Error("Unsupported feature".to_string())) + } } /** * Sets the desired speech volume. */ pub fn set_volume(&mut self, volume: u8) -> Result<&Self, Error> { - self.0.set_volume(volume)?; - Ok(self) + let Features { volume: volume_feature, .. } = self.supported_features(); + if volume_feature { + self.0.set_volume(volume)?; + Ok(self) + } else { + Err(Error("Unsupported feature".to_string())) + } } }