From 6e8057b023eee428bcfb7a54bac3f053b59f1dd8 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Fri, 4 Feb 2022 16:24:34 +0100 Subject: [PATCH] Make speech-dispatcher opt-in so we can avoid LGPL by default --- Cargo.toml | 4 ++-- src/backends/mod.rs | 4 ++-- src/lib.rs | 46 +++++++++++++++++++++++++-------------------- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0de8652..563a939 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } [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] cocoa-foundation = "0.1" @@ -39,4 +39,4 @@ web-sys = { version = "0.3", features = ["EventTarget", "SpeechSynthesis", "Spee [target.'cfg(target_os="android")'.dependencies] jni = "0.19" -ndk-glue = "0.6" \ No newline at end of file +ndk-glue = "0.6" diff --git a/src/backends/mod.rs b/src/backends/mod.rs index aad23ca..ec5eaf6 100644 --- a/src/backends/mod.rs +++ b/src/backends/mod.rs @@ -1,4 +1,4 @@ -#[cfg(target_os = "linux")] +#[cfg(feature = "speech_dispatcher")] mod speech_dispatcher; #[cfg(all(windows, feature = "tolk"))] @@ -19,7 +19,7 @@ mod av_foundation; #[cfg(target_os = "android")] mod android; -#[cfg(target_os = "linux")] +#[cfg(feature = "speech_dispatcher")] pub(crate) use self::speech_dispatcher::*; #[cfg(all(windows, feature = "tolk"))] diff --git a/src/lib.rs b/src/lib.rs index b157cf7..fe34ff6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,7 +27,7 @@ use lazy_static::lazy_static; use libc::c_char; #[cfg(target_os = "macos")] use objc::{class, msg_send, sel, sel_impl}; -#[cfg(target_os = "linux")] +#[cfg(feature = "speech_dispatcher")] use speech_dispatcher::Error as SpeechDispatcherError; use thiserror::Error; #[cfg(all(windows, feature = "tolk"))] @@ -204,7 +204,7 @@ pub enum Error { #[cfg(target_arch = "wasm32")] #[error("JavaScript error: [0]")] JavaScriptError(wasm_bindgen::JsValue), - #[cfg(target_os = "linux")] + #[cfg(feature = "speech_dispatcher")] #[error("Speech Dispatcher error: {0}")] SpeechDispatcher(#[from] SpeechDispatcherError), #[cfg(windows)] @@ -273,50 +273,56 @@ impl Tts { * Create a new `TTS` instance with the specified backend. */ pub fn new(backend: Backends) -> Result { - let backend = match backend { + let backend: Self = match backend { #[cfg(target_os = "linux")] Backends::SpeechDispatcher => { - let tts = backends::SpeechDispatcher::new()?; - Ok(Tts(Box::new(tts))) + #[cfg(feature = "speech_dispatcher")] + { + 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")] Backends::Web => { let tts = backends::Web::new()?; - Ok(Tts(Box::new(tts))) + Tts(Box::new(tts)) } #[cfg(all(windows, feature = "tolk"))] Backends::Tolk => { let tts = backends::Tolk::new(); if let Some(tts) = tts { - Ok(Tts(Box::new(tts))) + Tts(Box::new(tts)) } else { - Err(Error::NoneError) + return Err(Error::NoneError); } } #[cfg(windows)] Backends::WinRt => { let tts = backends::WinRt::new()?; - Ok(Tts(Box::new(tts))) + Tts(Box::new(tts)) } #[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"))] - Backends::AvFoundation => Ok(Tts(Box::new(backends::AvFoundation::new()))), + Backends::AvFoundation => Tts(Box::new(backends::AvFoundation::new())), #[cfg(target_os = "android")] Backends::Android => { 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() { - let mut callbacks = CALLBACKS.lock().unwrap(); - callbacks.insert(id, Callbacks::default()); - } - Ok(backend) - } else { - backend + if let Some(id) = backend.0.id() { + let mut callbacks = CALLBACKS.lock().unwrap(); + callbacks.insert(id, Callbacks::default()); } + Ok(backend) } pub fn default() -> Result {