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::error::SpotifyError;
use crate::error::SpotifyError::{LameConverterError, InvalidFormat};
use crate::error::SpotifyError::{InvalidFormat, LameConverterError};
/// Converts audio to MP3
pub enum AudioConverter {
@ -35,16 +35,16 @@ impl AudioConverter {
match lame.set_channels(2) {
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(_) => {}
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(_) => {}
Err(_) => return Err(LameConverterError("Bitrate".to_string()))
Err(_) => return Err(LameConverterError("Bitrate".to_string())),
};
match format {
@ -55,13 +55,13 @@ impl AudioConverter {
let decoder = OggStreamReader::new(ReadWrap::new(Box::new(read)))?;
let sample_rate = decoder.ident_hdr.audio_sample_rate;
// Init lame
match lame.set_sample_rate(sample_rate) {
match lame.set_sample_rate(sample_rate) {
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(_) => {}
Err(_) => return Err(LameConverterError("Init".to_string()))
Err(_) => return Err(LameConverterError("Init".to_string())),
};
Ok(AudioConverter::Ogg {

View file

@ -64,20 +64,20 @@ impl Downloader {
input: &str,
) -> Result<Option<Vec<SearchResult>>, SpotifyError> {
if let Ok(uri) = Spotify::parse_uri(input) {
if let Err(e) = self.add_uri(&uri).await {
return Err(e);
}
Ok(None)
} else {
let results: Vec<SearchResult> = self
.spotify
.search(input)
.await?
.into_iter()
.map(SearchResult::from)
.collect();
Ok(Some(results))
if let Err(e) = self.add_uri(&uri).await {
return Err(e);
}
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
@ -472,7 +472,7 @@ impl DownloaderInternal {
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 {
let t = Track::get(&session, alt).await?;
if t.available {

View file

@ -27,7 +27,7 @@ impl Settings {
client_id: client_id.to_string(),
client_secret: client_secret.to_string(),
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::config::SessionConfig;
use librespot::core::session::Session;
@ -89,14 +92,14 @@ impl Spotify {
/// Get search results for query
pub async fn search(&self, query: &str) -> Result<Vec<Track>, SpotifyError> {
Ok(self
.spotify
.search()
.search(query, [ItemType::Track], true, 50, 0, None)
.await?
.data
.tracks
.unwrap()
.items)
.spotify
.search()
.search(query, [ItemType::Track], true, 50, 0, None)
.await?
.data
.tracks
.unwrap()
.items)
}
/// Get all tracks from playlist

View file

@ -12,48 +12,47 @@ mod id3;
mod ogg;
pub enum TagWrap {
Ogg(OggTag),
Id3(ID3Tag),
Ogg(OggTag),
Id3(ID3Tag),
}
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
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())),
}
}
/// Get Tag trait
pub fn get_tag(&mut self) -> &mut dyn Tag {
match self {
TagWrap::Ogg(tag) => tag,
TagWrap::Id3(tag) => tag,
}
}
/// 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 {
// Set tag values separator
fn set_separator(&mut self, separator: &str);
fn set_raw(&mut self, tag: &str, value: Vec<String>);
fn set_field(&mut self, field: Field, value: Vec<String>);
fn set_release_date(&mut self, date: NaiveDate);
fn add_cover(&mut self, mime: &str, data: Vec<u8>);
fn save(&mut self) -> Result<(), SpotifyError>;
// Set tag values separator
fn set_separator(&mut self, separator: &str);
fn set_raw(&mut self, tag: &str, value: Vec<String>);
fn set_field(&mut self, field: Field, value: Vec<String>);
fn set_release_date(&mut self, date: NaiveDate);
fn add_cover(&mut self, mime: &str, data: Vec<u8>);
fn save(&mut self) -> Result<(), SpotifyError>;
}
#[derive(Debug, Clone)]
pub enum Field {
Title,
Artist,
Album,
TrackNumber,
DiscNumber,
AlbumArtist,
Genre,
Label,
Title,
Artist,
Album,
TrackNumber,
DiscNumber,
AlbumArtist,
Genre,
Label,
}