Clippy pedantic fixes

This commit is contained in:
2024-09-22 02:16:18 +03:00
parent 387a5eaf4a
commit f7008e882c
22 changed files with 162 additions and 275 deletions

View File

@@ -6,34 +6,34 @@ use crate::{config::ConfigWrapper, downloader::Downloader, manifest::{song::Song
pub async fn add(cfg: &ConfigWrapper, manifest: &mut Manifest, downloader: &mut Downloader, url: &String, name: &String, playlist: &String) -> anyhow::Result<()> {
pub fn song(cfg: &ConfigWrapper, manifest: &mut Manifest, downloader: &mut Downloader, url: &str, name: &String, playlist: &String) -> anyhow::Result<()> {
let mut playlists = manifest.get_playlists().keys().map(|f| f.clone()).collect::<Vec<String>>();
let mut playlists = manifest.get_playlists().keys().cloned().collect::<Vec<String>>();
playlists.sort();
if !is_supported_host(url::Url::from_str(&url)?) {
if !is_supported_host(&url::Url::from_str(url)?) {
log::error!("Invalid or unsupported host name");
return Ok(());
}
let song = Song::from_url_str(url.clone())?;
let song = Song::from_url_str(url.to_string())?;
manifest.add_song(playlist, name.clone(), song.clone());
manifest.save(None)?;
let should_download = crate::prompt::prompt_bool("Download song now?", Some(false));
let should_download = crate::prompt::yes_no("Download song now?", Some(false));
if should_download {
downloader.download_song(cfg, &name, &song, &playlist, manifest.get_format())?;
downloader.download_song(cfg, name, &song, playlist, manifest.get_format())?;
crate::process_manager::wait_for_procs_untill(0)?;
}
Ok(())
}
pub async fn add_playlist(cfg: &ConfigWrapper, manifest: &mut Manifest, downloader: &mut Downloader, url: &String, name: &String) -> anyhow::Result<()> {
pub fn playlist(cfg: &ConfigWrapper, manifest: &mut Manifest, downloader: &mut Downloader, url: &str, name: &String) -> anyhow::Result<()> {
let songs = downloader.download_playlist_nb(cfg, url, name, manifest.get_format())?;
if manifest.get_playlist(name).is_some() {

View File

@@ -4,14 +4,14 @@ use crate::{config::{cli::CliCommand, ConfigWrapper}, downloader::Downloader, ma
pub async fn command_run(cfg: &ConfigWrapper, manifest: &mut Manifest) -> anyhow::Result<()> {
pub fn command_run(cfg: &ConfigWrapper, manifest: &mut Manifest) -> anyhow::Result<()> {
log::info!("Is in term: {}", cfg.isatty);
//std::fs::write("./isatty", format!("{}\n", cfg.isatty))?;
let mut downloader = Downloader::new();
match (&cfg.cli.command, cfg.isatty) {
(None | Some(CliCommand::Download), true) => {
match downloader.download_all(manifest, &cfg).await {
match downloader.download_all(manifest, cfg) {
Ok(count) => log::info!("Downloaded {count} songs"),
Err(e) => {
log::error!("Failed to download songs: {e}");
@@ -23,12 +23,12 @@ pub async fn command_run(cfg: &ConfigWrapper, manifest: &mut Manifest) -> anyhow
match c {
CliCommand::Download => unreachable!(),
CliCommand::AddPlaylist { url, name } => {
if let Err(e) = add::add_playlist(cfg, manifest, &mut downloader, url, name).await {
if let Err(e) = add::playlist(cfg, manifest, &mut downloader, url, name) {
log::error!("Failed to run 'add-playlist' commmand: {e}");
}
}
CliCommand::Add { url, name, playlist } => {
if let Err(e) = add::add(cfg, manifest, &mut downloader, url, name, playlist).await {
if let Err(e) = add::song(cfg, manifest, &mut downloader, url, name, playlist) {
log::error!("Failed to run 'add' command: {e}");
}
}

View File

@@ -11,7 +11,7 @@ impl /* ComponentUi for */ ContextMenu {
let w = gui.windows.get_window::<GuiSongEditor>(WindowIndex::SongEdit);
w.set_active_song(pname, sname, song.get_url_str());
gui.windows.open(WindowIndex::SongEdit, true);
ui.close_menu()
ui.close_menu();
}
if ui.button("Download").clicked() {
@@ -19,7 +19,7 @@ impl /* ComponentUi for */ ContextMenu {
log::error!("{e}");
gui.throw_error(format!("Failed to download song {sname}: {e}"));
}
ui.close_menu()
ui.close_menu();
}
if ui.button("Open Source").clicked() {
@@ -27,18 +27,18 @@ impl /* ComponentUi for */ ContextMenu {
log::error!("{e}");
gui.throw_error(format!("Failed to open song source: {e}"));
}
ui.close_menu()
ui.close_menu();
}
if ui.button("Play").clicked() {
let p = crate::util::get_song_path(pname, sname, gui.manifest.get_format());
if !p.exists() {
gui.throw_error(format!("Song does not exist on disk"));
gui.throw_error("Song does not exist on disk".to_string());
} else if let Err(e) = open::that(p) {
log::error!("{e}");
gui.throw_error(format!("Failed to play song: {e}"));
}
ui.close_menu()
ui.close_menu();
}
if ui.button("Delete from disk").clicked() {
let p = crate::util::get_song_path(pname, sname, gui.manifest.get_format());
@@ -51,7 +51,7 @@ impl /* ComponentUi for */ ContextMenu {
}
if ui.button(RichText::new("Delete").color(Color32::RED)).clicked() {
gui.throw_error("TODO");
ui.close_menu()
ui.close_menu();
}
}
}

View File

@@ -3,8 +3,9 @@ use crate::ui::gui::{windows::WindowIndex, Gui};
use super::Component;
#[allow(clippy::pedantic)]
pub struct NavBar;
#[warn(clippy::pedantic)]
impl Component for NavBar {
fn ui(gui: &mut Gui, ctx: &egui::Context) {

View File

@@ -1,4 +1,4 @@
use egui::{Color32, RichText};
use egui::Color32;
use egui_extras::{Column, TableBuilder};
use crate::manifest::song::SongType;
@@ -60,7 +60,7 @@ impl ComponentUi for SongList {
let mut songs = Vec::new();
for (pname, p) in playlists {
for (sname, s) in p {
songs.push((pname.clone(), sname, s))
songs.push((pname.clone(), sname, s));
}
}
songs
@@ -91,10 +91,8 @@ impl ComponentUi for SongList {
if !s.get_url_str().contains(&filter_clean) {
continue;
}
} else if !filter_clean.is_empty() {
if !sname.to_lowercase().contains(&filter_clean) {
continue;
}
} else if !filter_clean.is_empty() && !sname.to_lowercase().contains(&filter_clean) {
continue;
}
body.row(18.0, |mut row| {
@@ -121,7 +119,7 @@ impl ComponentUi for SongList {
row.response()
.context_menu(|ui| ContextMenu::ui(gui, ui, &pname, &sname, &s));
})
});
}
});
});

View File

@@ -31,12 +31,10 @@ impl Gui {
let native_options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default()
.with_inner_size([400.0, 300.0])
.with_min_inner_size([300.0, 220.0]),
// .with_icon(
// // NOTE: Adding an icon is optional
// eframe::icon_data::from_png_bytes(&include_bytes!("../assets/icon-256.png")[..])
// .expect("Failed to load icon"),
// ),
.with_min_inner_size([300.0, 220.0])
.with_icon(
eframe::icon_data::from_png_bytes(&include_bytes!("../../../assets/icon.png")[..])?,
),
..Default::default()
};
@@ -50,9 +48,11 @@ impl Gui {
Ok(())
}
pub fn throw_error(&mut self, text: impl ToString) {
#[allow(clippy::pedantic)]
pub fn throw_error<S: ToString>(&mut self, text: S) {
let w = self.windows.get_window::<windows::error::GuiError>(WindowIndex::Error);
w.set_error_message(text);
w.set_error_message(&text);
self.windows.open(WindowIndex::Error, true);
}
}
@@ -66,7 +66,7 @@ impl eframe::App for Gui {
downloader: self.downloader.clone(),
manifest: self.manifest.clone(),
};
self.windows.ui(&mut state, ctx).unwrap();
self.windows.ui(&mut state, ctx);
self.cfg = state.cfg;
self.downloader = state.downloader;
self.manifest = state.manifest;

View File

@@ -3,6 +3,7 @@ use egui::{Color32, Label, RichText};
use super::{State, Window};
#[allow(clippy::pedantic)]
#[derive(Debug, Default)]
pub struct GuiError {
text: String,
@@ -26,7 +27,7 @@ impl Window for GuiError {
}
impl GuiError {
pub fn set_error_message<S: ToString>(&mut self, text: S) {
pub fn set_error_message<S: ToString>(&mut self, text: &S) {
self.text = text.to_string();
}
}

View File

@@ -1,7 +1,7 @@
use super::{State, Window};
#[allow(clippy::pedantic)]
#[derive(Debug, Default)]
pub struct GuiImportPlaylist {
ed_name: String,
@@ -41,7 +41,7 @@ impl Window for GuiImportPlaylist {
log::error!("Playlist {name} already exists");
}
let songs = state.downloader.download_playlist_nb(&state.cfg, &url, &name, &state.manifest.get_format()).unwrap();
let songs = state.downloader.download_playlist_nb(&state.cfg, &url, &name, state.manifest.get_format()).unwrap();
state.manifest.add_playlist(name.clone());
let playlist = state.manifest.get_playlist_mut(&name).expect("Unreachable");

View File

@@ -45,17 +45,17 @@ impl WindowManager {
}
#[allow(dead_code)]
pub fn is_open(&self, id: &WindowIndex) -> bool {
*self.opened.get(id).unwrap()
pub fn is_open(&self, id: WindowIndex) -> bool {
*self.opened.get(&id).unwrap()
}
pub fn open(&mut self, id: WindowIndex, open: bool) {
self.opened.insert(id, open);
}
pub fn ui(&mut self, state: &mut State, ctx: &egui::Context) -> anyhow::Result<()> {
pub fn ui(&mut self, state: &mut State, ctx: &egui::Context) {
for (id, window) in &mut self.windows {
if !self.opened.contains_key(&id) {
if !self.opened.contains_key(id) {
self.opened.insert(*id, false);
}
let open = self.opened.get_mut(id).unwrap();
@@ -63,8 +63,6 @@ impl WindowManager {
log::error!("Window {id:?} errored: {e}");
}
}
Ok(())
}
pub fn get_window<T: Window + 'static>(&mut self, id: WindowIndex) -> &mut Box<T> {

View File

@@ -1,6 +1,5 @@
use anyhow::{Result, bail};
use anyhow::bail;
use egui::Color32;
use crate::ui::gui::Gui;
use super::{State, Window};
@@ -44,7 +43,7 @@ impl Window for GuiSongEditor {
ui.horizontal(|ui| {
ui.label("Type: ");
ui.label(&song.get_type().to_string());
ui.label(song.get_type().to_string());
});
ui.horizontal(|ui| {
@@ -67,7 +66,7 @@ impl Window for GuiSongEditor {
bail!("Failed to get song (2)");
};
*song.get_url_str_mut() = self.ed_url.clone();
song.get_url_str_mut().clone_from(&self.ed_url);
}
let Some(playlist) = state.manifest.get_playlist_mut(&playlist) else {
@@ -86,10 +85,10 @@ impl Window for GuiSongEditor {
}
impl GuiSongEditor {
pub fn set_active_song(&mut self, pname: &String, sname: &String, url: &String) {
self.song.0 = pname.clone();
self.song.1 = sname.clone();
self.ed_name = sname.clone();
self.ed_url = url.clone();
pub fn set_active_song(&mut self, pname: &str, sname: &str, url: &str) {
self.song.0 = pname.to_string();
self.song.1 = sname.to_string();
self.ed_name = sname.to_string();
self.ed_url = url.to_string();
}
}

View File

@@ -3,10 +3,10 @@ use super::{State, Window};
#[derive(Debug, Default)]
pub struct GuiNewSong {
ed_type: SongType,
ed_name: String,
ed_playlist: Option<String>,
ed_url: String,
typ: SongType,
name: String,
playlist: Option<String>,
url: String,
}
impl Window for GuiNewSong {
@@ -18,33 +18,33 @@ impl Window for GuiNewSong {
ui.horizontal(|ui| {
ui.label("Type: ");
egui::ComboBox::from_id_source("new_song_window_type")
.selected_text(format!("{:?}", self.ed_type))
.selected_text(format!("{:?}", self.typ))
.show_ui(ui, |ui| {
ui.selectable_value(&mut self.ed_type, SongType::Youtube, "Youtube");
ui.selectable_value(&mut self.ed_type, SongType::Spotify, "Spotify");
ui.selectable_value(&mut self.ed_type, SongType::Soundcloud, "Soundcloud");
ui.selectable_value(&mut self.typ, SongType::Youtube, "Youtube");
ui.selectable_value(&mut self.typ, SongType::Spotify, "Spotify");
ui.selectable_value(&mut self.typ, SongType::Soundcloud, "Soundcloud");
}
);
});
ui.horizontal(|ui| {
ui.label("Name: ");
ui.text_edit_singleline(&mut self.ed_name);
ui.text_edit_singleline(&mut self.name);
});
ui.horizontal(|ui| {
ui.label("Playlist: ");
egui::ComboBox::from_id_source("new_song_window_playlist")
.selected_text(format!("{}", self.ed_playlist.clone().unwrap_or("".to_string())))
.selected_text(self.playlist.clone().unwrap_or_default())
.show_ui(ui, |ui| {
for p in state.manifest.get_playlists().keys() {
ui.selectable_value(&mut self.ed_playlist, Option::Some(p.clone()), p.as_str());
ui.selectable_value(&mut self.playlist, Option::Some(p.clone()), p.as_str());
}
}
);
});
ui.horizontal(|ui| {
ui.label("Url: ");
ui.text_edit_singleline(&mut self.ed_url);
ui.text_edit_singleline(&mut self.url);
});
if ui.button("Save").clicked() {
@@ -53,13 +53,13 @@ impl Window for GuiNewSong {
});
if save {
let Some(playlist) = state.manifest.get_playlist_mut(&self.ed_playlist.clone().unwrap()) else {
let Some(playlist) = state.manifest.get_playlist_mut(&self.playlist.clone().unwrap()) else {
panic!("couldnt find playlist from a preset playlist list????????????");
};
playlist.add_song(
self.ed_name.clone(),
Song::from_url_str(self.ed_url.clone()).unwrap().set_type(self.ed_type.clone()).clone()
self.name.clone(),
Song::from_url_str(self.url.clone()).unwrap().set_type(self.typ.clone()).clone()
);