api: implemented switch-to

This commit is contained in:
Nickiel12 2022-12-23 09:55:17 -08:00
parent 7ad1c9cdf9
commit 6bb967fa6a

View file

@ -13,6 +13,7 @@ enum SousaCommands {
Play, Play,
Pause, Pause,
Search, Search,
SwitchTo,
} }
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
@ -27,21 +28,19 @@ struct CliArgs {
port: Option<String>, port: Option<String>,
/// The command to execute /// The command to execute
#[arg(index = 1, value_enum)] #[arg(index = 1, value_enum, requires("search_arg"))]
action: Option<SousaCommands>, action: Option<SousaCommands>,
/// The string to search for when paired with a "Search" action /// The string to search for when paired with a "Search" action
#[arg(index = 2, required_if_eq("action", "search"))] #[arg(index = 2, requires("field"))]
search_arg: Option<String>, search_arg: Option<String>,
/// The field to search for when running `search` /// The field to search for when running `search`
#[arg( #[arg(
short,
long, long,
required_if_eq("action", "search"),
value_parser(["title", "artist", "album"]) value_parser(["title", "artist", "album"])
)] )]
search_field: Option<String>, field: Option<String>,
// Add flag for "search filepaths too" // Add flag for "search filepaths too"
} }
@ -58,25 +57,21 @@ fn main() {
SousaCommands::Play => serde_json::to_string(&UIRequest::Play).unwrap(), SousaCommands::Play => serde_json::to_string(&UIRequest::Play).unwrap(),
SousaCommands::Pause => serde_json::to_string(&UIRequest::Pause).unwrap(), SousaCommands::Pause => serde_json::to_string(&UIRequest::Pause).unwrap(),
SousaCommands::Search => { SousaCommands::Search => {
let request: UIRequest = match cli.search_field.unwrap().to_lowercase().as_str() { let request = match parse_to_partialtag(cli.field.unwrap(), cli.search_arg.unwrap()) {
"title" => UIRequest::Search(PartialTag { Ok(tag) => UIRequest::Search(tag),
title: cli.search_arg, Err(_) => panic!(
..PartialTag::default() "Unknown Search type! Expected values are 'title', 'artist', and 'album'"
}),
"artist" => UIRequest::Search(PartialTag {
artist: cli.search_arg,
..PartialTag::default()
}),
"album" => UIRequest::Search(PartialTag {
album: cli.search_arg,
..PartialTag::default()
}),
_ => panic!(
"Unknown search type! Expected values are 'title', 'artist', and 'album'"
), ),
}; };
serde_json::to_string(&request).unwrap() serde_json::to_string(&request).unwrap()
} }
SousaCommands::SwitchTo => {
let request = match parse_to_partialtag(cli.field.unwrap(), cli.search_arg.unwrap()) {
Ok(tag) => UIRequest::SwitchTo(tag),
Err(_) => panic!("Unknown type!"),
};
serde_json::to_string(&request).unwrap()
}
}; };
socket.write_message(Message::Text(message_string)).unwrap(); socket.write_message(Message::Text(message_string)).unwrap();
@ -88,6 +83,24 @@ fn main() {
socket.close(None).unwrap(); socket.close(None).unwrap();
} }
fn parse_to_partialtag(field: String, value: String) -> Result<PartialTag, ()> {
match field.to_lowercase().as_str() {
"title" => Ok(PartialTag {
title: Some(value),
..PartialTag::default()
}),
"artist" => Ok(PartialTag {
artist: Some(value),
..PartialTag::default()
}),
"album" => Ok(PartialTag {
album: Some(value),
..PartialTag::default()
}),
_ => Err(()),
}
}
impl ServerResponse { impl ServerResponse {
fn pretty_print(self: &Self) -> () { fn pretty_print(self: &Self) -> () {
let mut table = Table::new(vec![ let mut table = Table::new(vec![