refactorial spring cleaning (cargo fmt + clippy)

This commit is contained in:
Henrik Friedrichsen
2019-03-26 20:51:39 +01:00
parent 2eea519e82
commit 4f8342da83
17 changed files with 129 additions and 134 deletions

View File

@@ -12,9 +12,11 @@ use ui::layout::Layout;
use ui::listview::ListView;
use ui::search::SearchView;
type CommandResult = Result<Option<String>, String>;
type CommandCb = dyn Fn(&mut Cursive, Vec<String>) -> CommandResult;
pub struct CommandManager {
commands:
HashMap<String, Box<dyn Fn(&mut Cursive, Vec<String>) -> Result<Option<String>, String>>>,
commands: HashMap<String, Box<CommandCb>>,
aliases: HashMap<String, String>,
}
@@ -26,12 +28,7 @@ impl CommandManager {
}
}
pub fn register<S: Into<String>>(
&mut self,
name: S,
aliases: Vec<S>,
cb: Box<dyn Fn(&mut Cursive, Vec<String>) -> Result<Option<String>, String>>,
) {
pub fn register<S: Into<String>>(&mut self, name: S, aliases: Vec<S>, cb: Box<CommandCb>) {
let name = name.into();
for a in aliases {
self.aliases.insert(a.into(), name.clone());
@@ -112,7 +109,7 @@ impl CommandManager {
v.set_view("search");
});
s.call_on_id("search", |v: &mut SearchView| {
if args.len() >= 1 {
if !args.is_empty() {
v.run_search(args.join(" "), spotify.clone());
}
});
@@ -145,11 +142,11 @@ impl CommandManager {
"move",
Vec::new(),
Box::new(move |s, args| {
if args.len() < 1 {
if args.is_empty() {
return Err("Missing direction (up, down, left, right)".to_string());
}
let dir = args.get(0).unwrap();
let dir = &args[0];
let amount: i32 = args
.get(1)
@@ -356,7 +353,7 @@ impl CommandManager {
match arg.chars().next().unwrap() {
'+' | '-' => {
spotify.seek_relative(arg.parse::<i32>().unwrap_or(0));
},
}
_ => {
spotify.seek(arg.parse::<u32>().unwrap_or(0));
}
@@ -369,11 +366,11 @@ impl CommandManager {
}
}
fn handle_aliases(&self, name: &String) -> String {
fn handle_aliases(&self, name: &str) -> String {
if let Some(s) = self.aliases.get(name) {
self.handle_aliases(s)
} else {
name.clone()
name.to_string()
}
}
@@ -394,9 +391,9 @@ impl CommandManager {
}
}
pub fn register_keybinding<'a, E: Into<cursive::event::Event>, S: Into<String>>(
pub fn register_keybinding<E: Into<cursive::event::Event>, S: Into<String>>(
this: Arc<Self>,
cursive: &'a mut Cursive,
cursive: &mut Cursive,
event: E,
command: S,
) {
@@ -412,7 +409,7 @@ impl CommandManager {
keybindings: Option<HashMap<String, String>>,
) {
let mut kb = Self::default_keybindings();
kb.extend(keybindings.unwrap_or(HashMap::new()));
kb.extend(keybindings.unwrap_or_default());
for (k, v) in kb {
Self::register_keybinding(this.clone(), cursive, Self::parse_keybinding(k), v);

View File

@@ -34,7 +34,7 @@ pub struct ConfigTheme {
pub cmdline_bg: Option<String>,
}
fn proj_dirs () -> ProjectDirs {
fn proj_dirs() -> ProjectDirs {
ProjectDirs::from("org", "affekt", "ncspot").expect("can't determine project paths")
}

View File

@@ -22,9 +22,9 @@ impl EventManager {
let (tx, rx) = unbounded();
EventManager {
tx: tx,
rx: rx,
cursive_sink: cursive_sink,
tx,
rx,
cursive_sink,
}
}

View File

@@ -174,7 +174,7 @@ fn main() {
layout.cmdline.set_on_edit(move |s, cmd, _| {
s.call_on_id("main", |v: &mut ui::layout::Layout| {
if cmd.len() == 0 {
if cmd.is_empty() {
v.clear_cmdline();
}
});

View File

@@ -30,7 +30,7 @@ fn get_metadata(queue: Arc<Queue>) -> HashMap<String, Variant<Box<RefArg>>> {
Variant(Box::new(
track
.map(|t| format!("spotify:track:{}", t.id))
.unwrap_or("".to_string()),
.unwrap_or_default(),
)),
);
hm.insert(
@@ -40,26 +40,24 @@ fn get_metadata(queue: Arc<Queue>) -> HashMap<String, Variant<Box<RefArg>>> {
hm.insert(
"mpris:artUrl".to_string(),
Variant(Box::new(
track.map(|t| t.cover_url.clone()).unwrap_or("".to_string()),
track.map(|t| t.cover_url.clone()).unwrap_or_default(),
)),
);
hm.insert(
"xesam:album".to_string(),
Variant(Box::new(
track.map(|t| t.album.clone()).unwrap_or("".to_string()),
)),
Variant(Box::new(track.map(|t| t.album.clone()).unwrap_or_default())),
);
hm.insert(
"xesam:albumArtist".to_string(),
Variant(Box::new(
track.map(|t| t.album_artists.clone()).unwrap_or(Vec::new()),
track.map(|t| t.album_artists.clone()).unwrap_or_default(),
)),
);
hm.insert(
"xesam:artist".to_string(),
Variant(Box::new(
track.map(|t| t.artists.clone()).unwrap_or(Vec::new()),
track.map(|t| t.artists.clone()).unwrap_or_default(),
)),
);
hm.insert(
@@ -68,9 +66,7 @@ fn get_metadata(queue: Arc<Queue>) -> HashMap<String, Variant<Box<RefArg>>> {
);
hm.insert(
"xesam:title".to_string(),
Variant(Box::new(
track.map(|t| t.title.clone()).unwrap_or("".to_string()),
)),
Variant(Box::new(track.map(|t| t.title.clone()).unwrap_or_default())),
);
hm.insert(
"xesam:trackNumber".to_string(),
@@ -78,9 +74,7 @@ fn get_metadata(queue: Arc<Queue>) -> HashMap<String, Variant<Box<RefArg>>> {
);
hm.insert(
"xesam:url".to_string(),
Variant(Box::new(
track.map(|t| t.url.clone()).unwrap_or("".to_string()),
)),
Variant(Box::new(track.map(|t| t.url.clone()).unwrap_or_default())),
);
hm
@@ -179,15 +173,18 @@ fn run_dbus_server(spotify: Arc<Spotify>, queue: Arc<Queue>, rx: mpsc::Receiver<
let property_loopstatus = {
let queue = queue.clone();
f.property::<String, _>("LoopStatus", ())
.access(Access::Read)
.on_get(move |iter, _| {
iter.append(match queue.get_repeat() {
RepeatSetting::None => "None",
RepeatSetting::RepeatTrack => "Track",
RepeatSetting::RepeatPlaylist => "Playlist",
}.to_string());
Ok(())
})
.access(Access::Read)
.on_get(move |iter, _| {
iter.append(
match queue.get_repeat() {
RepeatSetting::None => "None",
RepeatSetting::RepeatTrack => "Track",
RepeatSetting::RepeatPlaylist => "Playlist",
}
.to_string(),
);
Ok(())
})
};
let property_metadata = {
@@ -296,11 +293,11 @@ fn run_dbus_server(spotify: Arc<Spotify>, queue: Arc<Queue>, rx: mpsc::Receiver<
let property_shuffle = {
let queue = queue.clone();
f.property::<bool, _>("Shuffle", ())
.access(Access::Read)
.on_get(move |iter, _| {
iter.append(queue.get_shuffle());
Ok(())
})
.access(Access::Read)
.on_get(move |iter, _| {
iter.append(queue.get_shuffle());
Ok(())
})
};
let method_playpause = {
@@ -394,7 +391,7 @@ fn run_dbus_server(spotify: Arc<Spotify>, queue: Arc<Queue>, rx: mpsc::Receiver<
warn!("Unhandled dbus message: {:?}", m);
}
if let Ok(_) = rx.try_recv() {
if rx.try_recv().is_ok() {
let mut changed: PropertiesPropertiesChanged = Default::default();
changed.interface_name = "org.mpris.MediaPlayer2.Player".to_string();
changed.changed_properties.insert(
@@ -427,7 +424,7 @@ impl MprisManager {
run_dbus_server(spotify, queue, rx);
});
MprisManager { tx: tx }
MprisManager { tx }
}
pub fn update(&self) {

View File

@@ -38,7 +38,7 @@ impl ListItem for Playlist {
.map(|t| t.id.clone())
.collect();
let ids: Vec<String> = self.tracks.iter().map(|t| t.id.clone()).collect();
ids.len() > 0 && playing == ids
!ids.is_empty() && playing == ids
}
fn display_left(&self) -> String {
@@ -150,7 +150,7 @@ impl Playlists {
store.len() - 1
}
pub fn overwrite_playlist(&self, id: &str, tracks: &Vec<Track>) {
pub fn overwrite_playlist(&self, id: &str, tracks: &[Track]) {
debug!("saving {} tracks to {}", tracks.len(), id);
self.spotify.overwrite_playlist(id, &tracks);
@@ -158,7 +158,7 @@ impl Playlists {
self.save_cache();
}
pub fn save_playlist(&self, name: &str, tracks: &Vec<Track>) {
pub fn save_playlist(&self, name: &str, tracks: &[Track]) {
debug!("saving {} tracks to new list {}", tracks.len(), name);
match self.spotify.create_playlist(name, None, None) {
Some(id) => self.overwrite_playlist(&id, &tracks),

View File

@@ -27,7 +27,7 @@ impl Queue {
current_track: RwLock::new(None),
repeat: RwLock::new(RepeatSetting::None),
random_order: RwLock::new(None),
spotify: spotify,
spotify,
}
}
@@ -107,7 +107,7 @@ impl Queue {
let first = match *self.current_track.read().unwrap() {
Some(index) => index + 1,
None => q.len()
None => q.len(),
};
let mut i = first;
@@ -158,7 +158,9 @@ impl Queue {
q.clear();
let mut random_order = self.random_order.write().unwrap();
random_order.as_mut().map(|o| o.clear());
if let Some(o) = random_order.as_mut() {
o.clear()
}
}
pub fn play(&self, index: usize, reshuffle: bool) {

View File

@@ -80,10 +80,10 @@ impl Worker {
player: Player,
) -> Worker {
Worker {
events: events,
commands: commands,
player: player,
session: session,
events,
commands,
player,
session,
play_task: Box::new(futures::empty()),
refresh_task: Box::new(futures::stream::empty()),
token_task: Box::new(futures::empty()),
@@ -157,17 +157,13 @@ impl futures::Future for Worker {
self.play_task = Box::new(futures::empty());
}
}
match self.refresh_task.poll() {
Ok(Async::Ready(_)) => {
self.refresh_task = match self.active {
true => {
progress = true;
self.create_refresh()
}
false => Box::new(futures::stream::empty()),
};
}
_ => (),
if let Ok(Async::Ready(_)) = self.refresh_task.poll() {
self.refresh_task = if self.active {
progress = true;
self.create_refresh()
} else {
Box::new(futures::stream::empty())
};
}
match self.token_task.poll() {
Ok(Async::Ready(_)) => {
@@ -208,7 +204,7 @@ impl Spotify {
elapsed: RwLock::new(None),
since: RwLock::new(None),
channel: tx,
user: user,
user,
};
// acquire token for web api usage
@@ -301,7 +297,7 @@ impl Spotify {
.elapsed
.read()
.expect("could not acquire read lock on elapsed time");
(*elapsed).clone()
(*elapsed)
}
fn set_since(&self, new_since: Option<SystemTime>) {
@@ -317,7 +313,7 @@ impl Spotify {
.since
.read()
.expect("could not acquire read lock on since time");
(*since).clone()
(*since)
}
fn refresh_token(&self) {
@@ -370,7 +366,7 @@ impl Spotify {
}
}
pub fn overwrite_playlist(&self, id: &str, tracks: &Vec<Track>) {
pub fn overwrite_playlist(&self, id: &str, tracks: &[Track]) {
// extract only track IDs
let mut tracks: Vec<String> = tracks.iter().map(|track| track.id.clone()).collect();
@@ -387,9 +383,10 @@ impl Spotify {
// send the remaining tracks in batches of max 100
while let Some(ref mut tracks) = remainder.clone() {
if let Some(_) = self.api_with_retry(|api| {
let result = self.api_with_retry(|api| {
api.user_playlist_add_tracks(&self.user, id, &tracks, None)
}) {
});
if result.is_some() {
// grab the next set of tracks
remainder = if tracks.len() > 100 {
Some(tracks.split_off(100))
@@ -512,7 +509,9 @@ impl Spotify {
None
});
self.channel.unbounded_send(WorkerCommand::Seek(position_ms)).unwrap();
self.channel
.unbounded_send(WorkerCommand::Seek(position_ms))
.unwrap();
}
pub fn seek_relative(&self, delta: i32) {

View File

@@ -44,7 +44,7 @@ pub fn load(cfg: &Config) -> Theme {
Theme {
shadow: false,
palette: palette,
borders: borders,
palette,
borders,
}
}

View File

@@ -45,10 +45,10 @@ impl Track {
track_number: track.track_number,
disc_number: track.disc_number,
duration: track.duration_ms,
artists: artists,
artists,
album: track.album.name.clone(),
album_artists: album_artists,
cover_url: cover_url,
album_artists,
cover_url,
url: track.uri.clone(),
}
}

View File

@@ -53,7 +53,7 @@ impl Layout {
screenchange: true,
last_size: Vec2::new(0, 0),
ev: ev.clone(),
theme: theme,
theme,
}
}
@@ -82,7 +82,7 @@ impl Layout {
pub fn set_view<S: Into<String>>(&mut self, id: S) {
let s = id.into();
let title = &self.views.get(&s).unwrap().title;
let title = &self.views[&s].title;
self.title = title.clone();
self.focus = Some(s);
self.cmdline_focus = false;
@@ -132,7 +132,7 @@ impl View for Layout {
// screen content
if let Some(ref id) = self.focus {
let screen = self.views.get(id).unwrap();
let screen = &self.views[id];
let printer = &printer
.offset((0, 1))
.cropped((printer.size.x, printer.size.y - 3 - cmdline_height))
@@ -168,10 +168,7 @@ impl View for Layout {
}
fn on_event(&mut self, event: Event) -> EventResult {
if let Event::Mouse {
position,
..
} = event {
if let Event::Mouse { position, .. } = event {
let error = self.get_error();
let cmdline_visible = self.cmdline.get_content().len() > 0;
@@ -186,7 +183,9 @@ impl View for Layout {
screen.view.on_event(event.clone());
}
} else if position.y < self.last_size.y - cmdline_height {
self.statusbar.on_event(event.relativized(Vec2::new(0, self.last_size.y - 2 - cmdline_height)));
self.statusbar.on_event(
event.relativized(Vec2::new(0, self.last_size.y - 2 - cmdline_height)),
);
}
return EventResult::Consumed(None);

View File

@@ -24,12 +24,12 @@ pub struct ListView<I: 'static + ListItem> {
impl<I: ListItem> ListView<I> {
pub fn new(content: Arc<RwLock<Vec<I>>>, queue: Arc<Queue>) -> Self {
Self {
content: content,
content,
last_content_length: 0,
selected: 0,
last_size: Vec2::new(0, 0),
scrollbar: ScrollBase::new(),
queue: queue,
queue,
}
}
@@ -70,9 +70,10 @@ impl<I: ListItem> View for ListView<I> {
let item = &content[i];
let style = if self.selected == i {
let fg = match item.is_playing(self.queue.clone()) {
true => *printer.theme.palette.custom("playing").unwrap(),
false => PaletteColor::Tertiary.resolve(&printer.theme.palette)
let fg = if item.is_playing(self.queue.clone()) {
*printer.theme.palette.custom("playing").unwrap()
} else {
PaletteColor::Tertiary.resolve(&printer.theme.palette)
};
ColorStyle::new(
ColorType::Color(fg),

View File

@@ -7,7 +7,7 @@ pub struct Modal<T: View> {
impl<T: View> Modal<T> {
pub fn new(inner: T) -> Self {
Modal { inner: inner }
Modal { inner }
}
}

View File

@@ -16,7 +16,7 @@ impl PlaylistView {
pub fn new(playlists: &Playlists, queue: Arc<Queue>) -> PlaylistView {
let list = ListView::new(playlists.store.clone(), queue).with_id("list");
PlaylistView { list: list }
PlaylistView { list }
}
}

View File

@@ -21,10 +21,7 @@ impl QueueView {
pub fn new(queue: Arc<Queue>, playlists: Arc<Playlists>) -> QueueView {
let list = ListView::new(queue.queue.clone(), queue.clone()).with_id("queue_list");
QueueView {
list: list,
playlists: playlists,
}
QueueView { list, playlists }
}
fn save_dialog_cb(s: &mut Cursive, playlists: Arc<Playlists>, id: Option<String>) {
@@ -61,7 +58,7 @@ impl QueueView {
let mut list_select: SelectView<Option<String>> = SelectView::new().autojump();
list_select.add_item("[Create new]", None);
for ref list in playlists.items().iter() {
for list in playlists.items().iter() {
list_select.add_item(list.meta.name.clone(), Some(list.meta.id.clone()));
}

View File

@@ -27,7 +27,7 @@ impl SearchView {
let searchfield = EditView::new()
.on_submit(move |s, input| {
if input.len() > 0 {
if !input.is_empty() {
s.call_on_id("search", |v: &mut SearchView| {
v.run_search(input, spotify.clone());
v.focus_view(&Selector::Id("list")).unwrap();
@@ -38,9 +38,9 @@ impl SearchView {
let list = ListView::new(results.clone(), queue).with_id("list");
SearchView {
results: results,
results,
edit: searchfield,
list: list,
list,
edit_focused: true,
}
}
@@ -67,9 +67,10 @@ impl SearchView {
}
fn pass_event_focused(&mut self, event: Event) -> EventResult {
match self.edit_focused {
true => self.edit.on_event(event),
false => self.list.on_event(event),
if self.edit_focused {
self.edit.on_event(event)
} else {
self.list.on_event(event)
}
}
}
@@ -111,7 +112,7 @@ impl View for SearchView {
}
fn on_event(&mut self, event: Event) -> EventResult {
let ret = match event {
match event {
Event::Key(Key::Tab) => {
self.edit_focused = !self.edit_focused;
EventResult::Consumed(None)
@@ -121,8 +122,6 @@ impl View for SearchView {
EventResult::Consumed(None)
}
_ => self.pass_event_focused(event),
};
ret
}
}
}

View File

@@ -22,10 +22,10 @@ pub struct StatusBar {
impl StatusBar {
pub fn new(queue: Arc<Queue>, spotify: Arc<Spotify>, cfg: &Config) -> StatusBar {
StatusBar {
queue: queue,
spotify: spotify,
queue,
spotify,
last_size: Vec2::new(0, 0),
use_nerdfont: cfg.use_nerdfont.unwrap_or(false)
use_nerdfont: cfg.use_nerdfont.unwrap_or(false),
}
}
}
@@ -87,13 +87,19 @@ impl View for StatusBar {
RepeatSetting::RepeatPlaylist => "[R] ",
RepeatSetting::RepeatTrack => "[R1] ",
}
}.to_string();
}
.to_string();
let shuffle = if self.use_nerdfont {
if self.queue.get_shuffle() { "\u{f99c} " } else { "" }
let shuffle = if self.queue.get_shuffle() {
if self.use_nerdfont {
"\u{f99c} "
} else {
"[Z]"
}
} else {
if self.queue.get_shuffle() { "[Z] " } else { "" }
}.to_string();
""
}
.to_string();
if let Some(ref t) = self.queue.get_current() {
let elapsed = self.spotify.get_current_progress();
@@ -105,7 +111,8 @@ impl View for StatusBar {
elapsed.as_secs() % 60
);
let right = repeat + &shuffle + &format!("{} / {} ", formatted_elapsed, t.duration_str());
let right =
repeat + &shuffle + &format!("{} / {} ", formatted_elapsed, t.duration_str());
let offset = HAlign::Right.get_offset(right.width(), printer.size.x);
printer.with_color(style, |printer| {
@@ -115,8 +122,7 @@ impl View for StatusBar {
printer.with_color(style_bar, |printer| {
printer.print((0, 0), &"".repeat(printer.size.x));
let duration_width =
(((printer.size.x as u32) * elapsed_ms) / t.duration) as usize;
let duration_width = (((printer.size.x as u32) * elapsed_ms) / t.duration) as usize;
printer.print((0, 0), &format!("{}{}", "=".repeat(duration_width), ">"));
});
} else {
@@ -145,8 +151,9 @@ impl View for StatusBar {
if let Event::Mouse {
offset,
position,
event
} = event {
event,
} = event
{
let position = position - offset;
if position.y == 0 {
@@ -158,20 +165,17 @@ impl View for StatusBar {
self.spotify.seek_relative(500);
}
if event == MouseEvent::Press(MouseButton::Left) ||
event == MouseEvent::Hold(MouseButton::Left)
if event == MouseEvent::Press(MouseButton::Left)
|| event == MouseEvent::Hold(MouseButton::Left)
{
if let Some(ref t) = self.queue.get_current() {
let f: f32 = position.x as f32 / self.last_size.x as f32;
let new = t.duration as f32 * f;
self.spotify.seek(new as u32);
}
}
} else {
if event == MouseEvent::Press(MouseButton::Left) {
self.queue.toggleplayback();
}
} else if event == MouseEvent::Press(MouseButton::Left) {
self.queue.toggleplayback();
}
EventResult::Consumed(None)