Apply clippy suggestions

This commit is contained in:
Elias Projahn 2021-05-08 00:06:01 +02:00
parent 7d7343ea8c
commit c92eece842
19 changed files with 46 additions and 53 deletions

1
clippy.toml Normal file
View file

@ -0,0 +1 @@
type-complexity-threshold = 500

View file

@ -97,7 +97,7 @@ impl Screen<Option<Performance>, Performance> for PerformanceEditor {
spawn!(@clone this, async move { spawn!(@clone this, async move {
if let Some(person) = push!(this.handle, PersonSelector).await { if let Some(person) = push!(this.handle, PersonSelector).await {
this.show_person(Some(&person)); this.show_person(Some(&person));
this.person.replace(Some(person.clone())); this.person.replace(Some(person));
this.show_ensemble(None); this.show_ensemble(None);
this.ensemble.replace(None); this.ensemble.replace(None);
} }

View file

@ -203,7 +203,7 @@ impl RecordingEditor {
self.handle self.handle
.backend .backend
.db() .db()
.update_recording(recording.clone().into()) .update_recording(recording.clone())
.await .await
.unwrap(); .unwrap();

View file

@ -11,7 +11,6 @@ use gtk_macros::get_widget;
use libadwaita::prelude::*; use libadwaita::prelude::*;
use musicus_backend::db::{generate_id, Instrument, Person, Work, WorkPart, WorkSection}; use musicus_backend::db::{generate_id, Instrument, Person, Work, WorkPart, WorkSection};
use std::cell::RefCell; use std::cell::RefCell;
use std::convert::TryInto;
use std::rc::Rc; use std::rc::Rc;
/// Either a work part or a work section. /// Either a work part or a work section.
@ -86,10 +85,7 @@ impl Screen<Option<Work>, Work> for WorkEditor {
} }
for section in work.sections { for section in work.sections {
structure.insert( structure.insert(section.before_index, PartOrSection::Section(section));
section.before_index.try_into().unwrap(),
PartOrSection::Section(section),
);
} }
(work.id, Some(work.composer), work.instruments, structure) (work.id, Some(work.composer), work.instruments, structure)
@ -141,7 +137,7 @@ impl Screen<Option<Work>, Work> for WorkEditor {
spawn!(@clone this, async move { spawn!(@clone this, async move {
if let Some(person) = push!(this.handle, PersonSelector).await { if let Some(person) = push!(this.handle, PersonSelector).await {
this.show_composer(&person); this.show_composer(&person);
this.composer.replace(Some(person.to_owned())); this.composer.replace(Some(person));
} }
}); });
})); }));
@ -179,7 +175,7 @@ impl Screen<Option<Work>, Work> for WorkEditor {
if let Some(instrument) = push!(this.handle, InstrumentSelector).await { if let Some(instrument) = push!(this.handle, InstrumentSelector).await {
let length = { let length = {
let mut instruments = this.instruments.borrow_mut(); let mut instruments = this.instruments.borrow_mut();
instruments.push(instrument.clone()); instruments.push(instrument);
instruments.len() instruments.len()
}; };
@ -344,8 +340,8 @@ impl WorkEditor {
.clone() .clone()
.expect("Tried to create work without composer!"), .expect("Tried to create work without composer!"),
instruments: self.instruments.borrow().clone(), instruments: self.instruments.borrow().clone(),
parts: parts, parts,
sections: sections, sections,
}; };
let upload = self.upload_switch.state(); let upload = self.upload_switch.state();
@ -356,7 +352,7 @@ impl WorkEditor {
self.handle self.handle
.backend .backend
.db() .db()
.update_work(work.clone().into()) .update_work(work.clone())
.await .await
.unwrap(); .unwrap();

View file

@ -211,7 +211,7 @@ impl MediumEditor {
id: generate_id(), id: generate_id(),
name: self.name_entry.text().to_string(), name: self.name_entry.text().to_string(),
discid: Some(self.session.source_id().to_owned()), discid: Some(self.session.source_id().to_owned()),
tracks: tracks, tracks,
}; };
let upload = self.publish_switch.state(); let upload = self.publish_switch.state();

View file

@ -210,7 +210,7 @@ impl MediumPreview {
/// Copy the tracks to the music library and add the medium to the database. /// Copy the tracks to the music library and add the medium to the database.
async fn import(&self) -> Result<()> { async fn import(&self) -> Result<()> {
let medium = self.medium.borrow(); let medium = self.medium.borrow();
let medium = medium.as_ref().ok_or(anyhow!("No medium set!"))?; let medium = medium.as_ref().ok_or_else(|| anyhow!("No medium set!"))?;
// Create a new directory in the music library path for the imported medium. // Create a new directory in the music library path for the imported medium.

View file

@ -6,7 +6,6 @@ use glib::clone;
use gtk::prelude::*; use gtk::prelude::*;
use gtk_macros::get_widget; use gtk_macros::get_widget;
use musicus_backend::import::ImportSession; use musicus_backend::import::ImportSession;
use std::path::PathBuf;
use std::rc::Rc; use std::rc::Rc;
/// A dialog for starting to import music. /// A dialog for starting to import music.
@ -63,7 +62,7 @@ impl Screen<(), ()> for SourceSelector {
this.widget.set_visible_child_name("loading"); this.widget.set_visible_child_name("loading");
spawn!(@clone this, async move { spawn!(@clone this, async move {
match ImportSession::folder(PathBuf::from(path)).await { match ImportSession::folder(path).await {
Ok(session) => { Ok(session) => {
let result = push!(this.handle, ImportScreen, session).await; let result = push!(this.handle, ImportScreen, session).await;
this.handle.pop(result); this.handle.pop(result);

View file

@ -61,10 +61,8 @@ impl Screen<(Recording, Vec<usize>), Vec<usize>> for TrackEditor {
let mut selection = this.selection.borrow_mut(); let mut selection = this.selection.borrow_mut();
if check.is_active() { if check.is_active() {
selection.push(index); selection.push(index);
} else { } else if let Some(pos) = selection.iter().position(|part| *part == index) {
if let Some(pos) = selection.iter().position(|part| *part == index) { selection.remove(pos);
selection.remove(pos);
}
} }
})); }));

View file

@ -65,10 +65,8 @@ impl Screen<Arc<ImportSession>, Vec<usize>> for TrackSelector {
let mut selection = this.selection.borrow_mut(); let mut selection = this.selection.borrow_mut();
if check.is_active() { if check.is_active() {
selection.push(index); selection.push(index);
} else { } else if let Some(pos) = selection.iter().position(|part| *part == index) {
if let Some(pos) = selection.iter().position(|part| *part == index) { selection.remove(pos);
selection.remove(pos);
}
} }
if selection.is_empty() { if selection.is_empty() {

View file

@ -70,7 +70,7 @@ impl Screen<(), LoginData> for RegisterDialog {
let captcha_id = this.captcha_id.borrow().clone().unwrap(); let captcha_id = this.captcha_id.borrow().clone().unwrap();
let answer = this.captcha_entry.text().to_string(); let answer = this.captcha_entry.text().to_string();
let email = if email.len() == 0 { let email = if email.is_empty() {
None None
} else { } else {
Some(email) Some(email)

View file

@ -10,7 +10,7 @@ pub struct ServerDialog {
backend: Rc<Backend>, backend: Rc<Backend>,
window: libadwaita::Window, window: libadwaita::Window,
url_entry: gtk::Entry, url_entry: gtk::Entry,
selected_cb: RefCell<Option<Box<dyn Fn(String) -> ()>>>, selected_cb: RefCell<Option<Box<dyn Fn(String)>>>,
} }
impl ServerDialog { impl ServerDialog {
@ -54,7 +54,7 @@ impl ServerDialog {
} }
/// The closure to call when the server was set. /// The closure to call when the server was set.
pub fn set_selected_cb<F: Fn(String) -> () + 'static>(&self, cb: F) { pub fn set_selected_cb<F: Fn(String) + 'static>(&self, cb: F) {
self.selected_cb.replace(Some(Box::new(cb))); self.selected_cb.replace(Some(Box::new(cb)));
} }

View file

@ -41,13 +41,13 @@ impl Screen<(), Ensemble> for EnsembleSelector {
this.selector this.selector
.set_load_online(clone!(@weak this => @default-panic, move || { .set_load_online(clone!(@weak this => @default-panic, move || {
let clone = this.clone(); let clone = this;
async move { Ok(clone.handle.backend.cl().get_ensembles().await?) } async move { Ok(clone.handle.backend.cl().get_ensembles().await?) }
})); }));
this.selector this.selector
.set_load_local(clone!(@weak this => @default-panic, move || { .set_load_local(clone!(@weak this => @default-panic, move || {
let clone = this.clone(); let clone = this;
async move { clone.handle.backend.db().get_ensembles().await.unwrap() } async move { clone.handle.backend.db().get_ensembles().await.unwrap() }
})); }));

View file

@ -41,13 +41,13 @@ impl Screen<(), Instrument> for InstrumentSelector {
this.selector this.selector
.set_load_online(clone!(@weak this => @default-panic, move || { .set_load_online(clone!(@weak this => @default-panic, move || {
let clone = this.clone(); let clone = this;
async move { Ok(clone.handle.backend.cl().get_instruments().await?) } async move { Ok(clone.handle.backend.cl().get_instruments().await?) }
})); }));
this.selector this.selector
.set_load_local(clone!(@weak this => @default-panic, move || { .set_load_local(clone!(@weak this => @default-panic, move || {
let clone = this.clone(); let clone = this;
async move { clone.handle.backend.db().get_instruments().await.unwrap() } async move { clone.handle.backend.db().get_instruments().await.unwrap() }
})); }));

View file

@ -41,13 +41,13 @@ impl Screen<(), Person> for PersonSelector {
this.selector this.selector
.set_load_online(clone!(@weak this => @default-panic, move || { .set_load_online(clone!(@weak this => @default-panic, move || {
let clone = this.clone(); let clone = this;
async move { Ok(clone.handle.backend.cl().get_persons().await?) } async move { Ok(clone.handle.backend.cl().get_persons().await?) }
})); }));
this.selector this.selector
.set_load_local(clone!(@weak this => @default-panic, move || { .set_load_local(clone!(@weak this => @default-panic, move || {
let clone = this.clone(); let clone = this;
async move { clone.handle.backend.db().get_persons().await.unwrap() } async move { clone.handle.backend.db().get_persons().await.unwrap() }
})); }));

View file

@ -20,8 +20,8 @@ pub struct Selector<T: 'static> {
stack: gtk::Stack, stack: gtk::Stack,
list: Rc<List>, list: Rc<List>,
items: RefCell<Vec<T>>, items: RefCell<Vec<T>>,
back_cb: RefCell<Option<Box<dyn Fn() -> ()>>>, back_cb: RefCell<Option<Box<dyn Fn()>>>,
add_cb: RefCell<Option<Box<dyn Fn() -> ()>>>, add_cb: RefCell<Option<Box<dyn Fn()>>>,
make_widget: RefCell<Option<Box<dyn Fn(&T) -> gtk::Widget>>>, make_widget: RefCell<Option<Box<dyn Fn(&T) -> gtk::Widget>>>,
load_online: RefCell<Option<Box<dyn Fn() -> Box<dyn Future<Output = Result<Vec<T>>>>>>>, load_online: RefCell<Option<Box<dyn Fn() -> Box<dyn Future<Output = Result<Vec<T>>>>>>>,
load_local: RefCell<Option<Box<dyn Fn() -> Box<dyn Future<Output = Vec<T>>>>>>, load_local: RefCell<Option<Box<dyn Fn() -> Box<dyn Future<Output = Vec<T>>>>>>,
@ -147,12 +147,12 @@ impl<T> Selector<T> {
} }
/// Set the closure to be called when the user wants to go back. /// Set the closure to be called when the user wants to go back.
pub fn set_back_cb<F: Fn() -> () + 'static>(&self, cb: F) { pub fn set_back_cb<F: Fn() + 'static>(&self, cb: F) {
self.back_cb.replace(Some(Box::new(cb))); self.back_cb.replace(Some(Box::new(cb)));
} }
/// Set the closure to be called when the user wants to add an item. /// Set the closure to be called when the user wants to add an item.
pub fn set_add_cb<F: Fn() -> () + 'static>(&self, cb: F) { pub fn set_add_cb<F: Fn() + 'static>(&self, cb: F) {
self.add_cb.replace(Some(Box::new(cb))); self.add_cb.replace(Some(Box::new(cb)));
} }

View file

@ -5,16 +5,11 @@ use std::cell::Cell;
glib::wrapper! { glib::wrapper! {
/// A thin list model managing only indices to an external data source. /// A thin list model managing only indices to an external data source.
pub struct IndexedListModel(ObjectSubclass<indexed_list_model::IndexedListModel>) pub struct IndexedListModel(ObjectSubclass<indexed_list_model_imp::IndexedListModel>)
@implements gio::ListModel; @implements gio::ListModel;
} }
impl IndexedListModel { impl IndexedListModel {
/// Create a new indexed list model, which will be empty initially.
pub fn new() -> Self {
glib::Object::new(&[]).unwrap()
}
/// Set the length of the list model. /// Set the length of the list model.
pub fn set_length(&self, length: u32) { pub fn set_length(&self, length: u32) {
let old_length = self.property("length").unwrap().get::<u32>().unwrap(); let old_length = self.property("length").unwrap().get::<u32>().unwrap();
@ -23,7 +18,13 @@ impl IndexedListModel {
} }
} }
mod indexed_list_model { impl Default for IndexedListModel {
fn default() -> Self {
glib::Object::new(&[]).unwrap()
}
}
mod indexed_list_model_imp {
use super::*; use super::*;
#[derive(Debug, Default)] #[derive(Debug, Default)]
@ -97,7 +98,7 @@ mod indexed_list_model {
glib::wrapper! { glib::wrapper! {
/// A simple GObject holding just one integer. /// A simple GObject holding just one integer.
pub struct ItemIndex(ObjectSubclass<item_index::ItemIndex>); pub struct ItemIndex(ObjectSubclass<item_index_imp::ItemIndex>);
} }
impl ItemIndex { impl ItemIndex {
@ -112,7 +113,7 @@ impl ItemIndex {
} }
} }
mod item_index { mod item_index_imp {
use super::*; use super::*;
#[derive(Debug, Default)] #[derive(Debug, Default)]

View file

@ -18,7 +18,7 @@ pub struct List {
impl List { impl List {
/// Create a new list. The list will be empty initially. /// Create a new list. The list will be empty initially.
pub fn new() -> Rc<Self> { pub fn new() -> Rc<Self> {
let model = IndexedListModel::new(); let model = IndexedListModel::default();
let filter = gtk::CustomFilter::new(|_| true); let filter = gtk::CustomFilter::new(|_| true);
let filter_model = gtk::FilterListModel::new(Some(&model), Some(&filter)); let filter_model = gtk::FilterListModel::new(Some(&model), Some(&filter));

View file

@ -17,7 +17,7 @@ pub struct PlayerBar {
play_image: gtk::Image, play_image: gtk::Image,
pause_image: gtk::Image, pause_image: gtk::Image,
player: Rc<RefCell<Option<Rc<Player>>>>, player: Rc<RefCell<Option<Rc<Player>>>>,
playlist_cb: Rc<RefCell<Option<Box<dyn Fn() -> ()>>>>, playlist_cb: Rc<RefCell<Option<Box<dyn Fn()>>>>,
} }
impl PlayerBar { impl PlayerBar {
@ -37,7 +37,7 @@ impl PlayerBar {
get_widget!(builder, gtk::Image, pause_image); get_widget!(builder, gtk::Image, pause_image);
let player = Rc::new(RefCell::new(None::<Rc<Player>>)); let player = Rc::new(RefCell::new(None::<Rc<Player>>));
let playlist_cb = Rc::new(RefCell::new(None::<Box<dyn Fn() -> ()>>)); let playlist_cb = Rc::new(RefCell::new(None::<Box<dyn Fn()>>));
previous_button.connect_clicked(clone!(@strong player => move |_| { previous_button.connect_clicked(clone!(@strong player => move |_| {
if let Some(player) = &*player.borrow() { if let Some(player) = &*player.borrow() {
@ -74,8 +74,8 @@ impl PlayerBar {
duration_label, duration_label,
play_image, play_image,
pause_image, pause_image,
player: player, player,
playlist_cb: playlist_cb, playlist_cb,
} }
} }
@ -164,7 +164,7 @@ impl PlayerBar {
} }
} }
pub fn set_playlist_cb<F: Fn() -> () + 'static>(&self, cb: F) { pub fn set_playlist_cb<F: Fn() + 'static>(&self, cb: F) {
self.playlist_cb.replace(Some(Box::new(cb))); self.playlist_cb.replace(Some(Box::new(cb)));
} }
} }

View file

@ -40,7 +40,7 @@ impl UploadSection {
let section = Section::new(&gettext("Upload"), &list); let section = Section::new(&gettext("Upload"), &list);
let this = Rc::new(Self { let this = Rc::new(Self {
widget: section.widget.clone(), widget: section.widget,
backend, backend,
switch, switch,
}); });