Make speech-dispatcher opt-in so we can avoid LGPL by default

This commit is contained in:
Emil Ernerfeldt 2022-02-04 16:24:34 +01:00
parent 4466923620
commit 6e8057b023
3 changed files with 30 additions and 24 deletions

View File

@ -26,7 +26,7 @@ tolk = { version = "0.5", optional = true }
windows = { version = "0.30", features = ["alloc", "Foundation", "Media_Core", "Media_Playback", "Media_SpeechSynthesis", "Storage_Streams"] } windows = { version = "0.30", features = ["alloc", "Foundation", "Media_Core", "Media_Playback", "Media_SpeechSynthesis", "Storage_Streams"] }
[target.'cfg(target_os = "linux")'.dependencies] [target.'cfg(target_os = "linux")'.dependencies]
speech-dispatcher = "0.11" speech-dispatcher = { version = "0.11", optional = true } # optional, since it is using LGPL
[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies] [target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
cocoa-foundation = "0.1" cocoa-foundation = "0.1"
@ -39,4 +39,4 @@ web-sys = { version = "0.3", features = ["EventTarget", "SpeechSynthesis", "Spee
[target.'cfg(target_os="android")'.dependencies] [target.'cfg(target_os="android")'.dependencies]
jni = "0.19" jni = "0.19"
ndk-glue = "0.6" ndk-glue = "0.6"

View File

@ -1,4 +1,4 @@
#[cfg(target_os = "linux")] #[cfg(feature = "speech_dispatcher")]
mod speech_dispatcher; mod speech_dispatcher;
#[cfg(all(windows, feature = "tolk"))] #[cfg(all(windows, feature = "tolk"))]
@ -19,7 +19,7 @@ mod av_foundation;
#[cfg(target_os = "android")] #[cfg(target_os = "android")]
mod android; mod android;
#[cfg(target_os = "linux")] #[cfg(feature = "speech_dispatcher")]
pub(crate) use self::speech_dispatcher::*; pub(crate) use self::speech_dispatcher::*;
#[cfg(all(windows, feature = "tolk"))] #[cfg(all(windows, feature = "tolk"))]

View File

@ -27,7 +27,7 @@ use lazy_static::lazy_static;
use libc::c_char; use libc::c_char;
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
use objc::{class, msg_send, sel, sel_impl}; use objc::{class, msg_send, sel, sel_impl};
#[cfg(target_os = "linux")] #[cfg(feature = "speech_dispatcher")]
use speech_dispatcher::Error as SpeechDispatcherError; use speech_dispatcher::Error as SpeechDispatcherError;
use thiserror::Error; use thiserror::Error;
#[cfg(all(windows, feature = "tolk"))] #[cfg(all(windows, feature = "tolk"))]
@ -204,7 +204,7 @@ pub enum Error {
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
#[error("JavaScript error: [0]")] #[error("JavaScript error: [0]")]
JavaScriptError(wasm_bindgen::JsValue), JavaScriptError(wasm_bindgen::JsValue),
#[cfg(target_os = "linux")] #[cfg(feature = "speech_dispatcher")]
#[error("Speech Dispatcher error: {0}")] #[error("Speech Dispatcher error: {0}")]
SpeechDispatcher(#[from] SpeechDispatcherError), SpeechDispatcher(#[from] SpeechDispatcherError),
#[cfg(windows)] #[cfg(windows)]
@ -273,50 +273,56 @@ impl Tts {
* Create a new `TTS` instance with the specified backend. * Create a new `TTS` instance with the specified backend.
*/ */
pub fn new(backend: Backends) -> Result<Tts, Error> { pub fn new(backend: Backends) -> Result<Tts, Error> {
let backend = match backend { let backend: Self = match backend {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
Backends::SpeechDispatcher => { Backends::SpeechDispatcher => {
let tts = backends::SpeechDispatcher::new()?; #[cfg(feature = "speech_dispatcher")]
Ok(Tts(Box::new(tts))) {
let tts = backends::SpeechDispatcher::new()?;
Tts(Box::new(tts))
}
#[cfg(not(feature = "speech_dispatcher"))]
{
log::error!("tts must be compiled with the speech_dispatcher feature on linux");
return Err(Error::NoneError);
}
} }
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
Backends::Web => { Backends::Web => {
let tts = backends::Web::new()?; let tts = backends::Web::new()?;
Ok(Tts(Box::new(tts))) Tts(Box::new(tts))
} }
#[cfg(all(windows, feature = "tolk"))] #[cfg(all(windows, feature = "tolk"))]
Backends::Tolk => { Backends::Tolk => {
let tts = backends::Tolk::new(); let tts = backends::Tolk::new();
if let Some(tts) = tts { if let Some(tts) = tts {
Ok(Tts(Box::new(tts))) Tts(Box::new(tts))
} else { } else {
Err(Error::NoneError) return Err(Error::NoneError);
} }
} }
#[cfg(windows)] #[cfg(windows)]
Backends::WinRt => { Backends::WinRt => {
let tts = backends::WinRt::new()?; let tts = backends::WinRt::new()?;
Ok(Tts(Box::new(tts))) Tts(Box::new(tts))
} }
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
Backends::AppKit => Ok(Tts(Box::new(backends::AppKit::new()))), Backends::AppKit => Tts(Box::new(backends::AppKit::new())),
#[cfg(any(target_os = "macos", target_os = "ios"))] #[cfg(any(target_os = "macos", target_os = "ios"))]
Backends::AvFoundation => Ok(Tts(Box::new(backends::AvFoundation::new()))), Backends::AvFoundation => Tts(Box::new(backends::AvFoundation::new())),
#[cfg(target_os = "android")] #[cfg(target_os = "android")]
Backends::Android => { Backends::Android => {
let tts = backends::Android::new()?; let tts = backends::Android::new()?;
Ok(Tts(Box::new(tts))) Tts(Box::new(tts))
} }
}; };
if let Ok(backend) = backend { if let Some(id) = backend.0.id() {
if let Some(id) = backend.0.id() { let mut callbacks = CALLBACKS.lock().unwrap();
let mut callbacks = CALLBACKS.lock().unwrap(); callbacks.insert(id, Callbacks::default());
callbacks.insert(id, Callbacks::default());
}
Ok(backend)
} else {
backend
} }
Ok(backend)
} }
pub fn default() -> Result<Tts, Error> { pub fn default() -> Result<Tts, Error> {