Remove all runtime expects and unwraps

This commit is contained in:
hodasemi 2019-04-28 14:13:36 +02:00
parent 4aa7edf16a
commit 19b0719719
8 changed files with 242 additions and 175 deletions

View file

@ -57,12 +57,14 @@ impl Value {
T: FromStr,
{
match self {
Value::Array(value_array) => for value_string in value_array {
Value::Array(value_array) => {
for value_string in value_array {
match value_string.parse::<T>() {
Ok(val) => array.push(val),
Err(_) => return Err(format!("error parsing value {}", value_string)),
}
},
}
}
_ => return Err("key_value has wrong format".to_string()),
};
@ -121,7 +123,7 @@ pub fn read_config(file_name: &str) -> Result<HashMap<String, HashMap<String, Va
trimmed_value.remove(0);
trimmed_value.pop();
let mut value_split = trimmed_value.split(";");
let value_split = trimmed_value.split(";");
let value_array = value_split.map(|v| v.trim().to_string()).collect();
if let Some(ref mut section) = current_section {

View file

@ -33,7 +33,7 @@ impl serenity::framework::standard::Command for List {
let mut output = String::new();
{
let playlist_mutex = self.media.playlist_mut();
let playlist_mutex = self.media.playlist_mut()?;
let playlist = playlist_mutex.borrow();
output += &format!(

View file

@ -31,7 +31,7 @@ impl serenity::framework::standard::Command for Pause {
return Ok(());
}
let song_lock = self.media.song_mut();
let song_lock = self.media.song_mut()?;
if let Some(song) = song_lock.borrow_mut().deref_mut() {
let song_clone = song.clone();

View file

@ -4,7 +4,6 @@ use serenity;
use serenity::voice::LockedAudio;
use std::cell::RefCell;
use std::fs;
use std::ops::{Deref, DerefMut};
use std::sync::{Arc, MutexGuard};
@ -13,7 +12,6 @@ use rand::{seq::SliceRandom, thread_rng};
use super::super::prelude::*;
use rusqlite::params;
use rusqlite::types::{FromSql, ToSql};
pub struct Play {
media: Arc<MediaData>,
@ -77,14 +75,13 @@ impl Play {
ctx: &mut serenity::client::Context,
msg: &serenity::model::channel::Message,
mut source: Vec<Song>,
) {
) -> Result<(), String> {
{
let playlist_mutex = self.media.playlist_mut();
let playlist_mutex = self.media.playlist_mut()?;
playlist_mutex.borrow_mut().append(&mut source);
}
let manager_lock = ctx.data.lock().get::<VoiceManager>().cloned().unwrap();
if let Some(manager_lock) = ctx.data.lock().get::<VoiceManager>().cloned() {
Self::check_join_channel(&manager_lock, msg);
let check_finished = {
@ -106,8 +103,9 @@ impl Play {
};
MediaData::start_playing(&self.media, check_finished, msg.channel_id, &manager_lock);
}
//MediaData::start_thread(&self.media, msg.channel_id, &manager_lock);
Ok(())
}
fn handle_http_request(
@ -115,20 +113,27 @@ impl Play {
ctx: &mut serenity::client::Context,
msg: &serenity::model::channel::Message,
url: &String,
) {
let sql = self.media.lock_db();
) -> Result<(), String> {
let sql = self.media.lock_db()?;
let mut stmt = sql
.prepare("SELECT name FROM Vulva3 WHERE link = ?")
.unwrap();
let mut stmt = match sql.prepare("SELECT name FROM Vulva3 WHERE link = ?") {
Ok(statement) => statement,
Err(_) => return Err("failed preparing data base access".to_string()),
};
let rows = stmt
.query_map(&[url], |row| row.get(0) as rusqlite::Result<String>)
.expect("failed requesting db");
let rows = match stmt.query_map(&[url], |row| row.get(0) as rusqlite::Result<String>) {
Ok(rows) => rows,
Err(_) => return Err("failed querying rows".to_string()),
};
let mut names = Vec::new();
for name_result in rows {
names.push(name_result.unwrap());
let name = match name_result {
Ok(name) => name,
Err(_) => return Err("failed getting name from row".to_string()),
};
names.push(name);
}
if names.len() > 0 {
@ -138,9 +143,9 @@ impl Play {
ctx,
msg,
names.iter().map(|n| Song { name: n.clone() }).collect(),
);
)?;
return;
return Ok(());
}
let source = match youtube_dl(&url) {
@ -152,7 +157,7 @@ impl Play {
.channel_id
.say(format!("Error using youtube-dl: {}", why)));
return;
return Ok(());
}
};
@ -165,45 +170,58 @@ impl Play {
for song in &source {
info = format!("{}\n\t{}", info, song.name);
sql.execute(
if sql
.execute(
"INSERT INTO Vulva3 (name, link)
VALUES (?1, ?2)",
params![song.name, url],
)
.expect("failed inserting song");
.is_err()
{
return Err("failed inserting songs into db".to_string());
}
}
print_error!(msg.channel_id.say(info));
self.append_songs(ctx, msg, source);
self.append_songs(ctx, msg, source)?;
Ok(())
}
fn handle_local_request(
&self,
ctx: &mut serenity::client::Context,
msg: &serenity::model::channel::Message,
) {
//print_error!(self.media.reset(ctx, msg));
) -> Result<(), String> {
let sql = self.media.lock_db()?;
let sql = self.media.lock_db();
let mut stmt = match sql.prepare("SELECT name FROM Vulva3") {
Ok(statement) => statement,
Err(_) => return Err("failed preparing data base access".to_string()),
};
let mut stmt = sql.prepare("SELECT name FROM Vulva3").unwrap();
let rows = stmt
.query_map(params![], |row| row.get(0) as rusqlite::Result<String>)
.expect("failed requesting db");
let rows = match stmt.query_map(params![], |row| row.get(0) as rusqlite::Result<String>) {
Ok(rows) => rows,
Err(_) => return Err("failed querying rows".to_string()),
};
let mut songs = Vec::new();
for name_result in rows {
songs.push(Song {
name: name_result.unwrap(),
});
let name = match name_result {
Ok(name) => name,
Err(_) => return Err("failed getting name from row".to_string()),
};
songs.push(Song { name });
}
let mut rng = thread_rng();
songs.shuffle(&mut rng);
self.append_songs(ctx, msg, songs);
self.append_songs(ctx, msg, songs)?;
Ok(())
}
fn handle_song_request(
@ -211,37 +229,44 @@ impl Play {
ctx: &mut serenity::client::Context,
msg: &serenity::model::channel::Message,
pattern: &str,
) {
let sql = self.media.lock_db();
) -> Result<(), String> {
let sql = self.media.lock_db()?;
let mut stmt = sql
.prepare(&format!(
let mut stmt = match sql.prepare(&format!(
"SELECT name FROM Vulva3 WHERE name LIKE '%{}%'",
pattern
))
.unwrap();
)) {
Ok(statement) => statement,
Err(_) => return Err("failed preparing data base access".to_string()),
};
let rows = stmt
.query_map(params![], |row| row.get(0) as rusqlite::Result<String>)
.expect("failed requesting db");
let rows = match stmt.query_map(params![], |row| row.get(0) as rusqlite::Result<String>) {
Ok(rows) => rows,
Err(_) => return Err("failed querying rows".to_string()),
};
let mut songs = Vec::new();
for name_result in rows {
songs.push(Song {
name: name_result.unwrap(),
});
let name = match name_result {
Ok(name) => name,
Err(_) => return Err("failed getting name from row".to_string()),
};
songs.push(Song { name });
}
if !songs.is_empty() {
let mut rng = thread_rng();
songs.shuffle(&mut rng);
self.append_songs(ctx, msg, songs);
self.append_songs(ctx, msg, songs)?;
} else {
print_error!(msg
.channel_id
.say(format!("no song found with pattern {}", pattern)));
}
Ok(())
}
}
@ -262,7 +287,7 @@ impl serenity::framework::standard::Command for Play {
}
if args.len() == 0 {
if !Self::check_for_continue(self.media.song_mut()) {
if !Self::check_for_continue(self.media.song_mut()?) {
print_error!(msg.channel_id.say("Must provide a URL to a video or audio"));
}
} else if args.len() == 1 {
@ -273,11 +298,11 @@ impl serenity::framework::standard::Command for Play {
};
if arg == "--local" {
self.handle_local_request(ctx, msg);
self.handle_local_request(ctx, msg)?;
} else if arg.starts_with("http") {
self.handle_http_request(ctx, msg, &arg);
self.handle_http_request(ctx, msg, &arg)?;
} else {
self.handle_song_request(ctx, msg, &arg);
self.handle_song_request(ctx, msg, &arg)?;
}
} else {
print_error!(msg.channel_id.say("Unsupported argument list"));

View file

@ -27,13 +27,16 @@ impl serenity::framework::standard::Command for Remove {
) -> ::std::result::Result<(), serenity::framework::standard::CommandError> {
// (1)
let song_name = self.media.name_mut();
let song_name = self.media.name_mut()?;
let name: String = song_name.borrow().clone();
let sql = self.media.lock_db();
let sql = self.media.lock_db()?;
sql.execute("DELETE FROM Vulva3 WHERE name = ?", params![name])
.expect("failed deleting song");
if let Err(_) = sql.execute("DELETE FROM Vulva3 WHERE name = ?", params![name]) {
return Err(serenity::framework::standard::CommandError(
"failed executing sql delete".to_string(),
));
}
if !name.is_empty() {
match fs::remove_file(&name) {

View file

@ -31,8 +31,7 @@ impl serenity::framework::standard::Command for Skip {
return Ok(());
}
let mut manager_lock = ctx.data.lock().get::<VoiceManager>().cloned().unwrap();
if let Some(mut manager_lock) = ctx.data.lock().get::<VoiceManager>().cloned() {
let mut manager = manager_lock.lock();
let guild_id = match guild_id(msg.channel_id) {
@ -46,17 +45,16 @@ impl serenity::framework::standard::Command for Skip {
};
if let Some(handler) = handler(guild_id, &mut manager) {
let playlist_mutex = self.media.playlist_mut();
let playlist_mutex = self.media.playlist_mut()?;
let mut playlist = playlist_mutex.borrow_mut();
let song_mutex = self.media.song_mut();
let song_mutex = self.media.song_mut()?;
let mut song = song_mutex.borrow_mut();
if playlist.is_empty() {
print_error!(
msg.channel_id
.say("playlist is empty, no next song available")
);
print_error!(msg
.channel_id
.say("playlist is empty, no next song available"));
return Ok(());
} else {
@ -76,19 +74,20 @@ impl serenity::framework::standard::Command for Skip {
match self.media.next_callback.borrow().as_ref() {
Some(callback) => {
*song = Some(handler.play_returning_and_callback(source, callback.clone()));
let song_name = self.media.name_mut();
*song =
Some(handler.play_returning_and_callback(source, callback.clone()));
let song_name = self.media.name_mut()?;
*song_name.borrow_mut() = first.name.clone();
print_error!(
msg.channel_id
.say(format!("Skipped current song, now playing: {}", first.name))
);
print_error!(msg
.channel_id
.say(format!("Skipped current song, now playing: {}", first.name)));
}
None => println!("error getting callback from media"),
}
}
}
}
Ok(())
}

View file

@ -8,7 +8,6 @@ use std::sync::{Arc, Mutex, MutexGuard};
use super::prelude::*;
use rusqlite::types::ToSql;
use rusqlite::{params, Connection};
pub struct Song {
@ -61,17 +60,17 @@ impl MediaData {
msg: &serenity::model::channel::Message,
) -> Result<(), String> {
{
let playlist = self.playlist_mut();
let playlist = self.playlist_mut()?;
playlist.borrow_mut().clear();
}
{
let song = self.song_mut();
let song = self.song_mut()?;
*song.borrow_mut() = None;
}
{
let name = self.name_mut();
let name = self.name_mut()?;
*name.borrow_mut() = String::new();
}
@ -79,8 +78,7 @@ impl MediaData {
*self.next_callback.borrow_mut() = None;
}
{
let manager_lock = ctx.data.lock().get::<VoiceManager>().cloned().unwrap();
if let Some(manager_lock) = ctx.data.lock().get::<VoiceManager>().cloned() {
let mut manager = manager_lock.lock();
let guild_id = match guild_id(msg.channel_id) {
@ -105,20 +103,32 @@ impl MediaData {
Ok(())
}
pub fn lock_db(&self) -> MutexGuard<Connection> {
self.sql_data_base.lock().unwrap()
pub fn lock_db(&self) -> Result<MutexGuard<Connection>, String> {
match self.sql_data_base.lock() {
Ok(sql) => Ok(sql),
Err(_) => Err("failed locking db".to_string()),
}
}
pub fn song_mut(&self) -> MutexGuard<RefCell<Option<LockedAudio>>> {
self.current_song.lock().unwrap()
pub fn song_mut(&self) -> Result<MutexGuard<RefCell<Option<LockedAudio>>>, String> {
match self.current_song.lock() {
Ok(sql) => Ok(sql),
Err(_) => Err("failed locking current song".to_string()),
}
}
pub fn playlist_mut(&self) -> MutexGuard<RefCell<Vec<Song>>> {
self.playlist.lock().unwrap()
pub fn playlist_mut(&self) -> Result<MutexGuard<RefCell<Vec<Song>>>, String> {
match self.playlist.lock() {
Ok(sql) => Ok(sql),
Err(_) => Err("failed locking playlist".to_string()),
}
}
pub fn name_mut(&self) -> MutexGuard<RefCell<String>> {
self.song_name.lock().unwrap()
pub fn name_mut(&self) -> Result<MutexGuard<RefCell<String>>, String> {
match self.song_name.lock() {
Ok(sql) => Ok(sql),
Err(_) => Err("failed locking current song name".to_string()),
}
}
pub fn start_playing(
@ -131,7 +141,14 @@ impl MediaData {
) {
// check if there is already playing
let already_started = {
let song_lock = mediadata.song_mut();
let song_lock = match mediadata.song_mut() {
Ok(song) => song,
Err(msg) => {
println!("{}", msg);
return;
}
};
let song = song_lock.borrow();
song.is_some()
};
@ -175,13 +192,31 @@ impl MediaData {
}
};
let playlist_lock = mediadata.playlist_mut();
let playlist_lock = match mediadata.playlist_mut() {
Ok(playlist) => playlist,
Err(msg) => {
println!("{}", msg);
return;
}
};
let mut playlist = playlist_lock.borrow_mut();
let song_name_lock = mediadata.name_mut();
let song_name_lock = match mediadata.name_mut() {
Ok(name) => name,
Err(msg) => {
println!("{}", msg);
return;
}
};
let mut name = song_name_lock.borrow_mut();
let song_lock = mediadata.song_mut();
let song_lock = match mediadata.song_mut() {
Ok(song) => song,
Err(msg) => {
println!("{}", msg);
return;
}
};
let mut song = song_lock.borrow_mut();
if playlist.is_empty() {

View file

@ -32,8 +32,8 @@ pub fn channel_contains_author(
msg: &serenity::model::channel::Message,
) -> bool {
let (guild_id, voice_channel_id_bot) = {
let manager_lock = ctx.data.lock().get::<VoiceManager>().cloned().unwrap();
match ctx.data.lock().get::<VoiceManager>().cloned() {
Some(manager_lock) => {
let mut manager = manager_lock.lock();
let guild_id = match guild_id(msg.channel_id) {
Some(guild_id) => guild_id,
@ -61,6 +61,9 @@ pub fn channel_contains_author(
}
},
)
}
None => return false,
}
};
let author = &msg.author;