Eliminate Clippy warnings.

This commit is contained in:
Nolan Darilek 2020-11-17 19:43:57 -06:00
parent b34c880a36
commit e2c1bfa7c9

View File

@ -1,275 +1,269 @@
use std::sync::mpsc::{channel, Receiver}; use std::sync::mpsc::{channel, Receiver};
use gdnative::prelude::*; use gdnative::prelude::*;
use tts::{Features, UtteranceId, TTS as Tts}; use tts::{Features, UtteranceId, TTS as Tts};
#[derive(NativeClass)] #[derive(NativeClass)]
#[inherit(Reference)] #[inherit(Reference)]
struct Utterance(pub(crate) Option<UtteranceId>); struct Utterance(pub(crate) Option<UtteranceId>);
#[methods] #[methods]
impl Utterance { impl Utterance {
fn new(_owner: &Reference) -> Self { fn new(_owner: &Reference) -> Self {
Self(None) Self(None)
} }
} }
enum Msg { #[allow(clippy::enum_variant_names)]
UtteranceBegin(UtteranceId), enum Msg {
UtteranceEnd(UtteranceId), UtteranceBegin(UtteranceId),
UtteranceStop(UtteranceId), UtteranceEnd(UtteranceId),
} UtteranceStop(UtteranceId),
}
#[derive(NativeClass)]
#[inherit(Node)] #[derive(NativeClass)]
#[register_with(Self::register)] #[inherit(Node)]
struct TTS(Tts, Receiver<Msg>); #[register_with(Self::register)]
struct TTS(Tts, Receiver<Msg>);
#[methods]
impl TTS { #[methods]
fn new(owner: &Node) -> Self { impl TTS {
owner.set_pause_mode(2); fn new(owner: &Node) -> Self {
let tts = Tts::default().unwrap(); owner.set_pause_mode(2);
let (tx, rx) = channel(); let tts = Tts::default().unwrap();
let Features { let (tx, rx) = channel();
utterance_callbacks, let Features {
.. utterance_callbacks,
} = tts.supported_features(); ..
if utterance_callbacks { } = tts.supported_features();
let tx_end = tx.clone(); if utterance_callbacks {
let tx_stop = tx.clone(); let tx_end = tx.clone();
tts.on_utterance_begin(Some(Box::new(move |utterance| { let tx_stop = tx.clone();
tx.send(Msg::UtteranceBegin(utterance)).unwrap(); tts.on_utterance_begin(Some(Box::new(move |utterance| {
}))) tx.send(Msg::UtteranceBegin(utterance)).unwrap();
.expect("Failed to set utterance_begin callback"); })))
tts.on_utterance_end(Some(Box::new(move |utterance| { .expect("Failed to set utterance_begin callback");
tx_end.send(Msg::UtteranceEnd(utterance)).unwrap(); tts.on_utterance_end(Some(Box::new(move |utterance| {
}))) tx_end.send(Msg::UtteranceEnd(utterance)).unwrap();
.expect("Failed to set utterance_end callback"); })))
tts.on_utterance_stop(Some(Box::new(move |utterance| { .expect("Failed to set utterance_end callback");
tx_stop.send(Msg::UtteranceStop(utterance)).unwrap(); tts.on_utterance_stop(Some(Box::new(move |utterance| {
}))) tx_stop.send(Msg::UtteranceStop(utterance)).unwrap();
.expect("Failed to set utterance_stop callback"); })))
} .expect("Failed to set utterance_stop callback");
Self(tts, rx) }
} Self(tts, rx)
}
fn register(builder: &ClassBuilder<Self>) {
builder fn register(builder: &ClassBuilder<Self>) {
.add_property("rate") builder
.with_getter(|this: &TTS, _| match this.0.get_rate() { .add_property("rate")
Ok(rate) => rate, .with_getter(|this: &TTS, _| match this.0.get_rate() {
_ => 0., Ok(rate) => rate,
}) _ => 0.,
.with_setter(|this: &mut TTS, _, v: f32| { })
let Features { .with_setter(|this: &mut TTS, _, v: f32| {
rate: rate_supported, let Features {
.. rate: rate_supported,
} = this.0.supported_features(); ..
if rate_supported { } = this.0.supported_features();
let mut v = v; if rate_supported {
if v < this.0.min_rate() { let mut v = v;
v = this.0.min_rate(); if v < this.0.min_rate() {
} else if v > this.0.max_rate() { v = this.0.min_rate();
v = this.0.max_rate(); } else if v > this.0.max_rate() {
} v = this.0.max_rate();
this.0.set_rate(v).unwrap(); }
} this.0.set_rate(v).unwrap();
}) }
.done(); })
builder .done();
.add_property("min_rate") builder
.with_getter(|this: &TTS, _| { .add_property("min_rate")
let Features { .with_getter(|this: &TTS, _| {
rate: rate_supported, let Features {
.. rate: rate_supported,
} = this.0.supported_features(); ..
if rate_supported { } = this.0.supported_features();
this.0.min_rate() if rate_supported {
} else { this.0.min_rate()
0. } else {
} 0.
}) }
.done(); })
builder .done();
.add_property("max_rate") builder
.with_getter(|this: &TTS, _| { .add_property("max_rate")
let Features { .with_getter(|this: &TTS, _| {
rate: rate_supported, let Features {
.. rate: rate_supported,
} = this.0.supported_features(); ..
if rate_supported { } = this.0.supported_features();
this.0.max_rate() if rate_supported {
} else { this.0.max_rate()
0. } else {
} 0.
}) }
.done(); })
builder .done();
.add_property("normal_rate") builder
.with_getter(|this: &TTS, _| { .add_property("normal_rate")
let Features { .with_getter(|this: &TTS, _| {
rate: rate_supported, let Features {
.. rate: rate_supported,
} = this.0.supported_features(); ..
if rate_supported { } = this.0.supported_features();
this.0.normal_rate() if rate_supported {
} else { this.0.normal_rate()
0. } else {
} 0.
}) }
.done(); })
builder .done();
.add_property("can_detect_screen_reader") builder
.with_getter(|_: &TTS, _| { .add_property("can_detect_screen_reader")
if cfg!(all(windows, features = "use_tolk")) { .with_getter(|_: &TTS, _| cfg!(all(windows, features = "use_tolk")))
true .done();
} else { #[allow(unreachable_code)]
false builder
} .add_property("has_screen_reader")
}) .with_getter(|_: &TTS, _| {
.done(); #[cfg(all(windows, features = "use_tolk"))]
#[allow(unreachable_code)] {
builder let tolk = tolk::Tolk::new();
.add_property("has_screen_reader") return tolk.detect_screen_reader().is_some();
.with_getter(|_: &TTS, _| { }
#[cfg(all(windows, features = "use_tolk"))] false
{ })
let tolk = tolk::Tolk::new(); .done();
return tolk.detect_screen_reader().is_some(); builder
} .add_property("can_detect_is_speaking")
false .with_getter(|this: &TTS, _| {
}) let Features {
.done(); is_speaking: is_speaking_supported,
builder ..
.add_property("can_detect_is_speaking") } = this.0.supported_features();
.with_getter(|this: &TTS, _| { is_speaking_supported
let Features { })
is_speaking: is_speaking_supported, .done();
.. builder
} = this.0.supported_features(); .add_property("is_speaking")
return is_speaking_supported; .with_getter(|this: &TTS, _| {
}) let Features {
.done(); is_speaking: is_speaking_supported,
builder ..
.add_property("is_speaking") } = this.0.supported_features();
.with_getter(|this: &TTS, _| { if is_speaking_supported {
let Features { this.0.is_speaking().unwrap()
is_speaking: is_speaking_supported, } else {
.. false
} = this.0.supported_features(); }
if is_speaking_supported { })
return this.0.is_speaking().unwrap(); .done();
} else { builder.add_signal(Signal {
return false; name: "utterance_begin",
} args: &[SignalArgument {
}) name: "utterance",
.done(); default: Variant::default(),
builder.add_signal(Signal { export_info: ExportInfo::new(VariantType::Object),
name: "utterance_begin", usage: PropertyUsage::DEFAULT,
args: &[SignalArgument { }],
name: "utterance", });
default: Variant::default(), builder.add_signal(Signal {
export_info: ExportInfo::new(VariantType::Object), name: "utterance_end",
usage: PropertyUsage::DEFAULT, args: &[SignalArgument {
}], name: "utterance",
}); default: Variant::default(),
builder.add_signal(Signal { export_info: ExportInfo::new(VariantType::Object),
name: "utterance_end", usage: PropertyUsage::DEFAULT,
args: &[SignalArgument { }],
name: "utterance", });
default: Variant::default(), builder.add_signal(Signal {
export_info: ExportInfo::new(VariantType::Object), name: "utterance_stop",
usage: PropertyUsage::DEFAULT, args: &[SignalArgument {
}], name: "utterance",
}); default: Variant::default(),
builder.add_signal(Signal { export_info: ExportInfo::new(VariantType::Object),
name: "utterance_stop", usage: PropertyUsage::DEFAULT,
args: &[SignalArgument { }],
name: "utterance", });
default: Variant::default(), }
export_info: ExportInfo::new(VariantType::Object),
usage: PropertyUsage::DEFAULT, #[export]
}], fn speak(&mut self, _owner: &Node, message: GodotString, interrupt: bool) -> Variant {
}); let message = message.to_string();
} if let Ok(id) = self.0.speak(message, interrupt) {
let utterance: Instance<Utterance, Unique> = Instance::new();
#[export] if id.is_some() {
fn speak(&mut self, _owner: &Node, message: GodotString, interrupt: bool) -> Variant { utterance
let message = message.to_string(); .map_mut(|u, _| u.0 = id)
if let Ok(id) = self.0.speak(message, interrupt) { .expect("Failed to set utterance ID");
let utterance: Instance<Utterance, Unique> = Instance::new(); }
if id.is_some() { utterance.owned_to_variant()
utterance } else {
.map_mut(|u, _| u.0 = id) Variant::default()
.expect("Failed to set utterance ID"); }
} }
let utterance = utterance.owned_to_variant();
utterance #[export]
} else { fn stop(&mut self, _owner: &Node) {
Variant::default() self.0.stop().unwrap();
} }
}
#[export]
#[export] fn is_rate_supported(&mut self, _owner: &Node) -> bool {
fn stop(&mut self, _owner: &Node) { let Features {
self.0.stop().unwrap(); rate: rate_supported,
} ..
} = self.0.supported_features();
#[export] rate_supported
fn is_rate_supported(&mut self, _owner: &Node) -> bool { }
let Features {
rate: rate_supported, #[export]
.. fn are_utterance_callbacks_supported(&mut self, _owner: &Node) -> bool {
} = self.0.supported_features(); let Features {
rate_supported utterance_callbacks: supported,
} ..
} = self.0.supported_features();
#[export] supported
fn are_utterance_callbacks_supported(&mut self, _owner: &Node) -> bool { }
let Features {
utterance_callbacks: supported, #[export]
.. fn _process(&mut self, owner: &Node, _delta: f32) {
} = self.0.supported_features(); if let Ok(msg) = self.1.try_recv() {
supported match msg {
} Msg::UtteranceBegin(utterance_id) => {
let utterance: Instance<Utterance, Unique> = Instance::new();
#[export] utterance
fn _process(&mut self, owner: &Node, _delta: f32) { .map_mut(|u, _| u.0 = Some(utterance_id))
if let Some(msg) = self.1.try_recv().ok() { .expect("Failed to set utterance ID");
match msg { owner.emit_signal("utterance_begin", &[utterance.owned_to_variant()]);
Msg::UtteranceBegin(utterance_id) => { }
let utterance: Instance<Utterance, Unique> = Instance::new(); Msg::UtteranceEnd(utterance_id) => {
utterance let utterance: Instance<Utterance, Unique> = Instance::new();
.map_mut(|u, _| u.0 = Some(utterance_id)) utterance
.expect("Failed to set utterance ID"); .map_mut(|u, _| u.0 = Some(utterance_id))
owner.emit_signal("utterance_begin", &[utterance.owned_to_variant()]); .expect("Failed to set utterance ID");
} owner.emit_signal("utterance_end", &[utterance.owned_to_variant()]);
Msg::UtteranceEnd(utterance_id) => { }
let utterance: Instance<Utterance, Unique> = Instance::new(); Msg::UtteranceStop(utterance_id) => {
utterance let utterance: Instance<Utterance, Unique> = Instance::new();
.map_mut(|u, _| u.0 = Some(utterance_id)) utterance
.expect("Failed to set utterance ID"); .map_mut(|u, _| u.0 = Some(utterance_id))
owner.emit_signal("utterance_end", &[utterance.owned_to_variant()]); .expect("Failed to set utterance ID");
} owner.emit_signal("utterance_stop", &[utterance.owned_to_variant()]);
Msg::UtteranceStop(utterance_id) => { }
let utterance: Instance<Utterance, Unique> = Instance::new(); }
utterance }
.map_mut(|u, _| u.0 = Some(utterance_id)) }
.expect("Failed to set utterance ID"); }
owner.emit_signal("utterance_stop", &[utterance.owned_to_variant()]);
} fn init(handle: InitHandle) {
} env_logger::init();
} handle.add_tool_class::<Utterance>();
} handle.add_class::<TTS>();
} }
fn init(handle: InitHandle) { godot_gdnative_init!();
env_logger::init(); godot_nativescript_init!(init);
handle.add_tool_class::<Utterance>(); godot_gdnative_terminate!();
handle.add_class::<TTS>();
}
godot_gdnative_init!();
godot_nativescript_init!(init);
godot_gdnative_terminate!();