commit c5bc690d6b29448ccba8638ab0e339174fa64b1a
parent 382bde6bbbd1ca85ca2080ee997e9ded36cf1a29
Author: Robin <kroekerrobin@gmail.com>
Date: Mon, 13 Feb 2023 17:58:06 +0100
Remove compiler warning
Diffstat:
| M | src/main.rs | | | 131 | ++++++++++++++++++++++++++++++++++++++++--------------------------------------- |
1 file changed, 66 insertions(+), 65 deletions(-)
diff --git a/src/main.rs b/src/main.rs
@@ -74,7 +74,7 @@ fn add_ending_slash(path: String) -> String {
}
fn handle_text_message(api: &Api, msg: &Message, strings: &langs::Strings, songs_path: &String) {
- let text: &String = msg.text
+ let text: &str = msg.text
.as_ref()
.expect("Error while extracting text message.");
let chat_id: u64 = msg.from
@@ -84,77 +84,79 @@ fn handle_text_message(api: &Api, msg: &Message, strings: &langs::Strings, songs
.chat_id(ChatId::Integer(chat_id.try_into().unwrap()))
.text("")
.build();
- if text == "/start" {
- params.text = (*strings.start_msg).to_string();
- send_message(api, ¶ms);
- }
- else if text == "/list" {
- let songs = get_all_songs(songs_path);
- for song in &songs {
- let s: Vec<&str> = song.split(".").collect();
- let name = s.get(0).expect("Error: get(0)");
- let mut command: String = "/".to_string();
- command.push_str(name);
- command.push_str("\n");
- params.text.push_str(command.as_str());
- }
- send_message(api, ¶ms);
- }
- else {
- if text.starts_with("/") {
- let len = text.as_bytes().len();
- let maybe_file = get_song(&text[1..len], songs_path);
- match maybe_file {
- Ok(filepath) => {
- let mut path = PathBuf::new();
- path.push(filepath);
- let input_file = InputFile { path };
- let send_document_params = SendDocumentParams::builder()
- .chat_id(ChatId::Integer(chat_id.try_into().unwrap()))
- .document(File::InputFile(input_file))
- .build();
- send_document(api, &send_document_params);
- },
- Err(err) => {
- println!("{}", err.message);
- params.text = (*strings.song_not_found).to_string();
- send_message(api, ¶ms);
- }
+ match text {
+ "/start" => {
+ params.text = (*strings.start_msg).to_string();
+ send_message(api, ¶ms);
+ },
+ "/list" => {
+ let songs = get_all_songs(songs_path);
+ for song in &songs {
+ let s: Vec<&str> = song.split(".").collect();
+ let name = s.get(0).expect("Error: get(0)");
+ let mut command: String = "/".to_string();
+ command.push_str(name);
+ command.push_str("\n");
+ params.text.push_str(command.as_str());
}
- }
- else {
- match find_songs(&text, songs_path, strings) {
- Ok(files) => {
- let mut send_document_params = SendDocumentParams::builder()
- .chat_id(ChatId::Integer(chat_id.try_into().unwrap()))
- .document(File::String(String::new())) // Just to have a val
- .build();
- if files.len() == 1 {
- let file = files.get(0).expect("Err.");
- let mut filepath: String = songs_path.to_owned();
- filepath.push_str(file.as_str());
+ send_message(api, ¶ms);
+ },
+ _ => {
+ if text.starts_with("/") {
+ let len = text.as_bytes().len();
+ let maybe_file = get_song(&text[1..len], songs_path);
+ match maybe_file {
+ Ok(filepath) => {
let mut path = PathBuf::new();
path.push(filepath);
let input_file = InputFile { path };
- send_document_params.document = File::InputFile(input_file);
+ let send_document_params = SendDocumentParams::builder()
+ .chat_id(ChatId::Integer(chat_id.try_into().unwrap()))
+ .document(File::InputFile(input_file))
+ .build();
send_document(api, &send_document_params);
+ },
+ Err(err) => {
+ println!("{}", err.message);
+ params.text = (*strings.song_not_found).to_string();
+ send_message(api, ¶ms);
}
- else {
- for file in files {
- let f: Vec<&str> = file.split(".").collect();
- let name = f.get(0).expect("Error: get(0)");
- let mut command: String = "/".to_string();
- command.push_str(name);
- command.push_str("\n");
- params.text.push_str(command.as_str());
+ }
+ }
+ else {
+ match find_songs(&text.to_string(), songs_path, strings) {
+ Ok(files) => {
+ let mut send_document_params = SendDocumentParams::builder()
+ .chat_id(ChatId::Integer(chat_id.try_into().unwrap()))
+ .document(File::String(String::new())) // Just to have a val
+ .build();
+ if files.len() == 1 {
+ let file = files.get(0).expect("Err.");
+ let mut filepath: String = songs_path.to_owned();
+ filepath.push_str(file.as_str());
+ let mut path = PathBuf::new();
+ path.push(filepath);
+ let input_file = InputFile { path };
+ send_document_params.document = File::InputFile(input_file);
+ send_document(api, &send_document_params);
}
+ else {
+ for file in files {
+ let f: Vec<&str> = file.split(".").collect();
+ let name = f.get(0).expect("Error: get(0)");
+ let mut command: String = "/".to_string();
+ command.push_str(name);
+ command.push_str("\n");
+ params.text.push_str(command.as_str());
+ }
+ send_message(api, ¶ms);
+ }
+ },
+ Err(err) => {
+ params.text = (*strings.song_not_found).to_string();
send_message(api, ¶ms);
+ println!("{}", err.message);
}
- },
- Err(err) => {
- params.text = (*strings.song_not_found).to_string();
- send_message(api, ¶ms);
- println!("{}", err.message);
}
}
}
@@ -203,12 +205,11 @@ fn find_songs(search_string: &String, songs_path: &String, strings: &langs::Stri
}
fn get_song(song_name: &str, songs_path: &String) -> Result<String, ErrNotFound> {
- let mut filepath = String::new();
for file in get_all_songs(songs_path) {
let f: Vec<&str> = file.split(".").collect();
let name: &str = f.get(0).expect("Error: get(0)");
if name == song_name {
- filepath = songs_path.to_owned();
+ let mut filepath = songs_path.to_owned();
filepath.push_str(&file);
return Ok(filepath);
}