From 142f2e6b3a683e5333c3a1e050761f14b225de75 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Wed, 30 Mar 2022 18:07:08 -0500 Subject: [PATCH] Use plain 'ol struct. --- src/backends/speech_dispatcher.rs | 43 ++++++++++--------------------- src/lib.rs | 38 ++++++++++++--------------- 2 files changed, 30 insertions(+), 51 deletions(-) diff --git a/src/backends/speech_dispatcher.rs b/src/backends/speech_dispatcher.rs index cf50145..f3581d5 100644 --- a/src/backends/speech_dispatcher.rs +++ b/src/backends/speech_dispatcher.rs @@ -1,15 +1,12 @@ -use std::str::FromStr; #[cfg(target_os = "linux")] -use std::{collections::HashMap, sync::Mutex}; +use std::{collections::HashMap, str::FromStr, sync::Mutex}; use lazy_static::*; use log::{info, trace}; -use speech_dispatcher::{Voice as SpdVoice, *}; -use unic_langid::{LanguageIdentifier, LanguageIdentifierError}; +use speech_dispatcher::*; +use unic_langid::LanguageIdentifier; -use crate::{ - Backend, BackendId, Error, Features, Gender, UtteranceId, Voice, VoiceImpl, CALLBACKS, -}; +use crate::{Backend, BackendId, Error, Features, Gender, UtteranceId, Voice, CALLBACKS}; #[derive(Clone, Debug)] pub(crate) struct SpeechDispatcher(Connection); @@ -21,24 +18,6 @@ lazy_static! { }; } -impl VoiceImpl for SpdVoice { - fn id(self) -> String { - self.name - } - - fn name(self) -> String { - self.name - } - - fn gender(self) -> Gender { - Gender::Other - } - - fn language(self) -> Result { - LanguageIdentifier::from_str(&self.language) - } -} - impl SpeechDispatcher { pub(crate) fn new() -> std::result::Result { info!("Initializing SpeechDispatcher backend"); @@ -91,7 +70,7 @@ impl SpeechDispatcher { } } -impl Backend for SpeechDispatcher { +impl Backend for SpeechDispatcher { fn id(&self) -> Option { Some(BackendId::SpeechDispatcher(self.0.client_id())) } @@ -203,14 +182,18 @@ impl Backend for SpeechDispatcher { Ok(*is_speaking) } - fn voices(&self) -> Result>, Error> { + fn voices(&self) -> Result, Error> { let rv = self .0 .list_synthesis_voices()? .iter() - .cloned() - .map(|v| Voice(Box::new(v))) - .collect::>>(); + .map(|v| Voice { + id: v.name.clone(), + name: v.name.clone(), + gender: Gender::Unspecified, + language: LanguageIdentifier::from_str(&v.language).unwrap(), + }) + .collect::>(); Ok(rv) } diff --git a/src/lib.rs b/src/lib.rs index 3945c0a..256a608 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,6 @@ use std::collections::HashMap; #[cfg(target_os = "macos")] use std::ffi::CStr; use std::fmt; -use std::marker::PhantomData; use std::sync::{Arc, Mutex}; use std::{boxed::Box, sync::RwLock}; @@ -34,7 +33,6 @@ use thiserror::Error; #[cfg(all(windows, feature = "tolk"))] use tolk::Tolk; pub use unic_langid::LanguageIdentifier; -use unic_langid::LanguageIdentifierError; mod backends; @@ -211,7 +209,7 @@ pub enum Error { } #[clonable] -pub trait Backend: Clone { +pub trait Backend: Clone { fn id(&self) -> Option; fn supported_features(&self) -> Features; fn speak(&mut self, text: &str, interrupt: bool) -> Result, Error>; @@ -232,7 +230,7 @@ pub trait Backend: Clone { fn get_volume(&self) -> Result; fn set_volume(&mut self, volume: f32) -> Result<(), Error>; fn is_speaking(&self) -> Result; - fn voices(&self) -> Result>, Error>; + fn voices(&self) -> Result, Error>; fn voice(&self) -> Result; fn set_voice(&mut self, voice: &str) -> Result<(), Error>; } @@ -256,22 +254,22 @@ lazy_static! { } #[derive(Clone)] -pub struct Tts(Arc>>>, PhantomData); +pub struct Tts(Arc>>); -unsafe impl Send for Tts {} +unsafe impl Send for Tts {} -unsafe impl Sync for Tts {} +unsafe impl Sync for Tts {} -impl Tts { +impl Tts { /** * Create a new `TTS` instance with the specified backend. */ - pub fn new(backend: Backends) -> Result, Error> { + pub fn new(backend: Backends) -> Result { let backend = match backend { #[cfg(target_os = "linux")] Backends::SpeechDispatcher => { let tts = backends::SpeechDispatcher::new()?; - Ok(Tts(Arc::new(RwLock::new(Box::new(tts))), PhantomData)) + Ok(Tts(Arc::new(RwLock::new(Box::new(tts))))) } #[cfg(target_arch = "wasm32")] Backends::Web => { @@ -317,7 +315,7 @@ impl Tts { } } - pub fn default() -> Result, Error> { + pub fn default() -> Result { #[cfg(target_os = "linux")] let tts = Tts::new(Backends::SpeechDispatcher); #[cfg(all(windows, feature = "tolk"))] @@ -566,7 +564,7 @@ impl Tts { /** * Returns list of available voices. */ - pub fn voices(&self) -> Result>, Error> { + pub fn voices(&self) -> Result, Error> { self.0.read().unwrap().voices() } @@ -682,7 +680,7 @@ impl Tts { } } -impl Drop for Tts { +impl Drop for Tts { fn drop(&mut self) { if Arc::strong_count(&self.0) <= 1 { if let Some(id) = self.0.read().unwrap().id() { @@ -694,16 +692,14 @@ impl Drop for Tts { } pub enum Gender { - Other, + Unspecified, Male, Female, } -pub trait VoiceImpl: Sized { - fn id(self) -> String; - fn name(self) -> String; - fn gender(self) -> Gender; - fn language(self) -> Result; +pub struct Voice { + pub id: String, + pub name: String, + pub gender: Gender, + pub language: LanguageIdentifier, } - -pub struct Voice(Box);