Create list of supported features and check them before a method is called.

This commit is contained in:
Nolan Darilek 2019-03-24 21:30:45 +00:00
parent 9724c5e087
commit ac4ebea05a
3 changed files with 81 additions and 13 deletions

View File

@ -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 {

View File

@ -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();

View File

@ -37,7 +37,15 @@ impl convert::From<Error> 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<u8, Error>;
@ -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<u8, Error> {
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<u8, Error> {
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<u8, Error> {
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()))
}
}
}