format code

Signed-off-by: Felix Breuer <fbreuer@pm.me>
This commit is contained in:
Felix Breuer 2022-01-27 17:08:47 +01:00
parent ea877ebde6
commit befad26e33
6 changed files with 70 additions and 67 deletions

1
rustfmt.toml Normal file
View file

@ -0,0 +1 @@
hard_tabs = true

View file

@ -3,7 +3,7 @@ use std::io::{Error, ErrorKind, Read, Seek};
use crate::downloader::{AudioFormat, Quality}; use crate::downloader::{AudioFormat, Quality};
use crate::error::SpotifyError; use crate::error::SpotifyError;
use crate::error::SpotifyError::{LameConverterError, InvalidFormat}; use crate::error::SpotifyError::{InvalidFormat, LameConverterError};
/// Converts audio to MP3 /// Converts audio to MP3
pub enum AudioConverter { pub enum AudioConverter {
@ -35,16 +35,16 @@ impl AudioConverter {
match lame.set_channels(2) { match lame.set_channels(2) {
Ok(_) => {} Ok(_) => {}
Err(_) => return Err(LameConverterError("Channels".to_string())) Err(_) => return Err(LameConverterError("Channels".to_string())),
}; };
match lame.set_quality(0) { match lame.set_quality(0) {
Ok(_) => {} Ok(_) => {}
Err(_) => return Err(LameConverterError("Quality".to_string())) Err(_) => return Err(LameConverterError("Quality".to_string())),
}; };
match lame.set_kilobitrate(bitrate) { match lame.set_kilobitrate(bitrate) {
Ok(_) => {} Ok(_) => {}
Err(_) => return Err(LameConverterError("Bitrate".to_string())) Err(_) => return Err(LameConverterError("Bitrate".to_string())),
}; };
match format { match format {
@ -55,13 +55,13 @@ impl AudioConverter {
let decoder = OggStreamReader::new(ReadWrap::new(Box::new(read)))?; let decoder = OggStreamReader::new(ReadWrap::new(Box::new(read)))?;
let sample_rate = decoder.ident_hdr.audio_sample_rate; let sample_rate = decoder.ident_hdr.audio_sample_rate;
// Init lame // Init lame
match lame.set_sample_rate(sample_rate) { match lame.set_sample_rate(sample_rate) {
Ok(_) => {} Ok(_) => {}
Err(_) => return Err(LameConverterError("Sample rate".to_string())) Err(_) => return Err(LameConverterError("Sample rate".to_string())),
}; };
match lame.init_params() { match lame.init_params() {
Ok(_) => {} Ok(_) => {}
Err(_) => return Err(LameConverterError("Init".to_string())) Err(_) => return Err(LameConverterError("Init".to_string())),
}; };
Ok(AudioConverter::Ogg { Ok(AudioConverter::Ogg {

View file

@ -64,20 +64,20 @@ impl Downloader {
input: &str, input: &str,
) -> Result<Option<Vec<SearchResult>>, SpotifyError> { ) -> Result<Option<Vec<SearchResult>>, SpotifyError> {
if let Ok(uri) = Spotify::parse_uri(input) { if let Ok(uri) = Spotify::parse_uri(input) {
if let Err(e) = self.add_uri(&uri).await { if let Err(e) = self.add_uri(&uri).await {
return Err(e); return Err(e);
}
Ok(None)
} else {
let results: Vec<SearchResult> = self
.spotify
.search(input)
.await?
.into_iter()
.map(SearchResult::from)
.collect();
Ok(Some(results))
} }
Ok(None)
} else {
let results: Vec<SearchResult> = self
.spotify
.search(input)
.await?
.into_iter()
.map(SearchResult::from)
.collect();
Ok(Some(results))
}
} }
/// Add URL or URI to queue /// Add URL or URI to queue
@ -472,7 +472,7 @@ impl DownloaderInternal {
Ok(()) Ok(())
} }
async fn find_alternative(session: &Session, track : Track) -> Result<Track, SpotifyError> { async fn find_alternative(session: &Session, track: Track) -> Result<Track, SpotifyError> {
for alt in track.alternatives { for alt in track.alternatives {
let t = Track::get(&session, alt).await?; let t = Track::get(&session, alt).await?;
if t.available { if t.available {

View file

@ -27,7 +27,7 @@ impl Settings {
client_id: client_id.to_string(), client_id: client_id.to_string(),
client_secret: client_secret.to_string(), client_secret: client_secret.to_string(),
refresh_ui_seconds: 1, refresh_ui_seconds: 1,
downloader: DownloaderConfig::new() downloader: DownloaderConfig::new(),
} }
} }

View file

@ -1,4 +1,7 @@
use aspotify::{Album, Artist, Client, ClientCredentials, ItemType, Playlist, PlaylistItemType, Track, TrackSimplified}; use aspotify::{
Album, Artist, Client, ClientCredentials, ItemType, Playlist, PlaylistItemType, Track,
TrackSimplified,
};
use librespot::core::authentication::Credentials; use librespot::core::authentication::Credentials;
use librespot::core::config::SessionConfig; use librespot::core::config::SessionConfig;
use librespot::core::session::Session; use librespot::core::session::Session;
@ -89,14 +92,14 @@ impl Spotify {
/// Get search results for query /// Get search results for query
pub async fn search(&self, query: &str) -> Result<Vec<Track>, SpotifyError> { pub async fn search(&self, query: &str) -> Result<Vec<Track>, SpotifyError> {
Ok(self Ok(self
.spotify .spotify
.search() .search()
.search(query, [ItemType::Track], true, 50, 0, None) .search(query, [ItemType::Track], true, 50, 0, None)
.await? .await?
.data .data
.tracks .tracks
.unwrap() .unwrap()
.items) .items)
} }
/// Get all tracks from playlist /// Get all tracks from playlist

View file

@ -12,48 +12,47 @@ mod id3;
mod ogg; mod ogg;
pub enum TagWrap { pub enum TagWrap {
Ogg(OggTag), Ogg(OggTag),
Id3(ID3Tag), Id3(ID3Tag),
} }
impl TagWrap { impl TagWrap {
/// Load from file
pub fn new(path: impl AsRef<Path>, format: AudioFormat) -> Result<TagWrap, SpotifyError> {
match format {
AudioFormat::Ogg => Ok(TagWrap::Ogg(OggTag::open(path)?)),
AudioFormat::Mp3 => Ok(TagWrap::Id3(ID3Tag::open(path)?)),
_ => Err(SpotifyError::Error("Invalid format!".into())),
}
}
/// Load from file /// Get Tag trait
pub fn new(path: impl AsRef<Path>, format: AudioFormat) -> Result<TagWrap, SpotifyError> { pub fn get_tag(&mut self) -> &mut dyn Tag {
match format { match self {
AudioFormat::Ogg => Ok(TagWrap::Ogg(OggTag::open(path)?)), TagWrap::Ogg(tag) => tag,
AudioFormat::Mp3 => Ok(TagWrap::Id3(ID3Tag::open(path)?)), TagWrap::Id3(tag) => tag,
_ => Err(SpotifyError::Error("Invalid format!".into())), }
} }
}
/// Get Tag trait
pub fn get_tag(&mut self) -> &mut dyn Tag {
match self {
TagWrap::Ogg(tag) => tag,
TagWrap::Id3(tag) => tag,
}
}
} }
pub trait Tag { pub trait Tag {
// Set tag values separator // Set tag values separator
fn set_separator(&mut self, separator: &str); fn set_separator(&mut self, separator: &str);
fn set_raw(&mut self, tag: &str, value: Vec<String>); fn set_raw(&mut self, tag: &str, value: Vec<String>);
fn set_field(&mut self, field: Field, value: Vec<String>); fn set_field(&mut self, field: Field, value: Vec<String>);
fn set_release_date(&mut self, date: NaiveDate); fn set_release_date(&mut self, date: NaiveDate);
fn add_cover(&mut self, mime: &str, data: Vec<u8>); fn add_cover(&mut self, mime: &str, data: Vec<u8>);
fn save(&mut self) -> Result<(), SpotifyError>; fn save(&mut self) -> Result<(), SpotifyError>;
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Field { pub enum Field {
Title, Title,
Artist, Artist,
Album, Album,
TrackNumber, TrackNumber,
DiscNumber, DiscNumber,
AlbumArtist, AlbumArtist,
Genre, Genre,
Label, Label,
} }